日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

firewalld、netfilter、 netfilter5表5链、iptables介绍

發布時間:2025/4/16 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 firewalld、netfilter、 netfilter5表5链、iptables介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. Linux網絡相關

  • ifconfig查看網卡ip(如果沒有這個命令請使用yum install net-tools安裝)
  • ifdown ens33 //關閉網卡ens33(在使用遠程終端連接時不可使用這條命令,否則會斷掉網絡)
  • ifup ens33 //打開網卡ens33,用這種方式可以單獨重啟指定的網卡(有幾張網卡的情況下),而不是重啟整個網絡服務。
    設定虛擬網卡ens33:1
  • ifdown ens33 && ifup ens33 //在遠程終端可以把兩條命令相結合使用,這樣重啟的效果更好。
    示例如下: [root@aminglinux-01 ~]# ifdown ens33 && ifup ens33 成功斷開設備 'ens33'。 成功激活的連接(D-Bus 激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/2)
  • 給一個網卡設定多個IP
    操作示例如下: [root@aminglinux-01 ~]# cd /etc/sysconfig/network-scripts/ [root@aminglinux-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:0 //復制網卡配置文件,并取一個新的網卡名,這里加反斜杠(\)是因為要把:轉義,不然linux命令無法識別。 [root@aminglinux-01 network-scripts]# vi ifcfg-ens33\:0 //更改完成后保存退出 [root@aminglinux-01 network-scripts]# cat ifcfg-ens33\:0 TYPE=Ethernet BOOTPROTO=static DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33:0 //這個地方要更改成前面設置的ens33:0 UUID=3c08189a-cb3f-4879-92de-ee0691b1d4cc DEVICE=ens33:0 //這個地方要更改成前面設置的ens33:0 ONBOOT=yse IPADDR=192.168.1.190 //更改一個沒有使用的IP NETMASK=255.255.0.0 GATEWAY=192.168.1.1 DNS1=119.29.29.29 DNS2=8.8.8.8 [root@aminglinux-01 network-scripts]# ifdown ens33 && ifup ens33 //重啟網卡 成功斷開設備 'ens33'。 成功激活的連接(D-Bus 激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/3) [root@aminglinux-01 network-scripts]# ifconfig //重啟后可以看到多了一個網卡ens33:0 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.1.185 netmask 255.255.0.0 broadcast 192.168.255.255inet6 fe80::1ffb:cde1:5f3e:5778 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:09:e5:58 txqueuelen 1000 (Ethernet)RX packets 3561287 bytes 361515464 (344.7 MiB)RX errors 0 dropped 1092 overruns 0 frame 0TX packets 113641 bytes 22059136 (21.0 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  • ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 192.168.1.190 netmask 255.255.0.0 broadcast 192.168.255.255
    ether 00:0c:29:09:e5:58 txqueuelen 1000 (Ethernet)

    lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
    inet 127.0.0.1 netmask 255.0.0.0
    inet6 ::1 prefixlen 128 scopeid 0x10<host>
    loop txqueuelen 1 (Local Loopback)
    RX packets 28 bytes 2380 (2.3 KiB)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 28 bytes 2380 (2.3 KiB)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    6. 查看網卡是否連接 示例如下:

    [root@aminglinux-01 ~]# mii-tool ens33 //link ok表示連接正常,如果顯示no link表示網卡壞了或沒有連接網線
    ens33: negotiated 1000baseT-FD flow-control, link ok
    [root@aminglinux-01 ~]# ethtool ens33 //最后一行顯示link detected:yes說明網卡正常,如果顯示為no,說明網卡壞或沒有連接網線
    Settings for ens33:
    Supported ports: [ TP ]
    Supported link modes: 10baseT/Half 10baseT/Full
    100baseT/Half 100baseT/Full
    1000baseT/Full
    Supported pause frame use: No
    Supports auto-negotiation: Yes
    Advertised link modes: 10baseT/Half 10baseT/Full
    100baseT/Half 100baseT/Full
    1000baseT/Full
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Speed: 1000Mb/s
    Duplex: Full
    Port: Twisted Pair
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: on
    MDI-X: off (auto)
    Supports Wake-on: d
    Wake-on: d
    Current message level: 0x00000007 (7)
    drv probe link
    Link detected: yes

    7. 更改主機名示例如下:

    [root@aminglinux-01 ~]# hostname //查看主機名
    aminglinux-01
    [root@aminglinux-01 ~]# hostname Gary-tao //更改主機名,但是這僅僅只是保存在內存中,重啟后失效。
    [root@aminglinux-01 ~]# hostname //更改完成,重啟后失效。
    Gary-tao
    [root@aminglinux-01 ~]# vim /etc/hostname //如果想要重啟生效,需要更改配置文件。
    [root@aminglinux-01 ~]# hostnamectl set-hostname Gary-tao //這個命令可以直接更改/ect/hostname文件 ,只適用centos7
    [root@aminglinux-01 ~]# hostname
    gary-tao
    [root@aminglinux-01 ~]# cat /etc/hostname //顯示文件已更改生效
    gary-tao

    8. 更改DNS- 在網卡配置文件里更改DNS是永久更改, 更改命令vi /etc/sysconfig/network-scripts/ifcfg-ens33(網卡名稱),- 但是在/etc/resolv.conf中也可以更改DNS,這里的更改是臨時的。一般可以在此設置多個DNS,以做備用,如果只是臨時修改DNS地址,就直接修改DNS配置文件,如果需要永久生效的話,還是要修改網卡的配置文件。示例如下:

    [root@aminglinux-01 ~]# cat /etc/resolv.conf //查看網卡已有的DNS

    Generated by NetworkManager

    nameserver 119.29.29.29
    nameserver 8.8.8.8
    [root@aminglinux-01 ~]# vim /etc/resolv.conf //臨時更改

    Generated by NetworkManager

    nameserver 119.29.29.29
    nameserver 8.8.8.8

    - /etc/hosts文件在linux下有一個特殊文件/etc/hosts可能解析域名,不過需要我們在里面手動添加ip與域名,它的作用是臨時解析某個域名,非常有用。示例如下:

    [root@aminglinux-01 ~]# cat /etc/hosts //查看文件內容
    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    [root@aminglinux-01 ~]# ping www.qq123.com
    PING www.qq123.com (202.91.250.93) 56(84) bytes of data.
    64 bytes from 202.91.250.93 (202.91.250.93): icmp_seq=1 ttl=231 time=30.3 ms
    64 bytes from 202.91.250.93 (202.91.250.93): icmp_seq=2 ttl=231 time=30.2 ms
    64 bytes from 202.91.250.93 (202.91.250.93): icmp_seq=3 ttl=231 time=30.2 ms
    ^C
    --- www.qq123.com ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2003ms
    rtt min/avg/max/mdev = 30.227/30.284/30.395/0.162 ms
    [root@aminglinux-01 ~]# vim /etc/hosts //編輯文件把上面這個域名指向的ip更改成192.168.1.190 ,保存退出
    [root@aminglinux-01 ~]# ping www.qq123.com //驗證域名指定IP
    PING www.qq123.com (192.168.1.190) 56(84) bytes of data.
    64 bytes from www.qq123.com (192.168.1.190): icmp_seq=1 ttl=64 time=0.152 ms
    64 bytes from www.qq123.com (192.168.1.190): icmp_seq=2 ttl=64 time=0.086 ms
    64 bytes from www.qq123.com (192.168.1.190): icmp_seq=3 ttl=64 time=0.060 ms
    ^C
    --- www.qq123.com ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2004ms
    rtt min/avg/max/mdev = 0.060/0.099/0.152/0.039 ms

    注意: 1. 一個IP后面可以跟多個域名,可以是幾十個甚至上百個。 2. 每一行只能有一個IP,也就是一個域名不能對應多個IP。 3. 如果有多行中出現相同的域名(對應的IP不一樣),會按最前面出現的記錄來解析。----------## 2. firewalld和netfilter1. SElinuxselinux是Linux系統特有的安全機制,因為這種機制的限制太多,配置也麻煩,所以幾乎沒有人真正的應用它。安裝完系統后我們一般會選擇關閉selinux。示例如下:

    [root@gary-tao ~]# getenforce //查看當前SElinux的狀態
    Enforcing
    [root@gary-tao ~]# setenforce 0 //臨時關閉
    [root@gary-tao ~]# getenforce
    Permissive
    [root@gary-tao ~]# cat /etc/selinux/config //查看配置文件

    This file controls the state of SELinux on the system.

    SELINUX= can take one of these three values:

    enforcing - SELinux security policy is enforced.

    permissive - SELinux prints warnings instead of enforcing.

    disabled - No SELinux policy is loaded.

    SELINUX=enforcing

    SELINUXTYPE= can take one of three two values:

    targeted - Targeted processes are protected,

    minimum - Modification of targeted policy. Only selected processes are protected.

    mls - Multi Level Security protection.

    SELINUXTYPE=targeted
    [root@gary-tao ~]# vi /etc/selinux/config //更改配置文件,重啟生效

    This file controls the state of SELinux on the system.

    SELINUX= can take one of these three values:

    enforcing - SELinux security policy is enforced.

    permissive - SELinux prints warnings instead of enforcing.

    disabled - No SELinux policy is loaded.

    SELINUX=disabled //把enforcing改成disabled 這里我已經更改了

    SELINUXTYPE= can take one of three two values:

    targeted - Targeted processes are protected,

    minimum - Modification of targeted policy. Only selected processes are protected.

    mls - Multi Level Security protection.

    SELINUXTYPE=targeted

    2. netfilter和firewalld- 在centos版本5和6上用的防火墻是netfiler,centos7則用的是firewalld防火墻,很多人把Linux的防火墻叫作iptables,其實這不是,iptables僅僅是一個工具。目前現在很多企業依然在使用centos6,但firewalld是向下兼容netfiler的,同樣也支持之前版本的命令用法。示例如下:

    [root@gary-tao ~]# systemctl disable firewalld //禁止firewalld服務開機啟動
    Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
    Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
    [root@gary-tao ~]# systemctl stop firewalld //關閉firewalld服務
    [root@gary-tao ~]# yum install -y iptables-services //安裝iptables-services,這樣就可以使用之前版本的iptables了。
    [root@gary-tao ~]# systemctl enable iptables //讓它開機啟動
    [root@gary-tao ~]# systemctl start iptables //啟動iptables服務

    ----------## 3. netfilter5表5鏈介紹- netfilter的5個表 1. filter表用于過濾包,是系統預設的表,也是最常用的表,該表有內建三個鏈INPUT、OUTPUT、FORWARD。 - INPUT鏈作用于進入本機的包。 - OUTPUT鏈作用于本機送機的包。 - FORWARD鏈作用于那些跟本機無關的包。2. nat表用于網絡地址轉換,有PREROUTING、OUTPUT、POSTROUTING三個鏈。 - PREROUTINC鏈的作用是在包剛剛到達防火墻時改變它的目的地址(如果需要的話) - OUTPUT鏈的作用是改變本地產生的包的目的地址。 - POSTROUTINC鏈的作用是在包即將離開防火墻時改變其源地址。3. 數據包流向與netfilter的5個鏈 - PREROUTING:數據包進入路由表之前。 - INPUT:通過路由表后目的地為本機。 - FORWARD:通過路由表后,目的地不為本機。 - OUTPUT:由本機產生,向外發出。 - POSTROUTING:發送到網卡接口之前。4. 具體的數據包流向,可以參考下圖。![](https://i.imgur.com/SnGqpwR.png)參考文章 http://www.cnblogs.com/metoy/p/4320813.html## 4. iptables語法1. iptables已經有默認規則,我們可以用以下命令查看。但是我們在設置自己的規則之前,建議先將其清除。-nvL選項表示查看規則,-F選項表示清除當前規則,但清除只是臨時的,重啟還是會加載已經保存的規則,所以需要使用service iptables save保存一下規則。通過下面這個命令輸出,我們也可以看到防火墻規則保存在/etc/sysconfig/iptables里,可以查看下這個文件。示例如下:

    [root@gary-tao ~]# iptables -nvL //查看iptables默認規則
    Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
    pkts bytes target prot opt in out source destination
    56 4076 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
    0 0 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
    0 0 ACCEPT all -- lo 0.0.0.0/0 0.0.0.0/0
    0 0 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
    1235 94818 REJECT all -- * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    pkts bytes target prot opt in out source destination
    0 0 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

    Chain OUTPUT (policy ACCEPT 40 packets, 3788 bytes)
    pkts bytes target prot opt in out source destination
    [root@gary-tao ~]# service iptables restart //重啟規則
    [root@gary-tao ~]# cat /etc/sysconfig/iptables //查看規則文件
    Redirecting to /bin/systemctl restart iptables.service
    [root@gary-tao ~]# iptables -F //清空所有規則
    [root@gary-tao ~]# service iptables save //保存后規則里就什么規則都沒有了
    iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 確定 ]

    2. -t后面跟表名,指定filter表

    [root@gary-tao ~]# iptables -t filter -nvl

    3. iptables -Z //-Z把包和流量計數器清零 4. 增加/刪除一條規則,其用法如下: 5. iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP //增加一條規則

    [root@gary-tao ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
    [root@gary-tao ~]# iptables -nvL
    Chain INPUT (policy ACCEPT 847 packets, 64330 bytes)
    pkts bytes target prot opt in out source destination
    0 0 DROP tcp -- 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80

    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    pkts bytes target prot opt in out source destination

    Chain OUTPUT (policy ACCEPT 25 packets, 2224 bytes)
    pkts bytes target prot opt in out source destination

    6. iptables -I INPUT -s 1.1.1.1 -j DROP //表示插入一條規則,丟掉所有來自1.1.1.1的數據包。 7. iptables -A INPUT -s 1.1.1.1 -j DROP //表示增加一條規則 8. iptables -D INPUT -s 1.1.1.1 -j DROP //注意刪除一條規則時,必須和插入的規則一致。 9. iptables -I INPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT //表示把來自192.168.1.0/24這個網段且作用在etho上的包放行。 10. 有時候服務器上的iptables過多了,你想刪除一某一條規則,但又不知道之前創建時的規則,你可先查看iptables規則,命令如下:

    [root@gary-tao ~]# iptables -nvL --line-numbers
    Chain INPUT (policy ACCEPT 9235 packets, 708K bytes)
    num pkts bytes target prot opt in out source destination
    1 0 0 DROP tcp -- 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80

    Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
    num pkts bytes target prot opt in out source destination

    Chain OUTPUT (policy ACCEPT 191 packets, 18404 bytes)
    num pkts bytes target prot opt in out source destination

    然后刪除一條規則,使用如下命令:

    [root@gary-tao ~]# iptables -D INPUT 1

    11. iptables還有一個規則經常用支,即-P(大寫)選項,它表示預設策略,用法如下:

    [root@gary-tao ~]# iptables -P INPUT DROP

    說明:-P后面跟鏈名,策略內容或為DROP,或為ACCEPT,默認是ACCEPT。注意:如果你在連接遠程服務器,千萬不要隨便執行這個命令,因為一旦輸入命令并回車,遠程連接會被斷開。這個策略一旦設定成功后,只有使用命令iptables -P INPUT ACCEPT才能恢復成原始狀態。 解釋: - -A:增加一條規則。 - -D:刪除一條規則。 - -I:插入一條規則 insert,其效果-A一樣。 - -p:指定協議,可以是tcp、udp或者icmp。 - --dport:和-p一起使用,指定目標端口destination port。 - --sport:和-p一起使用,指定源端口source port。 - -s:指定源ip(可以是一個ip段)。 - -d:指定目的ip(可以是一個ip段)。 - -j:后面跟動作,ACCEPT表示允許包,REJECT表示拒絕包,DROP表示丟掉包。 - -i:指定網卡interface(不常用,但偶爾能用到)。

    轉載于:https://blog.51cto.com/taoxie/2045793

    《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

    總結

    以上是生活随笔為你收集整理的firewalld、netfilter、 netfilter5表5链、iptables介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。