Cách sử dụng UFW Firewall trên Debian, Ubuntu và Linux Mint

Trong bài viết này tôi sẽ hướng dẫn bạn cách sử dụng UFW (Uncomplicated FireWall) trên Debian/Ubuntu/Linux Mint với một vài ví dụ thực tế. Quản lý firewall là một kỹ năng cơ bản mà mỗi admin system cần phải biết. Bài viết này nhằm mục đích giúp các bạn bắt đầu với UFW, nhưng không khám phá chi tiết và nội dung của UFW.

UFW là một front-end cho iptables, quản lý một Netfilter firewall một cách dễ dàng, được gọi là “Uncomplicated Firewall”. Nó cung cấp một giao diện dòng lệnh với cấu trúc tương đối đơn giản giống như OpenBSD’s Packet Filter. Nó là một firwall phù hợp cho các máy chủ trên các distro Linux.  UFW được khuyến nghị là front-end iptables trên Debian và thường được cài đặt sẵn trên những distro Linux này. Mặc định, UFW thiết lập những firewall rule cho cả 2 IPv4 và IPv6. Một loại front-end iptables nối tiếng khác là firewalld, có sẵn trên các distro Linux RHEL, CentOS, Fedora, OpenSUSE, etc.

Tắt các dịch vụ Iptables Restore khác

Như bạn có thể biết, các quy tắc tường lửa iptables sẽ bị xóa khi hệ điều hành tắt và bản thân chương trình iptables không khôi phục các quy tắc tường lửa. UFW theo mặc định khôi phục các quy tắc tường lửa sau khi hệ thống khởi động lại. Trước khi sử dụng UFW, điều quan trọng là bạn phải kiểm tra xem có dịch vụ khôi phục iptables nào khác trên hệ thống của mình hay không. Nếu có hai dịch vụ khôi phục, chúng sẽ xung đột với nhau, điều này thường dẫn đến các ứng dụng web không khả dụng sau khi hệ thống khởi động lại.

Nếu bạn đang sử dụng tường lửa iptables trực tiếp và bây giờ bạn muốn chuyển sang UFW, bạn chỉ cần tắt dịch vụ khôi phục iptables của mình. Iptables-persistent là một dịch vụ khôi phục iptables nổi tiếng trên Debian / Ubuntu. Bạn có thể kiểm tra xem nó có đang chạy hay không bằng lệnh sau.

systemctl status iptables-persistent

Nếu nó đang chạy, bạn có thể stop và disabled nó.

sudo systemctl stop iptables-persistent

sudo systemctl disable iptables-persistent

Hoặc bạn có thể remove nó khỏi hệ thống.

sudo apt remove iptables-persistent

Nếu hệ thống của bạn đang sử dụng iRedMail sử dụng lệnh bên dưới để kiểm tra iptables có đang chạy hay không.

systemctl status iptables

Dịch vụ iptables này chạy cùng với iRedmail để phục hồi những rules sau khi khởi động. Nếu nó đang chạy, bạn có thể stop và disabled nó.

sudo systemctl stop iptables

sudo systemctl disable iptables

Bây giờ tôi sẽ hướng dẫn cách sử dụng UFW.

Bắt đầu UFW trên Debian/Ubuntu/Linux Mint Server

UFW thường được cài đặt sẵn trên Debian/Ubuntu/Linux Mint, bạn vẫn có thể sử dụng lệnh bên dưới để cài đặt.

sudo apt install ufw

Chú ý: Bạn có thể cài UFW trên Arch Linux với lệnhsudo pacman -S ufw, nhưng bạn cũng cần enable UFW bằng lệnh sudo systemctl enable ufw.

Trong quá trình cài đặt UFW disabled. Bạn có thể kiểm tra trạng thái của nó bằng lệnh:

sudo ufw status

Mặc định, incoming policy là deny trên UFW và mặc định forward policy là deny, outgoing policy là allow, với những stateful tracking cho những kết nối từ ngoài vào hệ thống và chuyển hướng kết nối. Trước khi enable UFW, bạn cần phải biết port nào đang mở trên địa chỉ IP public của máy chủ, you need to know what ports are opened on the public IP address of your server, bạn có thể dùng nmap để scan hệ thống.

Việc cài đặt nmap trên Debian/Ubuntu/Linux Mint và scan những port mở trên địa chỉ IP public.

sudo apt install nmap

sudo nmap 12.34.56.78

Thay thế địa chỉ IP 12.34.56.78 với địa chỉ IP thực trên máy chủ của bạn. Bạn có thể thấy một số port được mở như hình bên dưới.

Ngoài ra cũng có những port mở nhưng chỉ lắng nghe trên localhost, nếu bạn muốn biết thì có thể sử dụng lệnh sudo nmap localhost, nhưng bạn cũng không cần lo lắng quá về những port mở trên localhost.

Mặc định, Nmap chỉ có thể scan TCP ports. Chúng ta có thể sử dụng lệnh bên dưới để scan UDP port.

sudo nmap -sU 12.34.56.78

Tuy nhiên, việc scan UDP port hiển thị kết quả chậm. Bạn cũng có thể sử dụng lệnh netstat để biết port UDP đang mở.

