Basic Firewall Setup on Dedicated Linux Server

A firewall will stop any unusual activities on one network from being passed on to another network. In most systems the Linux kernel is compiled with IP forwarding set to yes. This means is that if the computer has more than one network connected to it then network information will be passed directly from one network to the other as if it was physically connected.

Forgetting to secure and configure a dedicated server firewall is a common mistake and a huge security flaw. Going into the firewall’s configuration allows you to remove unnecessary software that’s connected to the internet. This makes your server and its ports vulnerable to intrusion.

Now, we can see how to setup basic firewall on dedicated linux server.

Installing Firewall on Linux

On Ubuntu/Debian:

$ sudo apt-get install iptables

On CentOS 7/RHEL 7:

# yum install iptables-services

Let’s decide which Firewall Ports to Block

The first step in firewall installation is deciding which ports has to be left open on our dedicated server. This will vary based on what you are using the dedicated host for, if you are running a web server, you would likely want the following ports open:

  • Web: 80 and 443
  • SSH: Typically run on port 22
  • Email: 110 (POP3), 143 (IMAP), 993 (IMAP SSL), 995 (POP3 SSL).

Change your SSH port to a non-default port by reading our article on changing your servers SSH port. Want your users to only use email over SSL? Block standard POP3 and IMAP ports in your firewall to force SSL use.

Flush Default Firewall Rules

Run the below command to flush to default firewall rules.

# iptables -F

Block Common Server Attack Routes

We have to run some standard commands here to block common attacks.

Block syn-flood packets:

# iptables -A INPUT -p tcp ! –syn -m state –state NEW -j DROP

Block XMAS Packets:

# iptables -A INPUT -p tcp –tcp-flags ALL ALL -j DROP

Block null packets:

# iptables -A INPUT -p tcp –tcp-flags ALL NONE -j DROP

Opening Required Ports

Run the below command to open up the ports you need. Here are some examples for you to work,
Allow SSH Access:

# iptables -A INPUT -p tcp -m tcp –dport 22 -j ACCEPT

Open up LOCALHOST access:

# iptables -A INPUT -i lo -j ACCEPT

Allow web traffic:

# iptables -A INPUT -p tcp -m tcp –dport 80 -j ACCEPT
# iptables -A INPUT -p tcp -m tcp –dport 443 -j ACCEPT

Test Firewall Configuration

Run the following command to save the configuration and restart your firewall:

# iptables -L -n
# iptables-save | sudo tee /etc/sysconfig/iptables
# service iptables restart

The above steps will definitely provide you the setup of Firewall in your server.For more information related to IP address or how to block particular IP address click here.