Show Menu
Cheatography

Learning SSH 2 Cheat Sheet (DRAFT) by

Cheat sheet for the LinkedIN course about SSH

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Installing OpenSSH

Command
Options
Arguments
Arguments
sudo apt
 
update
Check for updated package defini­tions
sudo apt install
 
opens-­server
Install OpenSSH server
sudo systemctl
 
status sshd
Inform­ation about SSH config­uration
sudo ufw
 
status
Check firewall status and rules
sudo ufw
 
allow ssh
Allows SSH traffic through the firewall
sudo ufw
 
enable
Enables firewall
sudo nano
 
/etc/s­sh/­ssh­d_c­onfig
Allows editing the SSH server config­uration
sudo nano
 
/etc/s­sh/­ssh­_config
Allows editing the SSH client config­uration
By default, SSH runs in port 22 using TCP protocol
SSH server config­uration is stored in /etc/s­sh/­ssh­d_c­onfig.
SSH client config­uration is stored in /etc/s­sh/­ssh­_co­nfig.

Managing Users and Access

Command
Argument
Descri­ption
AllowUser
leigh vishal stefan
Only allow these users to log in
DenyUser
bob mary paulina
Allow all users except these
AllowGroup
admins developers
Only allow users in these groups to log in
DenyGroup
sales marketing
Allow all users except those in this groups
Match Address
10.0.1.0/24
To be able to connect remotely only from 10.0.1.0/24 addresses
Match User
alice bob
Alice and bob are able to connect remotely according to subsequent parameters
All the inform­ation above is available in the file /etc/s­sh/­ssh­d_c­onfig
Precedence matters when defining access

Connecting to a server using a password

Command
Arguments
Descri­ption
ssh
username@ip address
Connect to a remote SSH server
The first time connection is establ­ished to a remote SSH server, a host finger­print is indicated in the screen, and if the finger­print is accepted, the local device saves the finger­print together with inform­ation about the connection into a folder into the file /home/­ssh­/kn­own­_hosts

Creating a key pair with ssh-keygen

Command
**Desc­ription
ssh-keygen
Generate public­/pr­ivate rsa key pair.
ssh-ke­yscan server ip address
Displays keys to share depending on the encryption algorithm to be used.
It is recomm­ended to generate a key pair for only one purpose (one user/one server). It is also recomm­ended to save the keys in separate folders in the /home/user/.ssh/ directory. A passphrase can also be added as an extra layer of security for the key pair.

Managing and using key pairs

Command
Descri­ption
ssh-co­py-id -i ~/.ssh/mykey.pub user@server
Add key to ~/.ssh­/au­tho­riz­ed_keys if access to ssh server already exists
Add key to ~/.ssh­/au­tho­riz­ed_keys out of band
Add key to ~/.ssh­/au­tho­riz­ed_keys if access to ssh server does not exists
When changing the config­uration of ~/etc/­ssh­/ss­hd_­config with nano, remember to restart the service for the new settings to apply, with sudo systemctl restart sshd.
When having a lot of keys, we can speed up the connection process by specifying which key we want to use to connect to the server, like: ssh user@server -i ~/.ssh/key directory

Client Config­uration Options

Host name
 
Hostname ip address
 
Port port number
 
User username
 
Identi­tyFile ~/.ssh/key name
For inform­ation about preced­ence's and priori­ties, consult man ssh_co­nfig. SSH obtains config­uration data from the following sources in the following order:
1. comman­d-line options
2. user's config­uration file (~/.ss­h/c­onfig)
3. system­-wide config­uration file (/etc/­ssh­/ss­h_c­onfig)

It is also good practice to change ~/.ssh­/config to be only read and write by the user with chmod 600 ~/.ssh­/config

Transf­erring Files with SFTP

Command
Option(s)
Argume­nt(s)
Descri­ption
sftp
 
user@ip address
Initiate SFTP connection with remote server.
bye
   
Terminates SFTP connection to remote server.
help
   
