Sunday, October 27, 2013

Installing and configuring SSH in Linux

SSH is a secure shell to allow access to remote machines. In this article, i’ll describe about ssh installation, configuration and more feature of ssh. 

Install Secure Shell:

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

Start and enable sshd server:
root@123linuxschool:/# chkconfig sshd on
root@123linuxschool:/# service sshd start

Configure OpenSSH Server

Edit /etc/ssh/sshd_config, enter:
root@123linuxschool:/# 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@123linuxschool:/# service sshd restart

Identify SSH Client Version:


Sometimes it may be necessary to identify the SSH client that currently use and it’s corresponding version number, 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.
After that you will see a message asking the password for the remote machine. After entering your password, you will be at the shell prompt for the remote machine. If you do not specify a username that is connected to the local client machine is passed to the remote machine. If you want to specify a different username, use the following command:
ssh username@test.example.comYou can also use the syntax ssh -l username test.example.com.


The ssh command can be used to execute a command on the remote machine without connected to the shell prompt. The syntax is ssh hostnamecommand. For example, if you want to run the command ls /var/log/messages in test.example.com the remote machine, type the following command at the prompt:
ssh test.example.com ls /var/log/messages
After entering the correct password, the contents of the remote directory /var/log/messages will appear, and you will return to your local shell prompt.

Transfer files To/From remote host:

Another common use of the ssh client is to copy files from/to remote host using scp command.
Copy a file from remotehost to localhost:
localhost$scp albin@remotehost.example.com:/home/albin/remotehostfile.txt remotehostfile.txt
Copy a file from localhost to remotehost:
localhost$scp localhostfile.txt albin@remotehost.example.com:/home/albin/localhostfile.txt

Switching SSH session:


Toggle or switching SSH Session, When you connect using SSH remotehost from the localhost, you may want to return to the localhost to perform any activity and go back to remote host again. In this case, no need to disconnect the ssh session to the remote host. See, below steps are as follows.
Login to remotehost from localhost: localhost$ssh -l albin remotehost
Now you are connected to remotehost: remotehost$
To return to the localhost temporarily, type the escape character ~ and Control-Z. When typing ~ you will not immediately see the screen until you press <Control-Z> and press enter. So, on the remotehost in a new line enter the following key strokes for the following work: ~<Control-Z>
remotehost$ ~^Z
[1]+  Stopped                 ssh -l albin remotehost
localhost$
Now you’re back to the localhost and the ssh remotehost client session runs as a typical unix background job, as following mention you can check:
localhost$ jobs
[1]+  Stopped                 ssh -l albin remotehost
You can go back to the remote host ssh without required password again by bringing the background ssh remotehost session job to foreground on the localhost. See the output as shown below:
localhost$ fg %1
ssh -l albin remotehost
remotehost$

No comments: