It's worth noting that in this article, the search was performed on port 22, which is the default port for the SSH service.
The SSH service is perfect for maintaining the confidentiality and integrity of data exchanged between systems. In addition to this, one of its main advantages is server authentication using public key.
So this time, we'll make some configurations to enhance the security of this widely used remote administration service.
Most of the changes are made in the /etc/ssh/sshd_config file.
The OpenSSH server supports various authentications. We recommend using public key authentication. To do this, you need to create the key pair using the following link from the folks at Digital Ocean, which shows you how to create the key pair and add it to the server.
Most attacks on this service occur from automated tools trying common usernames and passwords on port 22.
If you change the port, you'll avoid most of these types of attacks.
Port 300
The ListenAddress directive specifies on which IP addresses the port will be opened to provide the service. If the system has more than one IP address, whether IPv4 or IPv6, it is advisable to limit this listening only to the necessary ones. By default, it comes as ALL, so the service will open on all available interfaces of the system.
ListenAddress 192.168.1.xx
The system administrator should not authenticate directly against the machine. It is recommended to modify this parameter to set it to "no"
PermitRootLogin no
AllowUsers enables which users are allowed to use the SSH service. You can list several users. By default, it allows all system users to log in via SSH, but it is recommended to limit access only to those who really need to use this service.
AllowUsers user1 user2
Like with AllowUsers, AllowGroups allows specifying the group or groups of users allowed to access the system via SSH.
All password-based logins should be disabled. Only allow logins based on public keys.
AuthenticationMethods publickey
PubkeyAuthentication yes
In some old versions of SSHD, it would be like this:
PubkeyAuthentication yes
We must reject remote logins from accounts with empty passwords.
PermitEmptyPasswords no
The MaxAuthTries parameter specifies the maximum number of failed authentication attempts per connection. We recommend setting it to a value like 3.
MaxAuthTries 3
This directive specifies how many seconds a user is allowed to remain with an open connection without having authenticated correctly. We recommend this value to be, for example, 60.
LoginGraceTime 60
With this directive, MaxStartups specifies the maximum allowed number of simultaneous unauthenticated connections. All subsequent connection attempts will be denied until one of the previous ones completes the entire process successfully.
This way, we can deny possible brute-force attacks. By default, it comes as 10, and we recommend setting a lower value, for example, "3".
MaxStartups 3
Specifies the file that will be shown before a user authenticates on the system.
Banner /etc/issue
An inactive time interval can be set for SSH sessions. This way, if a user leaves the session unattended, it will automatically close after the specified time.
ClientAliveInterval 300
ClientAliveCountMax 0
SSH can emulate the behavior of the obsolete RSH command; simply disable insecure access via RSH.
IgnoreRhosts yes
It is advisable to disable this option since we initially defined that authentication would be by public key.
HostbasedAuthentication no
X11 is the graphical server used by almost all Linux distributions. This server allows, among other things, forwarding through SSH. This means that it is possible to run graphical applications from a remote machine by exporting the display to our local desktop. In other words, the application runs on the remote server, but we view the graphical interface on our local desktop.
So, if you don't need to use this graphical server, it's better not to have it enabled.
X11forwarding no
By default, users can navigate through the server's directories, such as /etc /, /bin, among others. SSH can be protected by using Chroot to lock users into their personal directories.
We will show this configuration in a new article soon.
To check the configuration file and detect any errors before restarting SSHD, run:
sshd -t
Finally, it is important to consider that access to this service can be limited only to the organization's LAN or through a VPN for cases requiring external access.
Jorge García
Fullstack developer