Shows a list of available commands while in SFTP mode, including commands to change working direct­ories.
put
 
file name
Sends a file from the local working directory to the remote local directory.
 

Transf­erring Files with SCP

Command
Option(s)
Argume­nt(s)
Descri­ption
scp
 
local file name user@ip address:
Copies a file from the local working directory to the remote working directory.
scp
 
user@ip address:remote file name local file name
Copies a file from the remote working directory to the local working directory.
The colon represents the remote user home directory, and both relative and absolute paths can be used to refer to a different directory than the home directory.

Multi-Step SSH Connec­tions

Command
Option(s)
Argume­nt(s)
Descri­ption
ssh
-J
user@server1,user@server2 user@server3
Enable multi-step SSH connection by providing the creden­tials to all interm­ediate and the final server to be accessed, without manually establ­ishing all connec­tions separa­tely.
 
Host myserver
 
Hostname ip address
 
Port port number
 
User username
 
Identi­tyFile ~/.ssh/key name
 
Host server2
 
Hostname ip address
 
ProxyJump user@ip address of myserver

Port Forwarding with SSH

Command
Option(s)
Argume­nt(s)
Descri­ption
ssh
-L
[bind_­addr:]port:host:port user@ip address
Local port forwar­ding.
ssh
-R
[bind_­addr:]port:host:port
Remote port forwar­ding.
ssh
-D
[bind_­addr:]port
Dynamic port forwar­ding.
 
-f
 
Fork the SSH process into the background
 
-n
 
Don't read from STDIN.
 
-N
 
Don't run remote commands.
 
-T
 
Don't allocate a TTY
ps x | grep
 
ssh
Find processes owned by the user, including those without a contro­lling terminal
kill
 
process port
Ends the process that belongs to a process port.
Port forwarding can also be configured in the client file ~/.ssh­/config
...
Host server1
\tHostname 10.0.1.110
\t# Access remote port 3306 through local port 3333
\tLocalF­orward 3333:l­oca­lho­st:3306
\t# Access local port 22 through remote port 5432
\tRemote­Forward 22:loc­alh­ost­:5432
\t# Starts a SOCKS proxy on local port 3000
\tDynami­cFo­rward 3000

Troubl­esh­ooting SSH

Command
Option(s)
Argume­nt(s)
Descri­ption
systemctl
status
sshd
Check the status of the SSH service.
systemctl
restart
sshd
Restarts the SSH service.
journalctl
-u
ssh
See the log for SSH services, to look at problems.
sudo ufw
 
status
Looks at the rules set for the firewall.
sudo cat
 
/etc/s­hadow/
Looks at the shadow file.
| grep
 
username
Pipes the search with grep to look for the username, if there is an exclam­ation mark at the beginning of the password field, that means the user is locked.
sudo usermod
-U
username
Unlocks the locked user account.

Securing a SSH Server

1) Don't allow the root user to log in
Permit­Roo­tLogin no (or prohib­it-­pas­sword)
2) Prevent password logins, and allow keys
Passwo­rdA­uth­ent­ication no
PubKey­Aut­hen­tic­ation yes
3) Change the service port
Port port number
4) Change the encryption ciphers the server allows
Ciphers ... (see man sshd_c­onfig)
5) Enact user control
AllowUser
DenyUser
AllowGroup
DenyGroup
6) Consider using software like Fail2ban to help prevent repeated malicious login attempts
7) Consider designing your system to use a bastion host
8) Consider putting your SSH server or bastion host behind a VPN

Tools That Use SSH (Mosh, Mobile Shell)

Command
Option(s)
Argume­nt(s)
Descri­ption
sudo apt install
 
mosh
Install Mosh (needed in both the client and the server)
sudo ufw
allow
60001/udp
Opens ports for Mosh (in the 60,000 range, only needed in the server).
mosh
 
user@ip address
Starts a Mosh session, just like a SSH connec­tion.
Mosh provides a fault-­tol­erant shell experi­ence. Mosh has to be configured in both the client and the server.