CentOS 7下iptables配置添加修改規則端口方法(轉)


簡介: 

Linux CentOS 7默認沒有安裝iptables,默認的防火牆是firewalld,雲吞鋪子分享CentOS 7系統下iptables安裝、iptables規則配置(放行或者禁用端口)教程方法: 以下大致步驟為:先關閉firewalld防火牆,然后按照iptables,最后配置添加或者禁用端口規則,詳細如下: 檢查firewalld並禁用 安裝iptables之前先檢查下當前CentOS 7系統下的firewalld防火牆狀態,如果是開啟狀態,需要先關閉firewalld防火牆。

Linux CentOS 7默認沒有安裝iptables,默認的防火牆是firewalld,雲吞鋪子分享CentOS 7系統下iptables安裝、iptables規則配置(放行或者禁用端口)教程方法:

以下大致步驟為:先關閉firewalld防火牆,然后按照iptables,最后配置添加或者禁用端口規則,詳細如下:

檢查firewalld並禁用

安裝iptables之前先檢查下當前CentOS 7系統下的firewalld防火牆狀態,如果是開啟狀態,需要先關閉firewalld防火牆。

先查看firewalld狀態

查看firewalld狀態,命令:

systemctl status firewalld

CentOS7防火牆狀態

開啟狀態:顯示綠色的“active (running)”,則代表開啟;
關閉狀態:顯示“inactive (dead)”,代表關閉。

再關閉firewalld

如果firewalld是開啟狀態,需要執行命令:

systemctl stop firewalld

,關閉firewalld即可。

一:安裝iptables

安裝命令:iptables:yum install -y iptables-services

二:啟動iptables

啟動命令:systemctl start iptables

三:查看當前iptables狀態

運行iptables啟動命令后,可以查看下iptables是否啟動成功,確保iptables啟動成功
查看狀態:systemctl status iptables
如果提示綠色的“active (exited)”,則iptables已經啟動成功。

四:查看iptables默認規則

查看默認規則命令:iptables -L
安裝iptables后,可以查看下當前系統下的iptables規則,熟悉一下。

五:備份iptables規則

安全起見,可以先備份下當前的iptables規則,然后再修改/添加規則
備份命令:cp -a /etc/sysconfig/iptables /etc/sysconfig/iptables.bak

六:添加或修改規則(放行或禁用端口)

雲吞鋪子以放行80號端口為例:

  • 1、添加以下規則可以放行80端口命令:
iptables -I INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
  • 2、查看規則是否生效,命令:
iptables -L
  • 3、生效后保存添加的規則,命令:
iptables-save > /etc/sysconfig/iptables
  • 4、設置iptables開啟啟動,命令:
systemctl enable iptables.service

以上為CentOS 7系統下iptables安裝、配置規則和啟用的方法,前面啰嗦比較多,重點在第六步,設置完畢后重啟服務器(命令:systemctl reboot)或者直接 service iptables restart也可以

 

七:查看服務器監聽的端口

netstat -tulpen

