Restricting SSH access to approved source addresses reduces who can attempt a server login. Use addresses the server actually sees, and keep a recovery route available before changing access rules.

Why does this matter?

A restricted source list reduces exposure to unwanted login attempts. It does not replace authentication: anyone sharing an allowed network still needs a valid user account and credentials.

Important safety tips before you start

Keep the existing SSH session open and confirm provider console access. Identify every administrator, automation account and VPN address that needs to connect, including IPv6 where used.

Back up the main configuration and any included files. Choose a new, unused backup filename rather than overwriting a previous recovery copy.

Use the public source address or VPN address visible to the server, not a private address behind your home router. A dynamic address may change and lock you out.

Different methods to restrict SSH access

Restrict user logins through OpenSSH

In /etc/ssh/sshd_config, outside any Match block, a global AllowUsers list can require both an approved account and source address:

These are documentation-only addresses. Replace them with the real usernames and source addresses, including all required accounts. With this setting, users not matching the list cannot log in.

Review existing AllowUsers, DenyUsers, group restrictions and included configuration; do not append an unrestricted username that defeats the intended source limit.

Validate syntax before applying:

sudo sshd -t

If validation succeeds, reload the appropriate service: commonly ssh on Debian/Ubuntu or sshd on RHEL-based systems. Confirm the actual service and service model first. Do not change the SSH port or root-login policy as part of the same untested change.

Restrict network connections through a firewall

Use the server’s existing firewall and any provider firewall to allow the approved sources to the actual SSH port. Review all active rules and IPv4/IPv6 paths.

Adding a source-specific allow rule alone does not restrict access if a general SSH allow rule remains. Add and test approved access before removing a broader rule. Do not enable a new firewall without accounting for the server’s other required services.

How to test your setup

Open a new connection from an approved address and verify the required administrator privileges. Test from a genuinely different, unapproved source as well; two devices behind the same router may share one public address.

Keep the existing session and console route until both tests behave as intended.

How to verify everything works well

For a specific login context, inspect the effective OpenSSH configuration:

sudo sshd -T -C user=adminuser,host=client.example,addr=203.0.113.10

Replace the example context with the tested user and source. Check the applicable access lists and authentication settings. This inspection supplements a real new-connection test; it does not prove the firewall permits or blocks traffic.

Best practices for everyone

Use tested SSH keys, maintain supported system updates and document approved addresses. Coordinate VPN and automation access with the restriction. Review entries when staff, services or source addresses change.

SSH access compatibility by operating system

OpenSSH configuration applies across supported Linux distributions, but service names, include files and firewall tools vary. UFW, firewalld and provider firewalls require their own maintained configuration procedures.

Do not rely on /etc/hosts.allow or /etc/hosts.deny without confirming that the installed SSH build supports TCP wrappers. Those legacy files are not a general solution for current systems.

Troubleshooting

If a new connection fails unexpectedly, use the existing session or console to check the source address, effective configuration and firewall rules. Restore the preserved settings where necessary, validate them and reload the correct service. Avoid repeatedly changing unrelated settings while investigating a lockout.