How to Limit SSH Connections to Local Network on Linux

SSH (Secure Shell) is a popular protocol used for securely connecting to remote servers and managing them. However, there may be times when you want to restrict SSH access to a server only from within your local network for security reasons. This can help prevent unauthorized access from external sources and enhance the overall security of your server. In this article, we will discuss how to limit SSH connections to a local network on Linux.

  1. Update SSH Configuration:

The first step is to update the SSH configuration file on your Linux server. You can do this by editing the /etc/ssh/sshd_config file using a text editor such as Nano or Vim. Open the file using the following command:

sudo nano /etc/ssh/sshd_config
  1. Add a Listen Address:

Add the following line to the SSH configuration file to specify the IP address or network range from which SSH connections are allowed:

ListenAddress 192.168.1.0

Replace 192.168.1.0 with the IP address of your local network. You can also specify a network range using CIDR notation, such as 192.168.1.0/24.

  1. Restart SSH Service:

After updating the configuration file, save the changes and exit the text editor. Then, restart the SSH service to apply the changes by running the following command:

sudo systemctl restart sshd
  1. Testing SSH Connection:

To test the changes, try connecting to your server using SSH from a device within your local network. If the connection is successful, it means that SSH access has been restricted to your local network.

  1. Additional Security Measures:

In addition to limiting SSH connections to a local network, you can also implement other security measures such as disabling password authentication and using key-based authentication. This can further enhance the security of your server and prevent unauthorized access.

To disable password authentication, you can edit the SSH configuration file and set PasswordAuthentication no. This will require users to authenticate using SSH keys instead of passwords.

Overall, limiting SSH connections to a local network on Linux is an effective way to enhance the security of your server and prevent unauthorized access. By following the steps outlined in this article, you can ensure that only devices within your local network can connect to your server via SSH.