➜ ~ sudo apt-get install -y ufw ➜ ~ sudo ufw status Status: inactive
Step2: Set Up Default Policies
sudo ufw default deny incoming sudo ufw default allow outgoing
Step3: Allow SSH Connections
pi@raspberrypi:~ $ sudo ufw app list Available applications: Nginx Full Nginx HTTP Nginx HTTPS SSH pi@raspberrypi:~ $ sudo ufw allow SSH pi@raspberrypi:~ $ sudo ufw enable Command may disrupt existing ssh connections. Proceed with operation (y|n)? y Firewall is active and enabled on system startup pi@raspberrypi:~ $ sudo ufw status Status: active To Action From -- ------ ---- SSH ALLOW Anywhere OpenSSH ALLOW Anywhere Nginx Full ALLOW Anywhere SSH (v6) ALLOW Anywhere (v6) OpenSSH (v6) ALLOW Anywhere (v6) Nginx Full (v6) ALLOW Anywhere (v6)
This will create firewall rules that will allow all connections on port 22, which is the port that the SSH daemon listens on. UFW knows what "ssh", and a bunch of other service names, means because it's listed as a service that uses port 22 in the /etc/services file.
We can actually write the equivalent rule by specifying the port instead of the service name. For example, this command works the same as the one above:
sudo ufw allow 22
If you configured your SSH daemon to use a different port, you will have to specify the appropriate port. For example, if your SSH server is listening on port 2222, you can use this command to allow connections on that port:
sudo ufw allow 2222
Now that your firewall is configured to allow incoming SSH connections, we can enable it.
Step5: Allow Other Connections
Now you should allow all of the other connections that your server needs to respond to. The connections that you should allow depends your specific needs. Luckily, you already know how to write rules that allow connections based on a service name or port—we already did this for SSH on port 22.
We will show a few examples of very common services that you may need to allow. If you have any other services for which you want to allow all incoming connections, follow this format.
HTTP connections, which is what unencrypted web servers use, can be allowed with this command:
pi@raspberrypi:~ $ sudo ufw allow 'Nginx HTTP'
If you'd rather use the port number, 80, use this command:
sudo ufw allow 80
HTTPS connections, which is what encrypted web servers use, can be allowed with this command:
pi@raspberrypi:~ $ sudo ufw allow 'Nginx HTTPS'
If you'd rather use the port number, 443, use this command:
sudo ufw allow 443
FTP connections, which is used for unencrypted file transfers (which you probably shouldn't use anyway), can be allowed with this command:
sudo ufw allow ftp
If you'd rather use the port number, 21, use this command:
sudo ufw allow 21/tcp
sudo ufw allow 80 允许外部访问80端口 sudo ufw delete allow 80 禁止外部访问80 端口 sudo ufw allow from 192.168.1.1 允许此IP访问所有的本机端口 sudo ufw deny smtp 禁止外部访问smtp服务 sudo ufw delete allow smtp 删除上面建立的某条规则 sudo ufw deny proto tcp from 10.0.0.0/8 to 192.168.0.1 port 22 要拒绝所有的TCP流量从10.0.0.0/8 到192.168.0.1地址的22端口
Allow Specific Port Ranges
You can specify port ranges with UFW. Some applications use multiple ports, instead of a single port.
For example, to allow X11 connections, which use ports 6000-6007, use these commands:
sudo ufw allow 6000:6007/tcp sudo ufw allow 6000:6007/udp
When specifying port ranges with UFW, you must specify the protocol (tcp or udp) that the rules should apply to. We haven't mentioned this before because not specifying the protocol simply allows both protocols, which is OK in most cases.
Allow Specific IP Addresses
When working with UFW, you can also specify IP addresses. For example, if you want to allow connections from a specific IP address, such as a work or home IP address of
184.108.40.206, you need to specify "from" then the IP address:
sudo ufw allow from 220.127.116.11
You can also specify a specific port that the IP address is allowed to connect to by adding "to any port" followed by the port number. For example, If you want to allow
18.104.22.168 to connect to port
22 (SSH), use this command:
sudo ufw allow from 22.214.171.124 to any port 22
Knowing how to delete firewall rules is just as important as knowing how to create them. There are two different ways specify which rules to delete: by rule number or by the actual rule (similar to how the rules were specified when they were created). We'll start with the delete by rule number method because it is easier, compared to writing the actual rules to delete, if you're new to UFW.
By Rule Number
If you're using the rule number to delete firewall rules, the first thing you'll want to do is get a list of your firewall rules. The UFW status command has an option to display numbers next to each rule, as demonstrated here:
sudo ufw status numbered
To Action From
-- ------ ----
[ 1] 22 ALLOW IN 126.96.36.199/24
[ 2] 80 ALLOW IN Anywhere
If we decide that we want to delete rule 2, the one that allows port 80 (HTTP) connections, we can specify it in a UFW delete command like this:
sudo ufw delete 2
This would show a confirmation prompt then delete rule 2, which allows HTTP connections. Note that if you have IPv6 enabled, you would want to delete the corresponding IPv6 rule as well.
By Actual Rule
The alternative to rule numbers is to specify the actual rule to delete. For example, if you want to remove the "allow http" rule, you could write it like this:
sudo ufw delete allow http
You could also specify the rule by "allow 80", instead of by service name:
sudo ufw delete allow 80
This method will delete both IPv4 and IPv6 rules, if they exist.
How To Disable UFW (optional)
If you decide you don't want to use UFW for whatever reason, you can disable it with this command:
sudo ufw disable
Any rules that you created with UFW will no longer be active. You can always run
sudo ufw enable if you need to activate it later.
Reset UFW Rules (optional)
If you already have UFW rules configured but you decide that you want to start over, you can use the reset command:
sudo ufw reset
This will disable UFW and delete any rules that were previously defined. Keep in mind that the default policies won't change to their original settings, if you modified them at any point. This should give you a fresh start with UFW.