Security
Linux Security is a critical aspect of maintaining a secure and stable server or personal environment. This page provides a comprehensive guide on key security practices to harden a Linux system, reduce vulnerabilities and protect against common threats. By following these best practices, you can ensure that your system is well-defended from unauthorized access, malware and potential exploits.
unattended-upgrades
Ensure your Linux system is automatically updated with security patches. Unattended Upgrades enables automatic installation of critical updates without manual intervention, ensuring your system stays secure against newly discovered vulnerabilities.
-
Install
Terminal window sudo apt install unattended-upgrades -
Open config
Terminal window sudo nano /etc/apt/apt.conf.d/02periodic -
Modify config
/etc/apt/apt.conf.d/02periodic # Update these linesAPT::Periodic::Enable "1";APT::Periodic::Update-Package-Lists "1";APT::Periodic::Download-Upgradeable-Packages "1";APT::Periodic::Unattended-Upgrade "1";APT::Periodic::AutocleanInterval "1";APT::Periodic::Verbose "2"; -
Verify
Terminal window sudo unattended-upgrades -d
Require password for sudo
Require users to authenticate with a password before using sudo privileges. This simple change adds an extra layer of protection by preventing unauthorized users or scripts from executing commands with elevated privileges.
-
Open config
Terminal window sudo nano /etc/sudoers.d/010_pi-nopasswd -
Modify config
/etc/sudoers.d/010_pi-nopasswd # Update this line# pi ALL=(ALL) NOPASSWD: ALLpi ALL=(ALL) PASSWD: ALL
Use ssh keys for authentication
Switch from password-based authentication to SSH key-based authentication. This method is more secure because it eliminates the risk of brute-force attacks and provides stronger encryption for remote logins.
-
Create key (client)
Terminal window ssh-keygen -t rsa -
Copy ket to host (client)
Terminal window scp ~/.ssh/id_rsa.pub {{{USERNAME_VAR}}}@{{{IP_ADDRESS_VAR}}}:~/.ssh/id_rsa.pub -
Add key to authorized keys (host)
Terminal window cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
sshd_config
The sshd_config file is a key part of securing SSH. By adjusting its settings, you can control login methods, restrict access and enhance overall SSH security, such as disabling root login and limiting authentication methods.
-
Open config
Terminal window sudo nano /etc/ssh/sshd_config -
Modify config
/etc/ssh/sshd_config # Disable password auth#PasswordAuthentication yesPasswordAuthentication no# Uncomment below to prevent root login#PermitRootLogin prohibit-passwordPermitRootLogin prohibit-password# Uncomment and change port (optional)#Port 22Port 2222 -
Restart ssh
Terminal window sudo service ssh restart
fail2ban
Install and configure Fail2Ban to protect your server from brute-force attacks. Fail2Ban monitors log files for suspicious activity and blocks IPs that repeatedly fail authentication, preventing attackers from gaining access.
-
Install fail2ban
Terminal window sudo apt install fail2ban -
Open config
Terminal window sudo nano /etc/fail2ban/jail.conf -
Modify config (optional)
Terminal window enabled = true# This must change if using a custom ssh portport = sshfilter = sshd# Number of seconds that a host is bannedbantime = 600# A host is banned if it has generated maxretry during the last findtimefindtime = 600# How many attempts until host is bannedmaxretry = 3 -
Restart fail2ban service
Terminal window sudo service fail2ban restart
ufw firewall
A firewall is your first line of defense against unauthorized access. Installing and configuring a firewall like UFW or iptables ensures that only trusted traffic is allowed to reach your system, reducing exposure to external threats.
-
Install
Terminal window sudo apt install ufw -
Allow ssh
Terminal window sudo ufw allow ssh -
Deny incoming (optional)
Terminal window sudo ufw default deny incoming -
Allow outgoing (optional)
Terminal window sudo ufw default allow outgoing -
Add custom rule (optional)
Terminal window sudo ufw allow from {{{IP_ADDRESS_VAR}}} port $PORT_NUMBERTerminal window # ALLOW IN: 22/tcp (OpenSSH) and 22/tcp (OpenSSH (v6))sudo ufw allow OpenSSH -
Enable ufw
Terminal window sudo ufw enable -
Reload ufw
Terminal window sudo ufw reload -
Check status
Terminal window sudo ufw status verbose