SSH Command Cheat Sheet
This cheat sheet provides a quick reference for common SSH commands and options. These commands are essential for remote server management, file transfers, and configuring secure connections.
Basic SSH Commands
Connecting to a Remote Server
ssh user@hostname
Establishes an SSH connection to the remote server. Replace user
with the username and hostname
with the server’s domain or IP address.
Specifying a Non-Default Port
ssh -p 2222 user@hostname
Connects to a remote server using a non-default port (e.g., 2222
instead of the default port 22
).
Running a Command Remotely
ssh user@hostname 'command'
Executes a command on the remote server without opening an interactive session.
Using SSH with a Specific Identity File (Private Key)
ssh -i /path/to/private_key user@hostname
Specifies an identity file (private key) for the SSH connection.
File Transfers with SSH
Copying Files to a Remote Server (scp
)
scp localfile.txt user@hostname:/remote/directory/
Copies localfile.txt
from the local machine to the specified remote directory.
Copying Files from a Remote Server (scp
)
scp user@hostname:/remote/directory/remotefile.txt /local/directory/
Copies remotefile.txt
from the remote server to the specified local directory.
Using rsync
for Efficient File Synchronization
rsync -avz -e ssh localfile.txt user@hostname:/remote/directory/
Uses rsync
to efficiently synchronize files between the local system and the remote server over SSH, transferring only the differences.
Port Forwarding and Tunneling
Local Port Forwarding
ssh -L local_port:remote_host:remote_port user@hostname
Forwards a local port to a remote host and port, making it possible to securely access services running on the remote machine. For example:
ssh -L 8080:localhost:80 user@hostname
Remote Port Forwarding
ssh -R remote_port:local_host:local_port user@hostname
Forwards a port from the remote server to the local machine. This is useful for exposing local services to the remote server.
Dynamic Port Forwarding (SOCKS Proxy)
ssh -D local_port user@hostname
Sets up a SOCKS proxy on the local machine, routing traffic through the SSH connection. This can be useful for browsing securely through a remote server.
Other Common Options
Verbose Mode
ssh -v user@hostname
Enables verbose output for debugging purposes. Additional verbosity can be added with -vv
or -vvv
.
Disabling Host Key Checking
ssh -o StrictHostKeyChecking=no user@hostname
Disables the host key verification step, often used in automated scripts. Be cautious, as this reduces security by not verifying the server’s identity.
Using an SSH Proxy (Jump Host)
ssh -J user@jump-host user@internal-host
Connects to a remote internal server by routing the connection through a jump host.
This cheat sheet provides a summary of essential SSH commands and options. These commands enable secure and efficient remote server management, file transfers, and tunneling over SSH.