While AWS EC2 instances should be well protected by VPC security tools, you may still need to implement protection at the OS-level, and that means firewalld.
This is the second part of our server security series. In this article, we will look at configuring firewall rules via
firewalld on Red Hat Enterprise Linux. While Amazon Linux is similar to Red Hat Enterprise Linux (RHEL) in many ways, it does not yet support
firewalld. So we’ll have to focus on RHEL. By the way, I will not cover
iptables here, as there are many good resources out there already.
You might wonder why we need to configure the firewall at the operating system level in the first place, if this is already taken care of by EC2 security groups within our VPC. I can think of a few possible use cases. If, for instance, the firewall in the security group is configured incorrectly, the OS-level firewall can act as a backup to protect the instance from possible probes or compromise. Or perhaps the administrator wants to bind additional ports for future services or temporarily block certain ports, but does not have access to the EC2 dashboard. In either case, depending on the security group state, he should be able to do so at the OS-level.
Now that we have justified using
firewalld, let’s learn a bit more about it.
firewalld is a new way to interact with the netfilter subsystem in the Linux kernel. It simplifies the way we manage firewall rules by classifying network traffic into different firewall zones. We could choose to simply use
iptables to configure the firewall rules if we wanted to, but both cannot co-exist with each other.
Getting started with firewalld
Therefore, to get started with
firewalld, we first need to make sure that the
iptables-related services are disabled. By default, Red Hat Enterprise Linux does not have either
firewalld packages installed. So when you try to query for
iptables-services, it will tell you that it is not there.
# rpm -q iptables-services package iptables-services is not installed
However, if it is already installed, we’ll need to disable the service so that it does not interfere with
# rpm -q iptables-services iptables-services-1.4.21-13.el7.x86_64 # systemctl stop iptables.service # systemctl stop ip6tables.service systemctl status iptables.service iptables.service - IPv4 firewall with iptables Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled) Active: inactive (dead) Nov 15 21:26:47 ip-172-30-1-83.ec2.internal systemd: Stopped IPv4 firewall with iptables. # systemctl status ip6tables.service ip6tables.service - IPv6 firewall with ip6tables Loaded: loaded (/usr/lib/systemd/system/ip6tables.service; disabled) Active: inactive (dead) Nov 15 21:26:52 ip-172-30-1-83.ec2.internal systemd: Stopped IPv6 firewall with ip6tables. # systemctl mask iptables.service ln -s '/dev/null' '/etc/systemd/system/iptables.service' # systemctl mask ip6tables.service ln -s '/dev/null' '/etc/systemd/system/ip6tables.service'
Notice that we also disabled the IPv6 version of the
iptables service. People tend to forget about that.
Now we are ready to install and start
# yum install firewalld # systemctl status firewalld.service firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled) Active: inactive (dead) # systemctl start firewalld.service # systemctl status firewalld.service firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled) Active: active (running) since Sun 2015-11-15 21:31:53 EST; 7s ago Main PID: 10942 (firewalld) CGroup: /system.slice/firewalld.service └─10942 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid Nov 15 21:31:52 ip-172-30-1-83.ec2.internal systemd: Starting firewalld - dynamic firewall dae..... Nov 15 21:31:53 ip-172-30-1-83.ec2.internal systemd: Started firewalld - dynamic firewall daemon. Hint: Some lines were ellipsized, use -l to show in full.
firewalld, we need to understand more about how network traffic is classified into different firewall zones. The
firewalld.zones man pages cover this in a very clear and concise manner. I will reproduce the description here verbatim.
drop– Any incoming network packets are dropped, there is no reply. Only outgoing network connections are possible.
block– Any incoming network connections are rejected with an icmp-host-prohibited message for IPv4 and icmp6-adm-prohibited for IPv6. Only network connections initiated within this system are possible.
public– For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
external– For use on external networks with masquerading enabled specifically for routers. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
dmz– For computers in your demilitarised zone that are publicly-accessible with limited access to your internal network. Only selected incoming connections are accepted.
work– For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
home– For use in home areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.
internal– For use in internal networks. You mostly trust the other computers on the networks to not harm your computer. Only selected incoming connections are accepted.
trusted– All network connections are accepted.
Configure firewalld rules
We will configure
firewalld using the
Instead of running
systemctl status firewalld, we can also use the
firewall-cmd with the
--state option to confirm that it has started.
# firewall-cmd --state running
You can list the firewall zones we discussed earlier by using the
# firewall-cmd --get-zones block dmz drop external home internal public trusted work
To view the default zone, it is as simple as specifying the
--get-default-zone option. If the EC2 instance is inside its own VPC subnet, and can only be accessed via a jumpbox, then we may change the default zone to
internal, and add or remove the services that were allowed by default.
# firewall-cmd --get-default-zone public # firewall-cmd --set-default-zone=internal success # firewall-cmd --list-all internal (default) interfaces: sources: services: dhcpv6-client ipp-client mdns samba-client ssh ports: masquerade: no forward-ports: icmp-blocks: rich rules:
firewall-cmd with the
--get-services option lists all the services that can be controlled by
# firewall-cmd --get-services RH-Satellite-6 amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https
If a service is not on the list, we can always define our own service, or add the port to be opened via
firewall-cmd directly. Let’s demonstrate that – and prepare for our next article on SELinux at the same time – by changing the default port number for the OpenSSH service to port 31337. Assuming that we will use
firewalld together with SELinux, we can enable the port by using the
--add-port option. Note that if we want to make our changes permanent, we need to reload the firewall rules immediately.
# firewall-cmd --permanent --add-port=31337/tcp; firewall-cmd --reload success success # firewall-cmd --list-all internal (default) interfaces: sources: services: dhcpv6-client ipp-client mdns samba-client ssh ports: 31337/tcp masquerade: no forward-ports: icmp-blocks: rich rules:
If we want to remove unneeded services such as
ipp-client, etc, we can do so with the
# firewall-cmd --permanent --remove-service=dhcpv6-client --remove-service=ipp-client --remove-service=mdns --remove-service=samba-client; firewall-cmd --reload success success # firewall-cmd --list-all internal (default) interfaces: sources: services: ssh ports: 31337/tcp masquerade: no forward-ports: icmp-blocks: rich rules:
You can take a peek at the
iptables rules that
firewall-cmd magically generated based on the commands we just issued:
# iptables -S | tail -A FWDI_internal -j FWDI_internal_allow -A FWDO_internal -j FWDO_internal_log -A FWDO_internal -j FWDO_internal_deny -A FWDO_internal -j FWDO_internal_allow -A INPUT_ZONES -g IN_internal -A IN_internal -j IN_internal_log -A IN_internal -j IN_internal_deny -A IN_internal -j IN_internal_allow -A IN_internal_allow -p tcp -m tcp --dport 22 -m conntrack --ctstate NEW -j ACCEPT -A IN_internal_allow -p tcp -m tcp --dport 31337 -m conntrack --ctstate NEW -j ACCEPT
If you have managed to successfully follow along so far, great work! You will find that
firewalld is a lot easier to work with than trying to directly configure
In our next article, we will explore setting up SELinux on Amazon Linux, and walking through a simple SELinux example on Red Hat Enterprise Linux. For more on Cloud server security, why not take Cloud Academy’s Linux Security course?
Comments? Please join in!
New Content: Platforms, Programming, and DevOps – Something for Everyone
This month our team of expert certification specialists released three new or updated learning paths, 16 courses, 13 hands-on labs, and four lab challenges! New content on Cloud Academy You can always visit our Content Roadmap to see what’s just released as well as what’s coming soon....
Mastering AWS Organizations Service Control Policies
Service Control Policies (SCPs) are IAM-like policies to manage permissions in AWS Organizations. SCPs restrict the actions allowed for accounts within the organization making each one of them compliant with your guidelines. SCPs are not meant to grant permissions; you should consider ...
New Content: Focus on DevOps and Programming Content this Month
This month our team of expert certification specialists released 12 new or updated learning paths, 15 courses, 25 hands-on labs, and four lab challenges! New content on Cloud Academy You can always visit our Content Roadmap to see what’s just released as well as what’s coming soon. Ja...
New Content: Get Ready for the CISM Cert Exam & Learn About Alibaba, Plus All the AWS, GCP, and Azure Courses You Know You Can Count On
This month our team of intrepid certification specialists released five learning paths, seven courses, 19 hands-on labs, and three lab challenges! One particularly interesting new learning path is Certified Information Security Manager (CISM) Foundations. After completing this learn...
Which Certifications Should I Get?
The old AWS slogan, “Cloud is the new normal” is indeed a reality today. Really, cloud has been the new normal for a while now and getting credentials has become an increasingly effective way to quickly showcase your abilities to recruiters and companies. With all that in mind, the s...
The 12 AWS Certifications: Which is Right for You and Your Team?
As companies increasingly shift workloads to the public cloud, cloud computing has moved from a nice-to-have to a core competency in the enterprise. This shift requires a new set of skills to design, deploy, and manage applications in cloud computing. As the market leader and most ma...
AWS Certified Solutions Architect Associate: A Study Guide
Want to take a really impactful step in your technical career? Explore the AWS Solutions Architect Associate certificate. Its new version (SAA-C02) was released on March 23, 2020. The AWS Solutions Architect - Associate Certification (or Sol Arch Associate for short) offers some ...
New Content: AWS Terraform, Java Programming Lab Challenges, Azure DP-900 & DP-300 Certification Exam Prep, Plus Plenty More Amazon, Google, Microsoft, and Big Data Courses
This month our Content Team continues building the catalog of courses for everyone learning about AWS, GCP, and Microsoft Azure. In addition, this month’s updates include several Java programming lab challenges and a couple of courses on big data. In total, we released five new learning...
Where Should You Be Focusing Your AWS Security Efforts?
Another day, another re:Invent session! This time I listened to Stephen Schmidt’s session, “AWS Security: Where we've been, where we're going.” Amongst covering the highlights of AWS security during 2020, a number of newly added AWS features/services were discussed, including: AWS Audit...
AWS re:Invent: 2020 Keynote Top Highlights and More
We’ve gotten through the first five days of the special all-virtual 2020 edition of AWS re:Invent. It’s always a really exciting time for practitioners in the field to see what features and services AWS has cooked up for the year ahead. This year’s conference is a marathon and not a...
WARNING: Great Cloud Content Ahead
At Cloud Academy, content is at the heart of what we do. We work with the world’s leading cloud and operations teams to develop video courses and learning paths that accelerate teams and drive digital transformation. First and foremost, we listen to our customers’ needs and we stay ahea...
Excelling in AWS, Azure, and Beyond – How Danut Prisacaru Prepares for the Future
Meet Danut Prisacaru. Danut has been a Software Architect for the past 10 years and has been involved in Software Engineering for 30 years. He’s passionate about software and learning, and jokes that coding is basically the only thing he can do well (!). We think his enthusiasm shines t...