- Edit SSH configuration:
Change the Port:
vi /etc/ssh/sshd_config
Port 2120
- Restart SSH service:
systemctl daemon-reload systemctl restart ssh
- SSH with the new port:
ssh -p 2120 root@SERVER_IP
-
Add a new user:
adduser ehsan
- Use a secure password
-
Add user to sudo group:
usermod -aG sudo ehsan
-
Log in with new user credentials:
ssh -p 2120 ehsan@SERVER_IP
-
For switching to sudo:
sudo su
-
Disable root login: Edit
/etc/ssh/sshd_config
, set:PermitRootLogin no
- Copy your SSH public key to the server:
ssh-copy-id -p 2120 ehsan@SERVER_IP
- Edit SSH configuration:
- Set
PasswordAuthentication
tono
in/etc/ssh/sshd_config
.
- Set
- Find any overrides:
grep -i passwordauthentication /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*
-
Update package lists and install Google Authenticator:
sudo apt update sudo apt-get install libpam-google-authenticator
-
Configure Google Authenticator: Make sure to run with the user you want to use with SSH, i.e.
ehsan
google-authenticator
- Answer prompts:
y,y,n,y
- Answer prompts:
-
Edit PAM configuration:
- In
/etc/pam.d/sshd
, add:
auth required pam_google_authenticator.so nullok auth required pam_permit.so
and Comment:
#@include common-auth
- In
-
Edit SSH configuration for 2FA:
vi /etc/ssh/sshd_config
KbdInteractiveAuthentication yes AuthenticationMethods publickey,keyboard-interactive
- Install Fail2ban:
sudo apt update sudo apt install fail2ban
- Create a local Fail2ban SSH configuration:
vi /etc/fail2ban/jail.d/sshd.local
- Add the following configuration:
[sshd] enabled = true port = 2120 filter = sshd logpath = /var/log/auth.log maxretry = 5 bantime = 3600 findtime = 600
- Restart Fail2ban service:
systemctl restart fail2ban
- Check Fail2ban status for SSH:
fail2ban-client status sshd