This document is meant to serve as a basic guide for hardening a Linux server.
sudo nano /etc/ssh/sshd_config
- Uncomment
#Port 22
and change it toPort <SSH_PORT>
. (Replace<SSH_PORT>
with your desired port to use for SSH connectivity.) sudo systemctl restart ssh
reboot
sudo ufw default deny incoming
sudo ufw default allow outgoing
- Allow inbound traffic on specific ports:
sudo ufw allow <SSH_PORT>/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status numbered
sudo ufw reload
sudo adduser --disabled-password <USERNAME>
sudo usermod -aG sudo <USERNAME>
mkdir -p /home/<USERNAME>/.ssh
chmod 700 /home/<USERNAME>/.ssh
cp ~/.ssh/authorized_keys /home/<USERNAME>/.ssh/
chown <USERNAME>:<USERNAME> /home/<USERNAME>/.ssh/
chown <USERNAME>:<USERNAME> /home/<USERNAME>/.ssh/authorized_keys
chmod 600 /home/<USERNAME>/.ssh/authorized_keys
If you are using your new account for a form of automation, you may need require that it not require a password for running commands that require sudo
.
sudo visudo
- Add
<USERNAME> ALL=(ALL) NOPASSWD: ALL
to the bottom of the file.
-
sudo nano /etc/ssh/sshd_config
- Change
PermitRootLogin yes
toPermitRootLogin no
. - Change
#PubkeyAuthentication yes
toPubkeyAuthentication yes
. - Chage
#PasswordAuthentication yes
toPasswordAuthentication no
. - Change
#PermitEmptyPasswords no
toPermitEmptyPasswords no
. - Change
#StrictModes yes
toStrictModes yes
. - Change
#MaxAuthTries 6
toMaxAuthTries 3
. - Change
#MaxSessions 10
toMaxSessions 3
.
- Change
-
sudo systemctl restart ssh
-
reboot
If your newly created account needs to be able to run docker
commands, you will want to create a docker
UserGroup and adde your user to it.
sudo groupadd docker
sudo usermod -aG docker <USERNAME>