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
Section titled “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
Section titled “Install”Terminal window sudo apt install unattended-upgrades -y -
Open config
Section titled “Open config”Terminal window sudo nano /etc/apt/apt.conf.d/20auto-upgrades -
Modify config
Section titled “Modify config”/etc/apt/apt.conf.d/20auto-upgrades # Update below:APT::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
Section titled “Verify”Terminal window sudo unattended-upgrades -d
Require password for sudo
Section titled “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
Section titled “Open config”Terminal window sudo visudo -f /etc/sudoers.d/010_{{USERNAME_VAR}}-nopasswd -
Modify config
Section titled “Modify config”/etc/sudoers.d/010_{{USERNAME_VAR}}-nopasswd # Comment out line below to require password for sudo (recommended){USERNAME_VAR} ALL=(ALL) NOPASSWD: ALL
fail2ban
Section titled “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
Section titled “Install fail2ban”Terminal window sudo apt install fail2ban -y -
Open config
Section titled “Open config”Terminal window sudo nano /etc/fail2ban/jail.local -
Modify config (optional)
Section titled “Modify config (optional)”/etc/fail2ban/jail.local [DEFAULT]bantime.increment = truebantime.factor = 1bantime.maxtime = 5w[sshd]enabled = trueport = sshfilter = sshdbackend = systemdmaxretry = 3findtime = 600bantime = 3600ignoreip = 127.0.0.1/8 ::1 {{SUBNET_VAR}} -
Restart service
Section titled “Restart service”Terminal window sudo systemctl restart fail2ban -
Check status
Section titled “Check status”Terminal window sudo fail2ban-client status sshd
ufw firewall
Section titled “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
Section titled “Install”Terminal window sudo apt install ufw -y -
Deny incoming
Section titled “Deny incoming”Terminal window sudo ufw default deny incoming -
Allow outgoing
Section titled “Allow outgoing”Terminal window sudo ufw default allow outgoing -
Allow SSH
Section titled “Allow SSH”Replace port number if your SSH port is different.
Terminal window sudo ufw allow 22/tcp -
Tailscale (optional)
Section titled “Tailscale (optional)”
-
Allow routed traffic
Section titled “Allow routed traffic”Required for exit node/subnet routing.
Terminal window sudo ufw route allow in on tailscale0 -
Allow Tailscale network
Section titled “Allow Tailscale network”Allow everything coming in from your Tailscale network.
Terminal window sudo ufw allow in on tailscale0 -
Tailscale performance (optional)
Section titled “Tailscale performance (optional)”Helps Tailscale make direct “peer-to-peer” connections.
Terminal window sudo ufw allow 41641/udp -
Enable packet forwarding
Section titled “Enable packet forwarding”Tell the kernel to allow packets to be routed through the server. Open the UFW sysctl configuration:
Terminal window sudo nano /etc/ufw/sysctl.confUncomment the following line:
/etc/ufw/sysctl.conf net/ipv4/ip_forward=1net/ipv6/conf/default/forwarding=1
-
-
AdGuard Home (optional)
Section titled “AdGuard Home (optional)”Allow local network devices to send DNS queries to AdGuard Home.
Terminal window sudo ufw allow from {{SUBNET_VAR}} to any port 53 -
Samba (optional)
Section titled “Samba (optional)”Allow Samba UDP ports (NetBIOS).
Terminal window sudo ufw allow from {{SUBNET_VAR}} to any port 137,138 proto udpAllow Samba TCP ports (SMB).
Terminal window sudo ufw allow from {{SUBNET_VAR}} to any port 139,445 proto tcp -
Allow web access (optional)
Section titled “Allow web access (optional)”Required for web server (e.g., Nginx Proxy Manager, Caddy).
Terminal window sudo ufw allow 80,443/tcp -
Add custom rule (optional)
Section titled “Add custom rule (optional)”Terminal window sudo ufw allow from {{SERVER_IP_VAR}} port $PORT_NUMBER -
Enable ufw
Section titled “Enable ufw”Terminal window sudo ufw enable -
Reload ufw
Section titled “Reload ufw”Terminal window sudo ufw reload -
Check status
Section titled “Check status”Terminal window sudo ufw status verbose