This is part of a series on hosting a website on the Raspberry Pi. Click the Raspberry Pi Webserver in the Blog Series to the right for the complete steps.

Start MobaXterm and connect to the Raspberry Pi.

Always make a backup of your config files before making changes to them.

Let’s start with adding a ssh-users group, and only users in this group will be able to login.

sudo groupadd ssh-users
sudo usermod -aG ssh-users user

Moving to the config file location for OpenSSH, make a copy, and begin editing it.

cd /etc/ssh
sudo cp sshd_config sshd_config_orig
sudo nano sshd_config

I began by commenting out all active lines in the config file. Then copying the list (below table) and pasting it into the config file. All other entries are set to default by OpenSSH.

Include /etc/ssh/sshd_config.d/*.confAdd the other config files.
AcceptEnv LANG LC_*Allow client to pass locale environment variables.
AddressFamily inetUse IPv4 only.
AllowTcpForwarding noNo TCP forwarding.
AllowGroups ssh-usersThe users from this group can log on.
AuthenticationMethods publickeyOnly allow publickey logins.
ClientAliveCountMax 2After 2 ClientAliveInterval(below) of 15 seconds with no response, it will terminate the connection
ClientAliveInterval 15After a timeout interval of 15 seconds, if no data has been received from the client, it requests a response.
DenyUsers rootRoot is not allowed to login.
KbdInteractiveAuthentication no No keyboard-interactive authentication.
LoginGraceTime 1515 seconds to login.
LogLevel VERBOSEPossible values are QUIET, FATAL, ERROR, INFO, VERBOSE, DEBUG, DEBUG1, DEBUG2, and DEBUG3.
MaxAuthTries 1Only give one try to login.
PasswordAuthentication noNo password authentication
PermitRootLogin noDo not allow the root to login.
Port 2222Change the port to help stop some of the bots that just attack port 22. You can pick any port number that is not in use.
PrintMotd noDon’t print file /etc/motd after user logs in interactively.
Subsystem sftp /usr/lib/openssh/sftp-serverOverride default of no subsystems.
UsePAM noNo PAM accounts
Include /etc/ssh/sshd_config.d/*.conf
AcceptEnv LANG LC_*
AddressFamily inet
AllowTcpForwarding no
AllowGroups ssh-users
AuthenticationMethods publickey
ClientAliveCountMax 2
ClientAliveInterval 15
DenyUsers root
KbdInteractiveAuthentication no
LoginGraceTime 15
LogLevel VERBOSE
MaxAuthTries 1
PasswordAuthentication no
PermitRootLogin no
Port 2222
PrintMotd no
Subsystem sftp /usr/lib/openssh/sftp-server
UsePAM  no

After pasting the contents and making any changes to fine tune it, close the file and restart the SSH service using the command below.

sudo systemctl restart ssh

In MobaXterm, right click your session and select Edit session. In the popup window, on the far right change the Port number to the number you selected. With the example above the Port number is 2222.

Click OK to close the window. Double clicking your session will open a new tab and connect to the Raspberry Pi using the new configuration.

With OpenSSH a lot more secure, check our the next post in this series to continue: Using TCP Wrapper to help secure OpenSSH

Share this content: