Changing Default Port and Network Bindings
You'll learn how to move SSH from its well-known port 22 to a non-standard port and bind it to specific network interfaces, reducing automated attack exposure.
Why Change the Default Port?
Port 22 is like having a neon sign saying "SSH server here!" Attackers' automated scanners constantly hammer this port with brute-force attempts. While changing ports isn't true security (it's "security through obscurity"), it dramatically reduces noise from automated attacks and log clutter, letting you focus on legitimate threats.
Think of it like moving your front door from the street-facing side of your house to a side entrance—casual troublemakers walking by won't even notice it.
Using the Port Directive
Edit /etc/ssh/sshd_config and modify or add:
Port 2222
This moves SSH from port 22 to port 2222. Choose a port above 1024 (unprivileged range) that doesn't conflict with other services. Users must now connect with ssh -p 2222 user@host.
Binding to Specific Interfaces with ListenAddress
By default, SSH listens on all network interfaces (0.0.0.0). The ListenAddress directive restricts which interfaces accept connections:
ListenAddress 192.168.1.10
ListenAddress 10.0.0.5:2222
The first example binds to a specific internal IP only. The second combines IP and port specification. This is crucial when your server has multiple network interfaces—perhaps one facing the internet and one for internal management. You might want SSH only accessible from the management network.
You can specify multiple ListenAddress directives for different interfaces.
After changing these settings, restart SSH: systemctl restart sshd (test your configuration first to avoid lockout!).
Key Takeaway: Use the Port directive to move SSH away from the heavily-scanned port 22, and use ListenAddress to bind SSH only to specific network interfaces, reducing your attack surface by limiting where SSH is accessible.