The basic structure of an SSH command:
ssh [options] user@hostname📌 Components:
- ssh → The command itself
- user → Username of the remote system
- hostname → IP address or domain of the remote system
- options → Additional parameters (e.g., port, identity file)
ssh user@remote_host➡️ Connects to the remote server as user
ssh -p 2222 user@remote_host➡️ Connects via port 2222 (instead of default 22)
ssh -i /path/to/private_key user@remote_host➡️ Uses an SSH private key for authentication
ssh user@remote_host "ls -l"➡️ Runs the ls -l command remotely and displays output locally
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"➡️ Creates a 4096-bit RSA key pair
ssh-copy-id user@remote_host➡️ Enables passwordless authentication
cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"➡️ Manually appends the public key
ssh -L 8080:localhost:80 user@remote_host➡️ Forwards local port 8080 to remote port 80
ssh -R 9090:localhost:3000 user@remote_host➡️ Allows access to localhost:3000 from remote host on 9090
ssh -D 9090 user@remote_host➡️ Creates a SOCKS proxy on port 9090
Save SSH settings for quick access in ~/.ssh/config:
Host myserver
HostName remote_host
User username
Port 2222
IdentityFile ~/.ssh/id_rsa➡️ Now you can connect with:
ssh myserverscp file.txt user@remote_host:/path/to/destination➡️ Uploads file.txt to the remote server
scp -r folder/ user@remote_host:/path/to/destinationscp user@remote_host:/path/to/file.txt .sftp user@remote_host
sftp> put local_file.txt # Upload file
sftp> get remote_file.txt # Download file
sftp> exitssh -o ServerAliveInterval=60 user@remote_host➡️ Prevents SSH from timing out
Add this to ~/.ssh/config for faster multiple SSH connections:
Host *
ControlMaster auto
ControlPath ~/.ssh/socket-%r@%h:%p
ControlPersist 10mssh -f -N user@remote_host➡️ Runs SSH in background mode
exitor
Ctrl + D~.➡️ Instantly terminates the SSH session