sudo netstat -lnpu

Sau khi biết được port TCP và UDP đang mở, bạn cần quyết định port nào nên cho phép từ ngoài kết nối vào hệ thống bằng port đó. Nếu openSSH đang chạy, bạn luôn luôn cho phép TCP port 22 trước khi kích hoạt UFW bằng lệnh bên dưới.

sudo ufw allow 22/tcp

or

sudo ufw allow ssh

You probably want to allow HTTP and HTTPS traffic, so run the following command to allow inbound connection on TCP port 80 and 443.

sudo ufw allow 80/tcp

sudo ufw allow 443/tcp

If you run an email server, you need to allow TCP port 25 (SMTP), 587(submission), 143(imap) and 993 (imaps).

sudo ufw allow 25/tcp

sudo ufw allow 587/tcp

sudo ufw allow 143/tcp

sudo ufw allow 993/tcp

If you want your user to be able to use POP3 protocol, you need to allow TCP port 110 (POP3) and 995 (POP3S).

sudo ufw allow 110/tcp

sudo ufw allow 995/tcp

If you run a BIND DNS server, then you need to open TCP and UDP port 53.

sudo ufw allow 53

The above command will allow both the TCP and UDP port. If you want to allow the UDP port only, then

sudo ufw allow 53/udp

Opening Multiple Ports at Once

You can allow multiple ports like below.

sudo ufw allow 80,443,25,587,465,143,993/tcp

Enabling UFW

After you have set allowed ports in UFW, you need to enable UFW. But before doing that, it’s recommended to enable logging with the following command so that you can better understand if your firewall is working correctly.

sudo ufw logging on

The default log level is ‘low’. The log file is /var/log/ufw.log. I usually use the “medium” log level.

sudo ufw logging medium

Now enable UFW.

sudo ufw enable

Note: If you have previously used iptables firewall directly, those iptables firewall rules will be undone once UFW is enabled.

Check status

sudo ufw status

To show more information, run

sudo ufw status verbose

Now you can re-scan your server to find out which ports are still opened.

sudo nmap 12.34.56.78

How to Delete A Firewall Rule

First, you need to get the reference number of the firewall rule you want to delete with the following command.

sudo ufw status numbered

Then you can delete a rule, for example, the 8th rule.

sudo ufw delete 8

Note that the reference number will change after you delete a rule, so you need to run sudo ufw status numbered again to delete another rule.

Reset UFW

If you made a mistake, you can disable and reset firewall to installation defaults.

sudo ufw reset

This is particularly useful for beginners.

UFW Application Profile

Many server programs ship with UFW profiles. You can list all application profiles with:

sudo ufw app list

We can show information on a specific application profile, for example, the “Apache Full” profile.

sudo ufw app info "Apache Full"

We can see that the port used by this profile are TCP port 80 and 443. If we enable this application profile with the following command, TCP port 80 and 443 will be allowed.

sudo ufw allow "Apache Full"

Creating IP Address Blacklist with UFW

Let’s say there’s a spammer who is constantly trying to send spam to your mail server. You can use UFW to block the spammer’s IP address from accessing TCP port 25 of your mail server, with the following command. Replace 12.34.56.78 with the spammer’s IP address.

sudo ufw insert 1 deny in from 12.34.56.78 to any port 25 proto tcp

Note that newly added firewall rules are put in the bottom by default. If you previously allowed access to port 25 from anywhere, then you need to insert the deny rule as the first rule, just like above, so the deny rule will be applied first. You can always insert new deny rule as the first rule.

sudo ufw insert 1 deny in from 78.56.34.12 to any port 25 proto tcp

You can also block an IP address range like below.

sudo ufw insert 1 deny in from 192.168.0.0/24 to any port 25 proto tcp

To block an IP address from accessing all ports on your server, run

sudo ufw insert 1 deny in from 12.34.56.78

Creating IP Address Whitelist with UFW

Now let’s say you run a OpenSSH server and you only want to allow certain IP address to log in to your server through SSH. You can use UFW to create an IP address whitelist. For example, I don’t have static IP address in my home, but I have set up several VPN servers on the cloud, so now I can configure UFW to allow inbound connection to port 22 from the IP address of my VPN server only.

First, add the IP address to the allow list.

sudo ufw insert 1 allow in from 12.34.56.78 to any port 22 proto tcp

Then you need to get the reference number of the allow SSH from anywhere rule and delete that rule.

sudo ufw status numbered

sudo ufw delete reference-number

Note that you need to delete both the IPv4 and IPv6 rule. Also notice that if you delete the upper rule first, the reference number of the lower rule will change.

From here on out, only your IP address can access TCP port 22.

How to Use IPv6 Address in UFW

First, make sure the IPV6=yes is set in /etc/default/ufw file. If it’s not set, then add it in that file and restart UFW (sudo systemctl restart ufw).

Then you can just replace IPv4 with IPv6 address in ufw commands like below.

sudo ufw allow in from 2607:f8b0:4005:804::200e to any port 587

Note that you can’t insert an IPv6 rule between IPv4 rules. IPv6 rules should always be placed after IPv4 rules.

