Setting Up a Secure Linux Server from Scratch
A comprehensive step-by-step guide to setting up a secure Linux server, from basic installation to securing the server against breaches.
- Key idea
- Definition
- When to use
- Caution
In this article
In this comprehensive guide, we'll learn how to set up a secure Linux server step by step, from basic installation to securing the server against breaches.
Prerequisites
- Dedicated server or VPS with root access
- Stable internet connection
- Basic knowledge of Linux commands
Installation and Initial Setup
System Update
# Update package list
sudo apt update
# Upgrade all installed packages
sudo apt upgrade -y
# Install essential packages
sudo apt install -y ufw fail2ban git curl wget
Create New User
-
Create new user (replace
yourusernamewith desired username)Bash # Create new user sudo adduser yourusername # Add user to sudo group sudo usermod -aG sudo yourusername -
Copy SSH keys for new user
Bash # Create .ssh directory sudo mkdir -p /home/yourusername/.ssh # Copy SSH keys sudo cp ~/.ssh/authorized_keys /home/yourusername/.ssh/ # Change directory owner sudo chown -R yourusername:yourusername /home/yourusername/.ssh # Set permissions sudo chmod 700 /home/yourusername/.ssh sudo chmod 600 /home/yourusername/.ssh/authorized_keys
Server Security
-
Configure Firewall (UFW)
Bash # Allow SSH service sudo ufw allow OpenSSH # Enable firewall sudo ufw enable # Show applied rules sudo ufw status verbose -
Secure SSH Service
Bash # Change default SSH port (optional) sudo sed -i 's/#Port 22/Port 2222/g' /etc/ssh/sshd_config # Prevent root login sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config # Reduce login grace time sudo sed -i 's/#LoginGraceTime 2m/LoginGraceTime 1m/g' /etc/ssh/sshd_config # Restart SSH service sudo systemctl restart sshd
Monitoring and Maintenance
# Install monitoring tools
sudo apt install -y htop nmon
# View active processes
htop
# Monitor system performance
nmon