Linux Commands

How to SSH from the Linux Command Line

Any Linux system supports secure connection to remote machines. When you have a remote machine that you want to connect to from your client machine, you must get a secure protocol and know what steps to follow. The Secure Shell (SSH) is a secure protocol that encrypts the data before transferring it from the client to the host and relays it back, allowing the users to connect to a remote machine and utilize it.

You can SSH using the command line with a few requirements and steps. This post details everything you should know.

Understanding SSH

The SSH command is available in the Linux system and is handy when connecting to a remote machine. With the SSH connection, you can transfer files, tunnel applications, execute commands on the remote machine, etc.

When using SSH on the Linux command line, you will interact with the remote machine only after you connect to it. Moreover, the remote and client machine must have the “openssh” installed and enabled to establish the connection.

How to SSH from the Linux Command Line

Before you can create the SSH connection, you must have the “openssh” installed on your server and the client. Let’s begin with getting the remote machine ready for the connection.

Update the apt repository before you can install the “openssh”.

sudo apt update

After the update, you must install the “openssh-server” on the remote machine. Only when the server has the “openssh-server” can it accept a remote connection that is established on the client machine. The “openssh” relies on the configurations that are defined in the /etc/ssh/sshd_config.

Run the following command to install the “openssh-server”:

sudo apt-get install openssh-server

Once installed, check the “ssh” status to ensure if it is active (running).

sudo systemctl status ssh

If the status is not active, you can start it by running the following command:

sudo systemctl start ssh

Check the IP of the remote machine. Use the “ip a” command and get the IP that is associated with your network interface. In this case, we use the wireless interface “wlo1”.

On the client machine, you must install the “openssh-client” to use it to create the connection to the remote machine.

Similar to the remote machine, start by updating the apt repository.

Install the “openssh-client” by executing the following command:

sudo apt-get install openssh-client

Once you installed the “openssh-client” and the “openssh-server”, you are ready to establish the connection using SSH. To establish the connection, open your terminal on the client machine and connect to the IP of the remote server.

Our remote machine has its IP as 192.168.137.201. Thus, we connect to it by running the following SSH command:

ssh linuxhint@192.168.137.201

You must specify the hostname of the remote machine. Linuxhint is the hostname for our target remote server.

You will get prompted to authenticate the connection by selecting whether to continue. On the prompt that appears, type “yes” and press the enter key. You will see a prompt which confirms that the IP address of the remote machine has been added as one of the known hosts.

The last step is to type the password of the remote machine in the login prompt that appears on the terminal.

Once the entered password matches to the remote host’s, you have successfully used the SSH to connect to a remote machine using the Linux command line. That’s it!

Conclusion

SSH offers a secure way of connecting to a remote machine from a client machine. To use SSH on the Linux command line, install the “openssh-client” on the client machine and the “openssh-server” on the remote machine. Once installed, enable “ssh” on both machines. Lastly, execute the “ssh” command with the hostname and the IP address of the remote machine, and enter the password to connect to it.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.