SSH: The Ultimate Cheat Sheet for Secure Connections
Generating SSH Keys
Thessh-keygen
command is a versatile tool for generating, managing, and converting authentication keys for SSH.
-t
: Specifies the type of key to create. Options includersa
,dsa
,ecdsa
,ed25519
, etc. The default it generates RSA keys, e.g.,-t dsa
.-b
: Specifies the number of bits in the key. For RSA keys, the default is 2048 bits, but you can increase it to 4096 bits for stronger security, e.g.,-b 4096
.-C
: Adds a comment to the key, which can be useful for identifying the key later, e.g.,-C "youremail@example.com"
.-f
: Specifies the filename of the key file, e.g.,-f ~/.ssh/id_rsa
.-p
: for changing the passphrase.-N
: Provides a new passphrase for the key, e.g.,-N "your_passphrase"
.-P
: Provides the old passphrase when changing the passphrase of an existing key, e.g.,-P "old_passphrase"
.-l
: To find out the fingerprint of an SSH key.-m
: Specifies the key format, such asPEM
orRFC4716
, e.g.,-m PEM
.
Here’s an example command that generates an RSA key with 4096 bits, adds a comment, and saves it to a specified file:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f ~/.ssh/id_rsa
Connecting to a Remote Server
You can use the command to connect to a remote server and open a shell session there.ssh username@host
-i [file_path]
: To specify the private key location.-p [port]
: Specify the port to connect to on the remote host.-l [user]
: Log in using the specified username.[command]
: Execute a command on the remote host.-L [local_port:remote_host:remote_port]
: Forward a local port to a remote address.-R [remote_port:local_host:local_port]
: Forward a remote port to a local address.-N
: which does not open a shell or execute a program on the remote side.
Here’s an example command for port forwording:
local(8888) => Remote(80)ssh -f -N -L 8888:remote_host:80 username@remote_host
Remote(8888) => Local(80)ssh -f -N -R 8888:loal_host:80 username@remote_host
Mastering SSH commands is essential for secure and efficient remote server connections. Generating robust SSH keys, utilizing port forwarding, and employing specific commands enhance security and streamline your workflow. This cheat sheet assists you in navigating SSH complexities to ensure secure connections and smooth operations.