Linux is renowned for its security, but like any operating system, it requires proactive measures to harden against threats. This tutorial covers essential steps to secure a Linux system, focusing on best practices for Ubuntu/Debian-based distributions (adaptable to others like Fedora or Arch). We’ll assume you’re using a recent version (e.g., Ubuntu 24.04 as of 2025) and have basic command-line familiarity. Always back up data before making changes.
Note: Security is ongoing; regularly update your system and monitor logs. This guide is not exhaustive—consult official docs for your distro.
1. Update and Patch Your System
Outdated software is a primary attack vector. Keep everything current.
- Update package lists and upgrade packages:
sudo apt update && sudo apt upgrade -y
apt update: Refreshes repository metadata.apt upgrade: Installs available updates.- Enable automatic security updates (for unattended patching):
Install the tool:
sudo apt install unattended-upgrades
Configure it:
sudo dpkg-reconfigure unattended-upgrades
Edit /etc/apt/apt.conf.d/50unattended-upgrades to enable security origins:
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}-security";
};
Test with sudo unattended-upgrades --dry-run.
- Schedule regular reboots if kernel updates require it (e.g., via cron job):
sudo crontab -e
# Add: 0 2 * * 0 /sbin/reboot # Weekly reboot at 2 AM Sunday
Run updates weekly or use tools like needrestart to detect reboot needs.
2. User Account Security
Limit privileges and use strong authentication.
- Create a non-root user (if not already):
sudo adduser secureuser
sudo usermod -aG sudo secureuser # Add to sudo group
Log in as this user and use sudo for admin tasks. Avoid direct root login.
- Disable root login:
Edit SSH config (if using remote access):sudo nano /etc/ssh/sshd_config
PermitRootLogin no
Restart SSH: sudo systemctl restart ssh.
- Enforce strong passwords:
Installlibpam-pwquality:
sudo apt install libpam-pwquality
Edit /etc/pam.d/common-password:
password requisite pam_pwquality.so retry=3 minlen=12 dcredit=-1 ucredit=-1 ocredit=-1 lcredit=-1
This requires 12+ characters with digits, uppercase, etc.
- Implement password expiration:
sudo chage -M 90 -W 7 username # Expires every 90 days, warns 7 days prior
- Enable two-factor authentication (2FA) for SSH:
Install Google Authenticator:
sudo apt install libpam-google-authenticator
Run google-authenticator as your user, scan the QR code with an app (e.g., Authy). Edit /etc/pam.d/sshd and /etc/ssh/sshd_config to enable it.
3. Secure SSH Access
SSH is a common entry point—lock it down.
- Change default port (from 22 to e.g., 2222):
In/etc/ssh/sshd_config:
Port 2222
Update firewall (see Section 5) and restart SSH.
- Disable password authentication (use keys):
Generate keys on your client:ssh-keygen -t ed25519.
Copy to server:ssh-copy-id -p 2222 user@server.
Insshd_config:
PasswordAuthentication no
PubkeyAuthentication yes
Restart SSH.
- Limit SSH access:
AllowUsers [email protected]
Use Fail2Ban to ban brute-force attempts:
sudo apt install fail2ban
sudo systemctl enable --now fail2ban
Configure jails in /etc/fail2ban/jail.local for SSH.
4. Firewall Configuration
Block unauthorized traffic with UFW (Uncomplicated Firewall).
- Install and enable UFW:
sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh # Or your custom port: sudo ufw allow 2222/tcp
sudo ufw enable
- Allow specific services (e.g., HTTP/HTTPS):
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
- Check status:
sudo ufw status verbose. - For advanced setups, use
nftablesorfirewalld(on RHEL-based distros).
5. Encrypt Your Disks
Protect data at rest with full-disk encryption.
- During installation: Use LUKS in the installer (e.g., Ubuntu’s guided option).
- For existing systems (backup first!):
Usecryptsetup:
sudo apt install cryptsetup
sudo cryptsetup luksFormat /dev/sdaX # Replace with your partition
sudo cryptsetup luksOpen /dev/sdaX encrypted
sudo mkfs.ext4 /dev/mapper/encrypted
Update /etc/fstab and /etc/crypttab for boot-time unlocking. This is advanced—consider tools like ecryptfs for home directories.
- Enable file encryption for sensitive dirs:
sudo apt install ecryptfs-utils
ecryptfs-migrate-home -u username
6. Install and Configure Antivirus/Security Tools
Linux malware exists—scan proactively.
- Install ClamAV (open-source AV):
sudo apt install clamav clamav-daemon
sudo freshclam # Update definitions
sudo clamscan -r /home # Scan home dir
Automate with cron: sudo crontab -e and add 0 2 * * 0 freshclam && clamscan -r / --bell -i.
- Use Lynis for auditing:
sudo apt install lynis
sudo lynis audit system
Follow its suggestions (e.g., harden kernel params).
- AppArmor/SELinux: Enable mandatory access controls.
For AppArmor (Ubuntu default):
sudo apt install apparmor apparmor-utils
sudo aa-enforce /etc/apparmor.d/*
For SELinux (Fedora): sudo setenforce 1.
7. Secure Software Management
Avoid untrusted sources.
- Use official repos: Stick to
aptordnf; avoid PPAs unless verified. - Remove unnecessary packages:
sudo apt autoremove
sudo apt autoclean
- Sandbox apps with Flatpak/Snap:
sudo apt install flatpak
flatpak install flathub org.libreoffice.LibreOffice # Example
- Kernel hardening: Edit
/etc/sysctl.conf:
kernel.kptr_restrict = 2
net.ipv4.conf.all.rp_filter = 1
Apply: sudo sysctl -p.
8. Monitoring and Logging
Detect issues early.
- Install auditd:
sudo apt install auditd audispd-plugins
sudo systemctl enable --now auditd
Watch logs: sudo tail -f /var/log/auth.log.
- Use Logwatch for summaries:
sudo apt install logwatch
sudo logwatch --detail High --mailto [email protected] --service all
- Intrusion detection: Consider OSSEC or AIDE for file integrity.
9. Network and Wireless Security
- Secure Wi-Fi: Use WPA3; avoid WEP/WPA2.
- VPN for public networks: Install OpenVPN or WireGuard.
sudo apt install wireguard
# Generate keys and config as per docs.
- Disable unused services:
sudo systemctl disable --now cups(if no printing).
10. Best Practices and Maintenance
| Practice | Command/Action | Why? |
|---|---|---|
| Use strong, unique passwords | passwd with pwquality | Prevents cracking. |
| Enable automatic backups | rsync or Deja Dup | Data recovery. |
| Regular audits | lynis audit system | Identifies weaknesses. |
| Multi-factor everywhere | Google Authenticator | Adds auth layer. |
| Keep minimal install | sudo apt purge unnecessary-package | Reduces attack surface. |
- Test your setup: Use tools like
nmapfrom another machine:nmap -p- your.ip. - Stay informed: Follow distro security mailing lists (e.g., ubuntu-security-announce).
- Physical security: Lock your machine; use BIOS passwords.
For distro-specific advice, check official docs (e.g., Ubuntu Security Guide). If you’re on a server, consider CIS benchmarks. Security evolves—revisit this monthly. If issues arise, seek help on forums like Ask Ubuntu. Stay secure!



