Installing and Configuring SSH in Linux


Installing and Configuring SSH in Linux

SSH is a secure shell to allow access to remote machines. In this article, i’ll describe about installing and configuring ssh in linux and more features of ssh.

Install Secure Shell

root@linuxpathfinder:/# yum -y install openssh-server openssh-clients

Start and enable sshd server:

root@linuxpathfinder:/# chkconfig sshd on
root@linuxpathfinder:/# service sshd start

Configure OpenSSH Server

Edit /etc/ssh/sshd_config, enter:

root@linuxpathfinder:/# vi /etc/ssh/sshd_config

To disable login as root, edit or add the following:

PermitRootLogin no

Restrict login to user user1 and user2 only via ssh:

AllowUsers user1 user2

Change ssh port i.e. run it on non-standard port like 2221
port 2221
Save and close the file. Restart sshd:

root@linuxpathfinder:/# service sshd restart

Identify SSH Client Version

You can check ssh client versions which can be determined as shown below.

$ ssh -V
OpenSSH_4.9p1, OpenSSL 1.0.1 Mar 14 2012

To login to a remote machine called test.example.com, type the following command at a shell prompt:

ssh test.example.com

The first time you ssh into a remote machine, you will see a message similar to the following:

The authenticity of host ‘test.example.com’ can’t be established.
DSA key fingerprint is 94:68:3a:3a:bc:f3:9a:9b:01:5d:b3:07:38:e2:11:0c.
Are you sure you want to continue connecting (yes/no)?

Type yes and enter to continue. This will add the server to your list of known hosts (~/.ssh/known_hosts) as shown in the following message:

Warning: Permanently added ‘test.example.com’ (RSA) to the list of known hosts.

For remote machine you’ll see a message asking the password. You will be at remote machine’s shell prompt after entering credentials. Use the as follows:

ssh username@test.example.com

You can also use the syntax ssh -l username test.example.com.

You can execute a command on remote host with ssh command without connected to shell prompt. Let suppose we want to run ls /var/log/messages in remote machine named test.example.com. We’ll perform this with following command:

ssh test.example.com ls /var/log/messages

When you will enter password, contents will be appeared of the remote directory and you will return to your local shell prompt.

Transfer files To/From Remote Host

SCP command is also another option to copy localhost to remote host vice versa.

Copy a file from remotehost to localhost:

localhost$scp allen@remotehost.example.com:/home/allen/remotehostfile.txt remotehostfile.txt

Copy a file from localhost to remotehost:

localhost$scp localhostfile.txt allen@remotehost.example.com:/home/allen/localhostfile.txt

Switching SSH session

When you connect to remote host with ssh, you may want to return to the localhost to perform any activity and go back to remote host again. There is an option available without disconnect the ssh session to the remote host as below way.

localhost$ssh -l allen remotehost
remotehost$

Type the escape character ~ and Control-Z. First type ~ and then Control-Z. See the results after entering command.

remotehost$ ~^Z
[1]+  Stopped                 ssh -l allen remotehost
localhost$

On localhost and the ssh remote client unix background:

localhost$ jobs
[1]+  Stopped                 ssh -l allen remotehost

On remote host without password to go background ssh remotehost session:

localhost$ fg %1
ssh -l allen remotehost

I hope this tutorial will be invaluable for you. Enjoy !

Avatar photo

Asif Khan

Responsible and proactive professional with more than 13 years of experience in IT systems, open source software applications, DevOps, Linux systems, and cloud operations. My main goals are to automate things, keep them safe, and make sure they are strong. I am very good at planning and building the infrastructure for services that people really want. I was drawn to the fast-paced world of cloud computing because it has resources that can be scaled up or down as needed. One of my best skills is being able to use a lot of different DevOps tools to set up, release management, and microservices ecosystems, as well as for provisioning, orchestration, and configuration management.