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.

In this article
  1. Prerequisites
  2. Installation and Initial Setup
  3. Server Security
  4. Monitoring and Maintenance

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

  1. Dedicated server or VPS with root access
  2. Stable internet connection
  3. Basic knowledge of Linux commands

Installation and Initial Setup

System Update

Bash
# 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

  1. Create new user (replace yourusername with desired username)

    Bash
    # Create new user
    sudo adduser yourusername
    
    # Add user to sudo group
    sudo usermod -aG sudo yourusername
  2. 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

  1. Configure Firewall (UFW)

    Bash
    # Allow SSH service
    sudo ufw allow OpenSSH
    
    # Enable firewall
    sudo ufw enable
    
    # Show applied rules
    sudo ufw status verbose
  2. 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

Bash
# Install monitoring tools
sudo apt install -y htop nmon

# View active processes
htop

# Monitor system performance
nmon