Secure Shell (SSH) is a widely-used protocol for securely connecting to remote servers. This cheat sheet provides essential SSH commands for managing remote connections, file transfers, and more.

Introduction:

SSH provides encrypted communication over a network, allowing users to connect to and manage remote servers securely. Key-based authentication and secure file transfer make SSH a fundamental tool for system administrators and developers.

Connecting to a Remote Server:

CommandDescription
ssh [username]@[hostname]Connect to a remote server with SSH.
ssh -p [port] [username]@[hostname]Specify a custom SSH port for connection.
ssh -i [private_key] [username]@[hostname]Connect using a specific private key file.
ssh -X [username]@[hostname]Enable X11 forwarding for GUI applications.

File Transfer with SCP:

CommandDescription
scp [file] [username]@[hostname]:[destination]Copy a file to a remote server.
scp -r [directory] [username]@[hostname]:[destination]Copy a directory and its contents recursively.

SSH Key Management:

CommandDescription
ssh-keygenGenerate a new SSH key pair.
ssh-copy-id [username]@[hostname]Copy the public key to the remote server.
ssh-add [private_key]Add a private key to the SSH agent.
ssh-keyscan [hostname] >> ~/.ssh/known_hostsAdd a host key to the known_hosts file.

Tunneling and Port Forwarding:

CommandDescription
ssh -L [local_port]:[destination_host]:[destination_port] [username]@[hostname]Local port forwarding.
ssh -R [remote_port]:[destination_host]:[destination_port] [username]@[hostname]Remote port forwarding.

Executing Commands Remotely:

CommandDescription
ssh [username]@[hostname] [command]Execute a command on the remote server.
ssh -t [username]@[hostname] [command]Force pseudo-terminal allocation.
ssh -T [username]@[hostname] [command]Disable pseudo-terminal allocation.

Miscellaneous:

CommandDescription
ssh -v [username]@[hostname]Verbose mode, display detailed debugging output.
ssh -o [option=value] [username]@[hostname]Set SSH options using the -o flag.
ssh -J [jump_host] [username]@[hostname]Use a jump host to reach the target host.
ssh -D [local_port] [username]@[hostname]Dynamic port forwarding (SOCKS proxy).

Conclusion

SSH is a critical tool for secure remote server management. This cheat sheet offers a quick reference for commonly used SSH commands, covering connection, file transfer, key management, and advanced features like port forwarding. Whether you are a system administrator or a developer, mastering these commands will enhance your efficiency in working with remote servers.