Overview
Connecting to a remote server typically involves using the ssh command with the server’s IP address or hostname. You can specify the remote user by either prepending the username followed by an @ symbol (e.g., user@hostname) or by using the -l flag. Remember, the remote server must have the SSH service running and allow connections through port 22. Also, valid authentication credentials are required to access the server. These credentials can be either a username and password combination or an SSH key pair for a passwordless login. In the following sections, we first explore basic SSH usage and then move on to setting up SSH key pairs.Basic SSH Connection
To connect from your laptop to a Linux host named node01, simply run:If you encounter connection issues, ensure that the SSH service is active on the remote server and that port 22 is open.
Using SSH Key Pairs for Authentication
A more secure authentication method involves using a cryptographic key pair—composed of a private key on the client and a public key installed on the remote server. With this setup, you can log in without repeatedly entering a password.Generating an SSH Key Pair
Generate the key pair on your client (e.g., your laptop) using the ssh-keygen command:Copying the Public Key to the Remote Server
To enable passwordless login, copy the public key to the remote server using the ssh-copy-id command. For example, if your username is mark and the remote server is node01, run:authorized_keys file inside the .ssh directory of your remote home folder. You can verify its content with:
Hardening the SSH Configuration
Once key-based authentication is set up, you can further secure your server by modifying the SSH configuration.Disabling Root Login
Disabling remote logins for the root account is a best security practice. This prevents unauthorized direct root access. Instead, use standard user accounts with privilege escalation tools like sudo for administrative tasks. Edit the SSH configuration file as the root user:Disabling Password-Based Authentication
Since key-based authentication is now in place, you can disable password-based authentication to further protect your server. In the same configuration file (/etc/ssh/sshd_config), find the PasswordAuthentication directive and update it as follows:
After restarting the SSH service, ensure you can log in with your SSH key. It is advised to keep an active session until you confirm that key-based authentication works properly, to avoid locking yourself out.
Summary
In this lesson, you learned how to secure your Linux nodes by hardening the SSH service. We covered:- Basic SSH connection commands
- Generating and using SSH key pairs for enhanced security
- Copying your public key to the remote server
- Securing the SSH configuration by disabling root login and password-based authentication