Download ResumeAboutProjectsSkillsRecent ArticlesContactBlogs

Securing My Linux Server: A Complete Setup Guide

2025-06-24

linuxtechdev-ops

Securing My Server

1. Creating a New User with Admin Privilege for My VM

2. Updating and Upgrading the System

Problem 1: grub-install: error: cannot find EFI directory. Encountered while updating and upgrading Linux

3. SSH Setup

Host hostinger
    HostName <hostname>
    User <username>
    Port <portnumber>
    IdentityFile <private-key-path>

GitHub Setup

4. Setting Up Firewall

# Switch to root.
sudo su -

# Install iptables-persistent, this will maintain the state of iptables and replace ufw. It automatically removes ufw
apt install iptables-persistent     

# Check status of services to confirm everything is going well using:
systemctl status -l ufw

# netfilter-persistent is the service. NOTE: service and package name can be different. The package name of netfilter-persistent service is iptables.
systemctl status -l netfilter-persistent

# This shows your current iptables firewall rule:
iptables -L -vn --line-numbers
# This is a very interesting rule. So this makes your firewall dynamic.
# -A INPUT: This appends the rule to the INPUT chain, which handles incoming packets.
# -m state: This uses the state module, which allows matching packets based on their connection state.
# --state ESTABLISHED: This matches packets that are part of an existing connection. For example, if you initiate a connection from your machine to a web server, the response packets from the web server are considered ESTABLISHED.
# --state RELATED: This matches packets that are not part of an existing connection but are related to an existing connection. For example, an FTP data transfer connection is related to the initial FTP control connection.
# -j ACCEPT: This specifies the target action for matching packets, which in this case is to accept them.
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# The loopback interface is critical for internal communication within the host. Many applications and services rely on the loopback interface to function correctly. Like localhost.
iptables -A INPUT -p all -i lo -j ACCEPT    # default of -p is all.

# Enables ssh connection over TCP protocol through port 22. If I don't set this, the connection will break when I set the drop policy.
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# Allows DNS resolution via both UDP and TCP.
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT

# Will be changing the default port 22.
sudo iptables -A INPUT -p tcp --dport <new-ssh-port> -j ACCEPT

# Sets default policy for FORWARD and INPUT CHAIN to DROP.
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -nvL --line

# Saves the iptables rules.
iptables-save > /etc/iptables/rules.v4
# Alternative. This also prints on the console.
iptables-save | tee /etc/iptables/rules.v4
exit

5. Securing the SSH Connection

6. Setting Up Root Access Using Keys

sudo vim /etc/ssh/sshd_config
sudo systemctl daemon-reload; sudo systemctl restart ssh;
cp -r /home/{home_user_name}/.ssh /root/

Extra Notes: Netfilter

References