SSH setup
Back to Home / Back to Notes

Complete SSH setup

Update&Install

sudo apt update && sudo apt upgrade
sudo apt install openssh-server

Check status & Enable SSH

sudo systemctl status ssh
sudo systemctl enable --now ssh

Check if SSH is enabled to start with the system

sudo systemctl is-enabled ssh

Allow SSH in Firewall

sudo ufw allow ssh

Connect to the server using the default port

ssh username@ip.address

Change the default SSH port

The new port must be located above port 1024

First, we will allow the new port in the firewall

sudo ufw allow 1234/tcp

Then we have to edit the SSH configuration file to set the new port we want to use.

sudo nano /etc/ssh/sshd_config

We will see this:

The last line that says "#Port 22" is the one that we want to edit by removing the "#" and changing the number from 22 to 1234

Then, we have to restart the SSH service to take into account the new port

sudo systemctl restart ssh

Connect to the server using the default port

ssh username@domain.name -p 1234

Setup SSH keys

Before we create a key, we'll ensure we don't have one already:

ls -al ~/.ssh

The existing keys should appear with the names "id_rsa" and "id_rsa.pub"

If we forgot to verify if we have them already and create new ones directly:

ssh-keygen

it will ask where we want to save them:
"Enter file in which to save the key (/home/benjamin/.ssh/id_rsa):"

We can press "Enter" to save on the default location, and we already have keys it asks if we want to overwrite them or not:
"Overwrite (y/n)?"

Copy the public key to the Host/Server

ssh-copy-id username@domain.name

With a differentport than the default one (22), the port must be specified before the username/domain if not, an error will be displayed:

"/usr/bin/ssh-copy-id: ERROR: Too many arguments. Expecting a target hostname, got:...

So the correct argument shuld be:

ssh-copy-id -p 1234 username@domain.name

Check IP of server

1- ip a

OR

2- ifconfig (requires apt install net-tools)