How To Connect A Remote Linux Server Using SSH?

How To Connect A Remote Linux Server Using SSH?

SSH(Secure Shell)

Secure Shell(SSH) is a cryptographic networking protocol for securely getting access to a remote computer with the command line interface. It provides a secure channel over the unsecured network to execute a command and operate a system remotely in a client-server architecture. SSH was designed as an alternative of telnet and other unsecured remote shell protocols such as Berkely rlogin, rsh(remote shell) and rexec(remote execution) protocols, etc.


How Does It Work?

To establish an SSH connection between the client and server we need to have two components the first one is SSH client application and another one is SSH Dameon which is a corresponding server-side component. The SSH Daemon constantly listening to a specific TCP/IP port for possible client connection requests. The client uses host information to initiate a connection if the credential verified successfully on the server, an encrypted connection gets established.


Installing OpenSSH Client

OpenSSH is an open-source ssh tool that widely used for Unix and Unix like systems. It requires access to the terminal on the server and the computer that you want to connect. Many Linux distributions come with pre-installed OpenSSH client. You can check it by executing the command $ ssh in your terminal.
It will display something like this –

That means it is ready for use and can be connected to a remote system. Otherwise, you will have to install the application in your system. If you don’t have in your system, you can install it by using the given command-
$ sudo apt-get install openssh-client
Next, it will ask you to enter the superuser password, once you enter, it will start the installation process.
Once it gets completed you can use it to connect with server-side ssh application.


Installing OpenSSH Server

To start listening to the SSH connection requests made by the client a server needs to configured with the OpenSSH server. To check if OpenSSH server is installed on your computer or not, execute the following command in your terminal –
$ ssh localhost
If it responds something like this –


That means the OpenSSH server is not installed on your system. You can use the following command to install it on your system.
$ sudo apt-get install openssh-server

Once it gets installed you can check the status of server by using the given command-
$ sudo service ssh status

As you can see above our ssh server is active ( running).


How To Connect Via SSH

Now OpenSSH client and server application has been set up you can start using it by establishing an SSH connection between them-

Open your terminal and execute the following command –
$ ssh username@host_ip_address

For example-
$ ssh lalit@127.0.0.1

or if you are connecting on the internet the syntax for using ssh will be something like this-
$ ssh username@example.com


SSH Public Key Authentication

There are various ways to authenticate a user on a remote system one of them is public-key authentication. SSH uses public-key cryptography to authenticate the remote computer. It is a robust and more secure alternative to logging in with an account password. Asymmetric cryptographic algorithms are used to generate a pair of keys one is private which is kept secret and store in a local computer and the other one is the public key which can be shared anywhere.

Setup public key authentication using ssh on Linux-

To set up public-key authentication using SSH in a Linux/Unix System follow the given steps:
1. Login into your system that you would like to connect to a remote computer. And use the following command to generate key pair (RSA keys)-
$ ssh-keygen -t rsa

You will be prompted to enter –

  • Filename – Enter the filename and press enter to proceed if you don’t pass any name it will proceed with default one i.e. id_rsa
  • password- Enter at least five-character password and retype it if asked again and press enter. You can skip it also but protecting your keys with a password is a good idea. Now a pair of public and private keys will be generated in the /home/lalit/.ssh/ directory.

2. Use the following command to copy the public key to the remote server

$ ssh-copy-id root@server-ip-address

For example –

$ ssh-copy-id root@34.66.17.114(Use the IP address of the server that you want to connect)

Or instead of using the above method you can use SCP to copy the public key file to your account on the remote system. SCP(Secure copy) is a utility that can be used to transfer files securely between client and server computers.
$ scp ~/.ssh/id_rsa.pub remote_user@remote-ip-address:file-location

3. If you are using SCP to copy the file then login into the remote system using username and password and add your public key ~/.ssh/authorized_keys file if the remote system does not contain this file you can create by using the following command –
mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys

Now copy the content of the public key file to authorized_key you can use the following command
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

You can delete the copied public key file that is saved into your account in the remote system. To remove it execute the following command in your terminal-

rm  ~/id_rsa.pub

You need to keep in mind that you are using the correct path of a file or directory and then execute the command.

4. Now you should be able to establish an ssh connection between the local and remote computer. Enter the command like this $ ssh user@server-ip-location and hit enter, now it will prompt you to enter the password that you set on private key which is saved in your local computer. Once you enter and press enter or return key it will be get connected. Look at the image below-

Now I hope it gives you a basic understanding of using ssh and authenticating a user using public-key authentication. If you would like to say something on the topic or have a query regarding it please write to us in the comments below.

Previous File Compression and Archive Utility in Linux/Unix
Next How To Setup FTP Server In Linux/Unix?

About author

You might also like

Linux Topics 0 Comments

How Systemd Is Different From SysVinit?

What Is init? In Unix or Unix based systems init is the first process that gets started during system boot. It is the parent process of all the processes that

Linux Topics 0 Comments

Some Online Linux Terminal to Learn Linux

Are you looking for some online linux terminals to work with?? Yes, there are some Linux terminals available online which will be helpful for you to learn Linux on the

Linux Topics 0 Comments

How to Download, Extract and update the Linux Kernel source

When you are starting to learn Linux programming or Device driver development, then the first thing you need is the Linux Kernel which is available in a zipped archive format

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply