6 Tips to Use SSH Client Effectively For Connecting To Linux Servers

SSH is the most common tool to connect to a VPS. If you are someone who connects to servers as a part of their role, I have listed 6 easy-to-use and practical tips to make your experience more secure and productive.

Tip 1- Create SSH Profil…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Hadi Samadzad

SSH is the most common tool to connect to a VPS. If you are someone who connects to servers as a part of their role, I have listed 6 easy-to-use and practical tips to make your experience more secure and productive.

Photo by Christina @ wocintechchat.com on Unsplash

Tip 1- Create SSH Profiles

SSH profiles are an interesting way to make it easy to connect to a server using SSH. Let's say you are using a custom username and port number to connect to your server, so each time you would like to connect to the VPS, you need to use ssh command like this:

ssh [USERNAME]@[IP_ADRESS] -p [PORT_NUMBER]

Finding and entering these ssh parameters each time can be frustrating. Instead, you can simply create a profile using the SSH config file, so the next time, you can connect using the profile name rather than connection info. Profiles are stored in the ~/.ssh/config file. The below code snippet shows the corresponding configuration for the above-mentioned connection info.

Host [PROFILE_NAME]
    HostName [IP_ADDRESS]
    User [USERNAME]
    IdentitiesOnly yes
    IdentityFile ~/.ssh/id_rsa
    Port [PORT_NUMBER]

Now, you can access the VPS only with the profile name benefiting auto-completion. Enjoyed it? jump to the next one to get more fun.

ssh [PROFILE_NAME]

Tip 2- Connect without a Password

Although having a strong password can effectively increase the security level of your VPS, recalling it each time you want to log in can be frustrating. The good news is that if you are using specific machines to log in to your servers, you can set a public/private key pair so that you don't need to provide a password each time.
First, you should generate an ssh key pair on your local machine; then, press enter button a couple of times until they are generated (These steps are to set a location, a filename and a passphrase but they can remain default).

# Create a key pair
ssh-keygen -t rsa

Now, you need to copy the generated key to the remote server using ssh-copy-id command.

ssh-copy-id [USERNAME]@[IP_ADDRESS] -p [PORT_NUMBER]

# or if you have already set a profile configuration
ssh-copy-id [PROFILE_NAME]

Try to connect to the remote server and you should be logged in without being prompted for a password. Just keep in mind, you should not use key pairs on shared machines as can be a security vulnerability.

Tip 3- Block root Access

Although some VPS hosting services provide connection configuration using an out-of-the-box admin user rather than root, generally, you will connect to the VPS using root access. Removing root access from SSH guarantees that the username must be provided at login time as root is the default username.
Be careful that before blocking the root access you need to create an admin user you are going to use instead of root. Otherwise, you may lose access to the VPS.
Another plus for blocking root access is avoiding unintentional changes on the server as new admin user access can be limited. To create a new so-called admin user on Ubuntu uses the below code snippet. As well, to prepare the created user for SSH login, you need to set a password as soon as you create that.

# Add a new user (e.g. admin)
sudo useradd -m admin

# Set a password for new user
sudo passwd admin

# Add user to sudoers' list
sudo usermod -aG sudo admin

Now, to remove the root access you need to set PermitRootLogin entry to no in the SSH config file located in /etc/ssh/sshd_config and restart the sshd service.
 raw `root` endraw  Login in SSH Config File

# Restart sshd service
systemctl restart sshd

Tip 4- Changing SSH Port

Changing the port number is a simple way to hide a VPS from crawlers. SSH uses port 22 by default, however, you can simply modify it to any port number from 1024 to 65,535 (ports 0 to 1023 are reserved). Nevertheless, using a 5-digit and uncommon port number is recommended. To do this, you can modify the port number in /etc/ssh/sshd_config by setting Port entry and reset sshd service.
SSH Port Modification to  raw `22334` endraw
NOTE - Before updating the SSH port number, be sure that you have opened the new port number through ufw if the firewall is already active. I you don't know what this means, please don't touch the port number until you have read ufw tip in the below sections.

# Restart sshd service
sudo systemctl restart sshd

Tip 5- Block Unused Ports

Although firewall configuration is not an SSH-related tip, it is worth mentioning as it is a crucial step when you are trying to connect to a VPS. Using a firewall in Ubuntu is not that much complex as you might expect. In Ubuntu, there is an out-of-the-box firewall named Uncomplicated Firewall and as can be inferred from its name it's easy to use. ufw is the command-line tool for working with that. By activating ufw you can control the network stream using different filters like ports and IPs. To this end, you can use allow and deny commands to manage a port.
NOTE - Before activating the firewall, make sure the SSH port is allowed (default port 22 unless you have changed it before), otherwise, you will lose your access to the VPS.

# Open SSH port
sudo ufw allow ssh
# - OR -
sudo ufw allow [SSH_PORT]

# Block a port
sudo ufw deny [UNUSED_PORT]

# Activate firewall
sudo ufw enable

# Check firewall status
sudo ufw enable

Tip 6- Block ping Requests

Similar to Tip 5, this topic is not related to SSH, but it's a simple yet effective action to elevate the server's security. Ping service responds to icmp packets requested from a client and it is widely used to test whether a server is reachable over a specific IP address or not. However, it can be used by crawlers to find your server's IP address as you are responding to their ping requests.
Sample result of  raw `ping` endraw  command execution
To deactivate ping permanently (which means it won't be activated again after reboot) you need to switch to root user and set net.ipv4.icmp_echo_ignore_all = 1 in /etc/sysctl.conf file (append if it's not existing in the file) and run sysctl -p command afterwards. In some Linux distros, you may notice that the setting is gone. In this case, you can try to append the same line of setting to /etc/ufw/sysctl.conf file.

# Switch to root
su root

# Append the config file
nano /etc/sysctl.conf
# OR
nano /etc/ufw/sysctl.conf

Modified  raw `sysctl` endraw  Config File

ow sysctl -p

Now, you try to ping the server and make sure that it's working.

Final Words!

These tips will help you to have a better experience in working with an SSH client. Keep in mind, if you find something tedious in your everyday work, you may find a better way to do that. Just be careful, in working with a VPS, a simple mistake may result in a major security risk or maybe a loss in your access to your server.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Hadi Samadzad


Print Share Comment Cite Upload Translate Updates
APA

Hadi Samadzad | Sciencx (2023-02-03T17:11:47+00:00) 6 Tips to Use SSH Client Effectively For Connecting To Linux Servers. Retrieved from https://www.scien.cx/2023/02/03/6-tips-to-use-ssh-client-effectively-for-connecting-to-linux-servers/

MLA
" » 6 Tips to Use SSH Client Effectively For Connecting To Linux Servers." Hadi Samadzad | Sciencx - Friday February 3, 2023, https://www.scien.cx/2023/02/03/6-tips-to-use-ssh-client-effectively-for-connecting-to-linux-servers/
HARVARD
Hadi Samadzad | Sciencx Friday February 3, 2023 » 6 Tips to Use SSH Client Effectively For Connecting To Linux Servers., viewed ,<https://www.scien.cx/2023/02/03/6-tips-to-use-ssh-client-effectively-for-connecting-to-linux-servers/>
VANCOUVER
Hadi Samadzad | Sciencx - » 6 Tips to Use SSH Client Effectively For Connecting To Linux Servers. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/03/6-tips-to-use-ssh-client-effectively-for-connecting-to-linux-servers/
CHICAGO
" » 6 Tips to Use SSH Client Effectively For Connecting To Linux Servers." Hadi Samadzad | Sciencx - Accessed . https://www.scien.cx/2023/02/03/6-tips-to-use-ssh-client-effectively-for-connecting-to-linux-servers/
IEEE
" » 6 Tips to Use SSH Client Effectively For Connecting To Linux Servers." Hadi Samadzad | Sciencx [Online]. Available: https://www.scien.cx/2023/02/03/6-tips-to-use-ssh-client-effectively-for-connecting-to-linux-servers/. [Accessed: ]
rf:citation
» 6 Tips to Use SSH Client Effectively For Connecting To Linux Servers | Hadi Samadzad | Sciencx | https://www.scien.cx/2023/02/03/6-tips-to-use-ssh-client-effectively-for-connecting-to-linux-servers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.