Ubuntu firewall opening and closing ports

UbuntuThe firewall in is iptablesimplemented by . It secures the network by allowing or denying specific network traffic. Firewalls work by making rule filters against network traffic. Each rule determines whether a packet is allowed to pass through the firewall. A rule contains source, destination, protocol, port and an action to perform, such as allow or deny.

1. Check if the firewall is enabled

sudo ufw status verbose

If enabled, the following information will be output

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

If the firewall is not enabled, the output will be the following

Status: inactive

2. Firewall rule settings

2.1 Allow a port to access

sudo ufw allow {
    
    PORT}/{
    
    PROTOCOL}

For example, if you want to allow port 22, you can freely enter and exit the data packets of the TCP protocol

sudo ufw allow 22/tcp

To allow UDP ports, you can use the following command

sudo ufw allow 53/udp

Multiple ports can be allowed

sudo ufw allow 80,443/tcp

2.2 Allow IP address access

sudo ufw allow from {
    
    IP_ADDRESS}

For example, to allow access from IP address 192.168.3.12:

sudo ufw allow from 192.168.3.12

It is also possible to allow access to specific IP addresses and ports:

sudo ufw allow from 192.168.3.12 to any port 22

2.3 Port access denied

sudo ufw deny {
    
    PORT}/{
    
    PROTOCOL}

For example, to deny access to TCP port 22:

sudo ufw deny 22/tcp

2.4 Deny access to specific IP addresses

sudo ufw deny from 192.168.1.100

3. Delete rules

To delete a rule, you first need to obtain the serial number of the rule

sudo ufw status numbered

Call the following command to delete the rules in the firewall

sudo ufw delete {
    
    RULE_NUM}

Guess you like

Origin blog.csdn.net/xiwenhec/article/details/131526142