How to Install and Use SSH on Linux?

In this tutorial, I will teach you how to install and use SSH on Linux, we will:

  • Configure your local computer with SSH
  • Configure the remote computer with SSH
  • Check if the SSH port is open
  • Transfer files between computers with SSH

To create SSH connections you have to run the following command in the terminal:

ssh-keygen

This command creates a public key (id_rsa.pub) and a private key which are NECESSARY if we want to connect via SSH.

When entering the command it will ask you the following:

  • Where do we want to save the keys? By default it offers the following path: (/home/user/.ssh/id_rsa)
  • What to name the key?
  • It asks for an optional passphrase

You can see the recently created keys in the folder of the chosen path, for this just run the following command to see hidden files:

ls -l

If you run the cat command you can see the content:

cat .ssh/id_rsa

or

cat .ssh/id_rsa.pub

How to add the key to the remote computer?

The safest method is with the following command:

ssh-copy-id -i ~/.ssh/id_rsa.pub user@youripaddress

It will ask you if you want to proceed copying from your local computer to your remote computer and you answer yes.

Another method is by using the SCP command, here is the command:

scp ~/.ssh/id_rsa.pub user@youripaddress:/~/.ssh/authorized_keys

NOTE: If you have already used the SSH keys to connect to the remote computer, I do not recommend using the SCP command because it will overwrite the keys and you will lose your usual access.

The easiest way is literally to copy and paste the public key into .ssh/authorized_keys using the following command:

nano .ssh/authorized_keys

However, this is not recommended because when copying and pasting you might delete something or make typos, but if it’s easier for you, you can start with this method.

#1

The first thing we have to do is check if you already have SSH configured and you can check this with the following command:

sudo systemctl status ssh

#2

The second step you have to do is install and configure SSH on the Linux computer (client) you will use. This is your computer that you will use to connect to the remote computer.

#3

The third step is to install the “openssh-client” and you can do this with the following command:

sudo apt install openssh-client

If you want to use Windows as a client, use software called: Putty

#4

The fourth step is to configure the computer you want to connect to (the remote). To do this, it is necessary to install “openssh-server” and you can do this with the following command:

sudo apt install openssh-server

Once “openssh” is installed you can make sure it is running with the following command:

sudo systemctl status ssh

If it is not running or active, just “restart” it and you can do this with the following command:

sudo systemctl start ssh

#5

The fifth step is to determine the IP of the remote computer. That is, the address of the computer we will connect to. The easiest way to do this is to see the network settings from the UI. If you only have access via the Linux terminal, you can use the following command:

ip a

#6

The sixth step is to check if the Firewall is interfering in any way, because if we don’t check this, we might have connection problems. We can check this with the following command:

sudo lsof -i -P -n | grep LISTEN

If you have a complicated firewall, here is a command that can help you easily open port 22 from the terminal:

sudo ufw allow 22

Now the million-dollar question, how do we connect to the remote computer via SSH?

On your Linux computer run the following command:

ssh youruser@youripaddress

Then it will ask for the password, just enter it and you will be inside the remote computer and able to work with it as if you were in front of it.

BONUS: How to configure SSH login using only keys

Look for the file: /etc/ssh/sshd_config

Edit it with the “nano” command

nano /etc/ssh/sshd_config

Search with control + W for the word: PasswordAuthentication and change it from Yes to “NO”

Leave a Reply

Your email address will not be published. Required fields are marked *