Authentication in Linux via SSH

Listen to this Post

2025-02-05

Secure Shell (SSH) is a cryptographic network protocol used for secure data communication, remote command-line login, remote command execution, and other secure network services between two networked computers. SSH is widely used by system administrators to manage systems and applications remotely.

Setting Up SSH Authentication

To set up SSH authentication, you need to generate a pair of cryptographic keys: a private key and a public key. The private key remains on the client machine, while the public key is placed on the server.

Step 1: Generate SSH Key Pair

ssh-keygen -t rsa -b 4096 -C "[email protected]"

This command generates a new SSH key, using the provided email as a label. You will be prompted to enter a file in which to save the key and an optional passphrase for added security.

Step 2: Copy the Public Key to the Server

ssh-copy-id user@remote_host

This command copies the public key to the remote server, allowing you to authenticate without a password.

Step 3: SSH into the Server

ssh user@remote_host

Once the public key is copied, you can log in to the server without entering a password.

Advanced SSH Configuration

Disable Password Authentication

To enhance security, disable password authentication and rely solely on SSH keys.

1. Edit the SSH configuration file:

sudo nano /etc/ssh/sshd_config

2. Find the line `#PasswordAuthentication yes` and change it to:

PasswordAuthentication no

3. Restart the SSH service:

sudo systemctl restart sshd

Use SSH Agent for Key Management

SSH agent is a program that holds your private keys in memory, so you don’t need to enter your passphrase every time you use your key.

1. Start the SSH agent:

eval "$(ssh-agent -s)"

2. Add your private key to the SSH agent:

ssh-add ~/.ssh/id_rsa

What Undercode Say

SSH is an essential tool for secure remote administration of Linux systems. By using key-based authentication, you can significantly enhance the security of your systems. Here are some additional commands and tips to further secure your SSH setup:

  • Change the Default SSH Port: Edit `/etc/ssh/sshd_config` and change `Port 22` to a non-standard port to reduce the risk of automated attacks.
  • Use Fail2Ban: Install Fail2Ban to monitor and block IP addresses that show malicious signs.
  • Limit User Access: Use the `AllowUsers` directive in `/etc/ssh/sshd_config` to specify which users can log in via SSH.
  • Monitor SSH Logs: Regularly check `/var/log/auth.log` for any unauthorized access attempts.
  • Use Two-Factor Authentication: Implement 2FA for SSH using tools like Google Authenticator.

For more detailed guides and advanced configurations, you can refer to the following resources:
OpenSSH Manual
SSH.com Documentation

By following these practices, you can ensure that your SSH setup is both secure and efficient, protecting your systems from unauthorized access and potential security threats.

References:

Hackers Feeds, Undercode AIFeatured Image