Linux firewall to open the relevant port and view the open port

Source: Internet
Author: User

Sometimes we worked hard to install a service but found that unable to access, thinking that they did not install success, in fact, the solution is very simple, is to open the relevant port. For example, if I install Nginx or Apache, I need to release port 80 on the firewall. The relevant instance code is as follows.

/sbin/iptables-i input-p TCP--dport 80-j ACCEPT #开启80端口

/sbin/iptables-i input-p TCP--dport 22-j ACCEPT #开启22端口

/etc/rc.d/init.d/iptables Save #保存配置

/etc/rc.d/init.d/iptables Restart #重启服务

View open Ports

/etc/init.d/iptables status

The results are as follows

Table:filter

Chain INPUT (Policy ACCEPT)

Num Target prot opt source destination

2 ACCEPT TCP--0.0.0.0/0 0.0.0.0/0 TCP dpt:22

3 ACCEPT TCP--0.0.0.0/0 0.0.0.0/0 TCP dpt:80

Iptables is a firewall under Linux and is also the service name.

Service iptables Status View firewall state

Service iptables start to turn on the firewall

Service iptables Stop shutting down the firewall

Service iptables Restart Restart firewall

Firewalls Open specific ports:

① file/etc/sysconfig/iptables

② Add:

-A rh-firewall-1-input-m state--state new-m tcp-p TCP--dport 8080-j ACCEPT

★ The number 8080 stands for open 8080 ports, can also be changed to other ports ★

③ Restart the firewall

================================================================

Save settings for the firewall

Serivce iptables Save

View Iptables rules and numbers

IPTABLES-NL--line-number

Turn off all input FORWARD (forwards) output for all ports

Iptables-p INPUT DROP

Iptables-p FORWARD DROP

Iptables-p OUTPUT DROP

Open only 22 ports

Iptables-a input-p TCP--dport 22-j ACCEPT

Iptables-a output-p TCP--sport 22-j ACCEPT

Parameter explanation:

The –A parameter is seen as adding a rule

–P specifies what protocol we commonly use for the TCP protocol, and of course there are UDP, such as 53-port DNS

–dport is the destination port, when the data goes from outside to the server as the destination port

–sport data goes out of the server, it is used for the data source port

–J is designated as Accept-receive or DROP not receive

Disable an IP access

Iptables-a input-p tcp-s 192.168.1.2-j DROP

–s parameter is source (i.e. 192.168.1.2)

The back rejection is drop.

Delete Rule

Iptables-d INPUT 2

Delete the input chain number 2 rule

Linux firewall to open the relevant ports and view open ports

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.