Is it possible to grand SSH access (using username and password) to a Centos 7 machine configured with an user using private key to connect?

2 min read 21-10-2024
Is it possible to grand SSH access (using username and password) to a Centos 7 machine configured with an user using private key to connect?

When it comes to securing a CentOS 7 server, SSH (Secure Shell) is a critical tool. Administrators often prefer using private key authentication for its enhanced security. However, there might be situations where you need to allow SSH access using both private keys and username/password combinations. This article addresses the possibility of configuring your CentOS 7 machine to allow both methods of authentication.

Problem Scenario

You may want to know if it is feasible to grant SSH access to a CentOS 7 machine that is primarily configured for user connections using a private key, while also allowing connections via a username and password. Below is the original code snippet for SSH configuration on CentOS 7:

# In your /etc/ssh/sshd_config file

# Key-based authentication configuration
PubkeyAuthentication yes
PasswordAuthentication no  # This is the default setting

# Other configurations...

Understanding SSH Authentication

By default, SSH allows for a private key authentication method, which is generally considered more secure than a password-based method. In private key authentication, the user has a unique key pair (public and private), and the server verifies the user's identity using the public key.

Can We Enable Both Authentication Methods?

Yes, it is indeed possible to allow both SSH access methods on your CentOS 7 machine. The key lies in modifying your sshd_config file, which is the configuration file for the SSH daemon.

Step-by-Step Configuration

  1. Open the SSH Configuration File:

    You need to open the sshd_config file using your preferred text editor. For example, you could use vi or nano.

    sudo vi /etc/ssh/sshd_config
    
  2. Modify Authentication Methods:

    Look for the following lines in the file and modify them as follows:

    # Uncomment if it exists and set to yes
    PubkeyAuthentication yes
    # Change to yes to allow password authentication
    PasswordAuthentication yes
    
  3. Restart SSH Service:

    After saving your changes, you’ll need to restart the SSH service to apply them.

    sudo systemctl restart sshd
    

Example Use Case

Imagine you are an administrator who manages several users on a CentOS 7 server. While most users have their SSH keys configured, there are times when new users need temporary access without the hassle of generating SSH keys immediately. By enabling both authentication methods, you allow new users to log in using their username and password while maintaining a secure environment for existing users using keys.

Security Implications

While enabling password authentication increases flexibility, it also introduces some security risks. Passwords can be weak or susceptible to brute-force attacks. Here are some suggestions to mitigate these risks:

  • Use Strong Password Policies: Require users to create strong, complex passwords that are difficult to guess.
  • Limit Login Attempts: You can install and configure fail2ban or a similar tool to prevent multiple failed login attempts.
  • Monitor Logs: Regularly check your SSH logs located at /var/log/secure for any unusual activities.

Conclusion

In summary, it is possible to configure a CentOS 7 machine to allow both SSH key-based authentication and username/password combinations. This flexibility can be advantageous in certain scenarios, such as onboarding new users or providing temporary access. However, always weigh the convenience against the potential security implications.

Useful Resources

With these considerations and adjustments, you can efficiently manage SSH access on your CentOS 7 server, catering to both secure private key users and those requiring password access.