[root@Server-45lvhb ~]# netstat -tulpen
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       User       Inode      PID/Program name
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      996        15186      739/redis-server 12
tcp        0      0 192.168.88.50:8300      0.0.0.0:*               LISTEN      10002      21662      1556/consul
tcp        0      0 192.168.88.50:8301      0.0.0.0:*               LISTEN      10002      21844      1556/consul
tcp        0      0 0.0.0.0:53229           0.0.0.0:*               LISTEN      0          20668      1782/nginx: master
tcp        0      0 127.0.0.1:7373          0.0.0.0:*               LISTEN      10000      20234      1544/serf
tcp        0      0 192.168.88.50:8302      0.0.0.0:*               LISTEN      10002      21842      1556/consul
tcp        0      0 0.0.0.0:53230           0.0.0.0:*               LISTEN      0          20667      1782/nginx: master
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          20664      1782/nginx: master
tcp        0      0 0.0.0.0:4369            0.0.0.0:*               LISTEN      10003      17892169   118561/epmd
tcp        0      0 0.0.0.0:53333           0.0.0.0:*               LISTEN      0          20666      1782/nginx: master
tcp        0      0 0.0.0.0:4822            0.0.0.0:*               LISTEN      10007      15252      744/guacd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      0          15244      854/sshd
tcp        0      0 0.0.0.0:15672           0.0.0.0:*               LISTEN      10003      17893803   118622/beam.smp
tcp        0      0 0.0.0.0:9272            0.0.0.0:*               LISTEN      10000      9024899    108110/python
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      0          20665      1782/nginx: master
tcp        0      0 0.0.0.0:8100            0.0.0.0:*               LISTEN      10000      9024140    4848/python2
tcp        0      0 0.0.0.0:25672           0.0.0.0:*               LISTEN      10003      17893780   118622/beam.smp
tcp        0      0 0.0.0.0:8200            0.0.0.0:*               LISTEN      10000      21738      496/python3
tcp6       0      0 :::3306                 :::*                    LISTEN      10009      13126221   111048/mysqld
tcp6       0      0 127.0.0.1:8010          :::*                    LISTEN      0          17264      777/java
tcp6       0      0 :::9100                 :::*                    LISTEN      0          15263      751/node_exporter
tcp6       0      0 :::9999                 :::*                    LISTEN      10006      23294      4038/java
tcp6       0      0 :::9200                 :::*                    LISTEN      997        25037      1505/java
tcp6       0      0 :::4369                 :::*                    LISTEN      10003      17892170   118561/epmd
tcp6       0      0 :::8081                 :::*                    LISTEN      0          16149      777/java
tcp6       0      0 :::9300                 :::*                    LISTEN      997        25000      1505/java
tcp6       0      0 :::8500                 :::*                    LISTEN      10002      21854      1556/consul
tcp6       0      0 :::22                   :::*                    LISTEN      0          15246      854/sshd
tcp6       0      0 :::3000                 :::*                    LISTEN      10005      9024870    108090/grafana-serv
tcp6       0      0 :::8600                 :::*                    LISTEN      10002      21852      1556/consul
tcp6       0      0 :::3005                 :::*                    LISTEN      10000      21475      4148/java
tcp6       0      0 :::9089                 :::*                    LISTEN      10004      21615      1545/prometheus
tcp6       0      0 :::9090                 :::*                    LISTEN      10004      19191      1546/prometheus
tcp6       0      0 :::9474                 :::*                    LISTEN      10000      20230      1544/serf
tcp6       0      0 :::9091                 :::*                    LISTEN      10004      21562      1500/pushgateway
tcp6       0      0 :::33060                :::*                    LISTEN      10009      13126981   111048/mysqld
tcp6       0      0 :::9093                 :::*                    LISTEN      10004      20640      1498/alertmanager
tcp6       0      0 127.0.0.1:8006          :::*                    LISTEN      0          23819      777/java
tcp6       0      0 :::9094                 :::*                    LISTEN      10004      20219      1498/alertmanager
tcp6       0      0 :::5671                 :::*                    LISTEN      10003      17892198   118622/beam.smp
tcp6       0      0 :::5672                 :::*                    LISTEN      10003      17892195   118622/beam.smp
udp        0      0 192.168.88.50:8301      0.0.0.0:*                           10002      21845      1556/consul
udp        0      0 192.168.88.50:8302      0.0.0.0:*                           10002      21843      1556/consul
udp6       0      0 :::8600                 :::*                                10002      21851      1556/consul
udp6       0      0 :::9094                 :::*                                10004      20220      1498/alertmanager
udp6       0      0 :::9474                 :::*                                10000      20231      1544/serf

 

八:查看遠程的端口是否能ping通

 telnet 192.168.88.50 15672 

 

轉自:https://developer.aliyun.com/article/708816


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM