Linux Firewall
Linux Firewall
firewall)
This article covers basic Linux firewall management, with specific
reference to the information needed for the RHCSA
EX200 certification exam. Extra information is required for the RHCE
EX300 certification exam, which will be supplied by another article.
Installation
system-config-firewall
system-config-firewall-tui
iptables
Quick Database Setup
Related articles.
Installation
Most installations will include the firewall functionality. If you need to
manually install it, the following commands will install the IP4 and IP6
firewall functionality. In this article we will only consider the IP4
settings.
system-config-firewall
The GUI screen to control the firewall is available from the menu
(System > Administration > Firewall) or can be started from the
command line using the system-config-firewallcommand. If it is not
already present, it can be installed using the following command.
The "Other Ports" section allows you to open ports that are not
covered in the "Trusted Services" section.
system-config-firewall-tui
The TUI utility is similar to the GUI utility shown above, but it feels
incredibly clumsy in comparison. If it is not already present, it can be
installed using the following command.
To alter the Trusted Services, tab to the "Customize" button and press
the return key. Amend the list using the arrow and space keys.
You can close out of the customization section at any point. The other
sections of the GUI tool are available by clicking the "Forward" button
on each successive screen.
iptables
In addition to the GUI and TUI interfaces, the firewall rules can be
amended directly using the iptables command. There are vast number
of parameters, so I will just focus on the elements necessary for the
RHCSA exam.
Each chain can contain multiple explicit rules that are checked in
order. If a rule matches, the associated action (ACCEPT and DROP being
the most common) is taken. If no specific rule is found, the default
policy is used to determine the action to take.
Since the default policy is a catch-all, one of two basic methods can
be chosen for each chain.
Set the default policy to ACCEPT and explicitly DROP things you
don't want.
Set the default policy to DROP and explicitly ACCEPT things you do
want.
# iptables -L -v --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source
destination
1 11 812 ACCEPT all -- any any anywhere
anywhere state RELATED,ESTABLISHED
2 0 0 ACCEPT icmp -- any any anywhere
anywhere
3 0 0 ACCEPT all -- lo any anywhere
anywhere
4 1 100 ACCEPT tcp -- any any anywhere
anywhere state NEW tcp dpt:ssh
5 0 0 REJECT all -- any any anywhere
anywhere reject-with icmp-host-prohibited
The default policy for a chain is set using the "-P" flag. In the following
example, assuming no specific rules were present, all communication
to and from the server would be prevented.
The next thing we want to do if flush any existing rules, leaving just
the default policies. This is done using the "-F" flag.
# iptables -F
Now we need to define specific rules for the type of access we want
the server to have. Focusing on the INPUT chain, we can grant access
to packets in a number of ways.
Once the explicit rules are defined, we need to set the real default
policies.
If you are using Fedora, you may need to use the following command
instead.
#!/bin/bash
# Set the default policies to allow everything while we set up new rules.
# Prevents cutting yourself off when running from remote SSH.
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
# Reset the default policies to stop all incoming and forward requests.
iptables -P INPUT DROP
iptables -P FORWARD DROP
# Accept any outbound requests from this server.
iptables -P OUTPUT ACCEPT
# /root/firewall.sh
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
Chain INPUT (policy DROP 4457 packets, 522K bytes)
num pkts bytes target prot opt in out source
destination
1 0 0 ACCEPT tcp -- any any anywhere
anywhere tcp dpt:ftp
2 1394 116K ACCEPT tcp -- any any anywhere
anywhere tcp dpt:ssh
3 0 0 ACCEPT tcp -- any any anywhere
anywhere tcp dpt:http
4 8 400 ACCEPT all -- lo any anywhere
anywhere
5 96358 138M ACCEPT all -- any any anywhere
anywhere state RELATED,ESTABLISHED