CentOS7 Open port (permanent) (leafage.top)

After services are installed on Linux and started, the external network is inaccessible by default because the firewall does not allow it. Therefore, you need to enable the firewall to access these port numbers (for example, Redis).

Method 1: Use the Firewall

1. Run the following command:

firewall-cmd --get-active-zones
Copy the code

After the run is complete, you should see the zone name as follows:

2. Run the following command:

firewall-cmd --zone=public --add-port=6379/tcp --permanent
Copy the code

3. Restart the firewall and run the following command:

firewall-cmd --reload
Copy the code

4. Check whether the port number is enabled and run the following command:

firewall-cmd --query-port=6379/tcp
Copy the code

Method 2: Use iptables

1. Run the following command:

/sbin/iptables -I INPUT -p tcp --dport xxx -j ACCEPT
Copy the code

2. Then run:

/etc/rc.d/init.d/iptables save
Copy the code

Or the following command:

-A INPUT -m state –state NEW -m tcp -p tcp –dport xxx -j ACCEPT
Copy the code

Then execute the command:

-a input-j REJECT - REJECT -with icMP-host-prohibitedCopy the code

Note: XXX — indicates the port number you want to open, such as 6379

Test: In Windows, press win+R and enter CMD to run the command (Telnet must be enabled). If the interface becomes empty, it indicates success:

Telnet 192.168 xx, xx, 6379Copy the code

Note: The method for enabling Telnet is as follows:

  1. Enter the control panel, select the program;

  1. Click the program, enter the program and function;

  1. Select Telnet and click OK.