How to Set Up IP Masquerading with UFW

Sometimes you want to set up your own VPN server, then you will need to set up IP masquerading on your VPN server so that it becomes a virtual router for VPN clients. Unfortunately, UFW doesn’t provide a convenient way to do this. We have to add iptables command in a UFW configuration file.

sudo nano /etc/ufw/before.rules

By default, there are some rules for the filter table. Add the following lines at the end of this file.

# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]
# Forward traffic through eth0 - Change eth0 to match your network interface
-A POSTROUTING -o eth0 -j MASQUERADE

# End each table with the 'COMMIT' line or these rules won't be processed
COMMIT

Notice that if your main network interface isn’t eth0, you need to change eth0 to your network interface name, which can be obtained with the ip addr command. And because we are adding firewall rules to a new table, namely the nat table, we need to add the ‘COMMIT’ line.

By default, UFW forbids packet forwarding, which is great because it prevents random people on the Internet to use your box to do malicious stuff. But we need to allow forwarding for our private network. Find the ufw-before-forward chain in this file and add the following 3 lines, which will accept packet forwarding if the source IP or destination IP is in the 10.10.10.0/24 range.

# allow forwarding for trusted network
-A ufw-before-forward -s 10.10.10.0/24 -j ACCEPT
-A ufw-before-forward -d 10.10.10.0/24 -j ACCEPT

Save and close the file. Then restart UFW.

sudo ufw disable

sudo ufw enable

or simply

sudo systemctl restart ufw

Although this is not related to UFW configuration, but in order to route packets, you also need to set up IP forwarding. This can be done by setting the following at the end of /etc/sysctl.conf file.

net.ipv4.ip_forward = 1

Then apply the changes with the following command.

sudo sysctl -p

How to Set Up Port Forwarding in UFW

What if you use UFW on your router, and you want to route packets such as HTTP requests to internal LAN hosts? In this case, you need to set up port forwarding. Edit the /etc/ufw/before.rules file.

sudo nano /etc/ufw/before.rules

Then add the following lines in the NAT table, above the COMMIT line. Replace 12.34.56.78 with your router’s public IP address.

:PREROUTING ACCEPT [0:0]
# forward 12.34.56.78  port 80 to 192.168.1.100:80
# forward 12.34.56.78  port 443 to 192.168.1.100:443
-A PREROUTING -i eth0 -d 12.34.56.78  -p tcp --dport 80 -j DNAT --to-destination 192.168.1.100:80
-A PREROUTING -i eth0 -d 12.34.56.78  -p tcp --dport 443 -j DNAT --to-destination 192.168.1.100:443

This tells UFW to use the DNAT target to forward HTTP and HTTPS requests to the 192.168.100 host in the local network.

Hint: DNAT (Destination NAT) changes the destination IP address. A typical example is port forwarding. SNAT (Source NAT) changes the source IP address. A typical example is when a host behind a Wifi router wants to browse the Internet.

Restart UFW for the changes to take effect.

sudo systemctl restart ufw

You also need to enable IP forwarding in /etc/sysctl.conf file as mentioned above.

If the 192.168.1.100 host is also running UFW, then you need to allow port 80 and 443 in UFW.

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Note that the 192.168.1.100 LAN host must use the UFW router as the gateway. If it’s using another IP address as the gateway, port forwarding won’t work.

How to Block BitTorrent Traffic on Cloud Servers

If you run a server on the cloud, you probably want to block BitTorrent traffic on the server, because if you or someone else accidentally download illegal content (Movies, TV Shows) via BitTorrent and you are caught, your hosting provider will likely suspend your account.

Modern BitTorrent clients encrypt traffic between peers, so it’s not easy to differentiate BitTorrent traffic from other types of traffic. While there is no perfect way to block BitTorrent traffic, here is a simple solution.

Block outgoing traffic by default.

sudo ufw default deny outgoing

Then allow specific outgoing ports. For example, you need to allow the DNS port.

sudo ufw allow out 53

And you need to allow port 80 and 443 to update your software packages from the repository.

sudo ufw allow out 80/tcp

sudo ufw allow out 443/tcp

If this is your mail server, you need to allow port 25.

sudo ufw allow out 25/tcp

Restart UFW for the changes to take effect.

sudo systemctl restart ufw

Although this is not a perfect solution, because some users might configure their BitTorrent clients to use port 80 or 443, in reality, this situation is quite rare. Most users just use the default settings and my BitTorrent client on the server immediately stopped all downloading and uploading when I enforce the above rules in the firewall. If it doesn’t stop, restart your BitTorrent client or your server.

Blocking Outgoing Connections to a Specific IP Address

First, you need to allow outgoing traffic.

sudo ufw default allow outgoing

Then block a specific IP address with:

sudo ufw insert 1 deny out to 12.34.56.78

Restart UFW.

sudo systemctl restart ufw

UFW and fail2ban

Fail2ban is a program that uses iptables firewall to prevent servers from brute-force attacks. UFW and Fail2ban won’t interfere with each other.

That’s it! I hope this article helped you use UFW on Debian, Ubuntu and Linux Mint. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks. Take care 🙂