As you all know
SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sends information as plain text through the network. While using SSH, the entire session, including password transmission, is encrypted. Using SSH, we can access other systems remotely, execute commands, transfer/move files etc. SSH uses two major protocols. They are SSH1 or SSH-1 and SSH2 or SSH-2. Comparing both protocols, SSH-2 is more secure. SSH is available for Windows, Unix, Macintosh etc. In short SSH, is a powerful tool, and there are lots of possibilities. Here are some awesome tips and tricks on how to use SSH for daily administration duties.
Before we start let me brief some basic things regarding SSH.
To access a remote system using SSH, we need the following details.
1.A SSH client 2.Remote servername/IP 3.SSH Port number (By default SSH use port number as 22) 4.Username in remote server 5.Password of the user
If you are using a Unix/Linux machine, you can use Terminal as SSH client. Execute the following command in terminal section to access the remote machine using SSH.
This will prompt for password. Enter the password and you are in. For remote servers having custom ssh port, you need to mention the port number in the command line.
Default Configuration file of SSH = /etc/ssh/sshd_config
Default Port Number = 22
SSH Start/Stop/Restart = /etc/rc.d/init.d/sshd or /etc/init.d/ssh (start/stop/restart)
If we are making any changes to sshd_config, we need to restart the ssh service for changes to take effect.
Moving to the configuration part, there are a lots of directives in the sshd_config which is the main SSH configuration file. Lets discuss one by one.
1. Change SSH port number (Port)
As mentioned above, SSH runs on default port number 22. You will be able to find the following entry in your SSH configuration file for the SSH port number.
To change port number just replace 22 with the required port number. Its always good to use a number higher than 1024 because by default port scanners will not scan for high port numbers.
Using this directive you can enable/disable root login to server. Allowing direct root login through SSH is a security risk. So I would recommend you to disable root logins. Just find the following line to disable root login.
This option sets a time limit for the user to authenticate. If this time exceeds and the user has not yet authenticated, the server closes the connection from that user. Its better to leave the option to the default value. You can change it if required.
Enabling key based authentication means, you can access the server only using your private key. You need to generate a key pair and add the private key to your remote machine and add the public key to the server so that the server can be accessed using the keys. Un-comment the following lines if you wish to activate key based authentication.
PubkeyAuthentication yesAuthorizedKeysFile .ssh/authorized_keysRSAAuthentication yesPasswordAuthentication no
When we are using "
PasswordAuthentication no", any person who tries to connect to your SSH service and doesn't have a public key on the server, will be rejected without even seeing the login prompt.
Refer the following article for details regarding key based authentication.
If you wish to display some message/warning/announceme
nts for the users while logging into the server via ssh, just edit the following file and include your message.
You can edit the file named
/etc/issue and include any message which will be displayed before the login prompt.
SSH Tunneling:
SSH tunneling is an awesome method where you can tunnel all the traffic from your local machine to a remote machine. SSH tunneling consists of an encrypted tunnel created through SSH protocol. This method is helpful when you are behind some firewalls. You can easily overcome any restrictions due to firewall using tunneling.
Refer the following article for more details regarding tunneling.
Creating a X(Graphical) session over a ssh tunnel:
With this option you can run application/software in GUI mode through SSH. You need to enable the following line in your ssh configuration file to accomplish this.
You can run a sample program like
firefox or
kate using following commands.
firefox
Just wait for some time and you will see firefox browser on client screen. Sometimes it will throw a error like "Error: no display specified". You need to set display using the following command.
Now all except 10.10.10.111 is blocked from using SSH.
You can also mention the ip ranges.
SCP (Secure Copy)
SCP is a file copying/transferring tool through SSH that’s included with Openssh package. You should use the following command if you wish to copy/transfer files between machines running SSH.
Copying between two remote machines through your machine.
You should use the following command to copy between two remote machines. Please make sure that you have access to both remote machines. Just use the following command.
ssh root@remotemachine1 “cd /path/to/directory/to/copy && tar –cf - .” | ssh root@remotemachine2 “cd /directorytocopyto/ && tar –xf –“
In the above example, if a user try to access using the,wrong password, his access is blocked for 120 seconds. After that he can try login only once / 120 seconds. You may use /second, /minute, /hour, or /day in the above example.
Allowing SSH for particular IP only.
Eg:
iptables -A INPUT -p tcp -m state --state NEW –source 10.10.10.111 --dport 22 -j ACCEPT
Restart ssh service and you should be able to use the sftp server:
Conclusion:
I was not able to cover all topics. This is just a overview of important commands. Hope you found this article helpful! Feel free to drop your suggestion’s. Thanks for reading. ASV
Comments (1)
Commented:
Nice article..I would like to know which algorithm/encryption used by default when we do ssh to any linux server.