firewalld、netfilter、 netfilter5表5链、iptables介绍
1. Linux網(wǎng)絡相關
設定虛擬網(wǎng)卡ens33:1
示例如下: [root@aminglinux-01 ~]# ifdown ens33 && ifup ens33 成功斷開設備 'ens33'。 成功激活的連接(D-Bus 激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/2)
操作示例如下: [root@aminglinux-01 ~]# cd /etc/sysconfig/network-scripts/ [root@aminglinux-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:0 //復制網(wǎng)卡配置文件,并取一個新的網(wǎng)卡名,這里加反斜杠(\)是因為要把:轉(zhuǎn)義,不然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 //重啟網(wǎng)卡 成功斷開設備 'ens33'。 成功激活的連接(D-Bus 激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/3) [root@aminglinux-01 network-scripts]# ifconfig //重啟后可以看到多了一個網(wǎng)卡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
[root@aminglinux-01 ~]# mii-tool ens33 //link ok表示連接正常,如果顯示no link表示網(wǎng)卡壞了或沒有連接網(wǎng)線
ens33: negotiated 1000baseT-FD flow-control, link ok
[root@aminglinux-01 ~]# ethtool ens33 //最后一行顯示link detected:yes說明網(wǎng)卡正常,如果顯示為no,說明網(wǎng)卡壞或沒有連接網(wǎng)線
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
[root@aminglinux-01 ~]# hostname //查看主機名
aminglinux-01
[root@aminglinux-01 ~]# hostname Gary-tao //更改主機名,但是這僅僅只是保存在內(nèi)存中,重啟后失效。
[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
[root@aminglinux-01 ~]# cat /etc/resolv.conf //查看網(wǎng)卡已有的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
[root@aminglinux-01 ~]# cat /etc/hosts //查看文件內(nèi)容
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
[root@gary-tao ~]# getenforce //查看當前SElinux的狀態(tài)
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 這里我已經(jīng)更改了
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僅僅是一個工具。目前現(xiàn)在很多企業(yè)依然在使用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服務
[root@gary-tao ~]# iptables -nvL //查看iptables默認規(guī)則
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 //重啟規(guī)則
[root@gary-tao ~]# cat /etc/sysconfig/iptables //查看規(guī)則文件
Redirecting to /bin/systemctl restart iptables.service
[root@gary-tao ~]# iptables -F //清空所有規(guī)則
[root@gary-tao ~]# service iptables save //保存后規(guī)則里就什么規(guī)則都沒有了
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 確定 ]
[root@gary-tao ~]# iptables -t filter -nvl
3. iptables -Z //-Z把包和流量計數(shù)器清零 4. 增加/刪除一條規(guī)則,其用法如下: 5. iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP //增加一條規(guī)則[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
[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還有一個規(guī)則經(jīng)常用支,即-P(大寫)選項,它表示預設策略,用法如下:[root@gary-tao ~]# iptables -P INPUT DROP
說明:-P后面跟鏈名,策略內(nèi)容或為DROP,或為ACCEPT,默認是ACCEPT。注意:如果你在連接遠程服務器,千萬不要隨便執(zhí)行這個命令,因為一旦輸入命令并回車,遠程連接會被斷開。這個策略一旦設定成功后,只有使用命令iptables -P INPUT ACCEPT才能恢復成原始狀態(tài)。 解釋: - -A:增加一條規(guī)則。 - -D:刪除一條規(guī)則。 - -I:插入一條規(guī)則 insert,其效果-A一樣。 - -p:指定協(xié)議,可以是tcp、udp或者icmp。 - --dport:和-p一起使用,指定目標端口destination port。 - --sport:和-p一起使用,指定源端口source port。 - -s:指定源ip(可以是一個ip段)。 - -d:指定目的ip(可以是一個ip段)。 - -j:后面跟動作,ACCEPT表示允許包,REJECT表示拒絕包,DROP表示丟掉包。 - -i:指定網(wǎng)卡interface(不常用,但偶爾能用到)。轉(zhuǎn)載于:https://blog.51cto.com/taoxie/2045793
《新程序員》:云原生和全面數(shù)字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的firewalld、netfilter、 netfilter5表5链、iptables介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL修改datadir目录
- 下一篇: 用钉钉接收zabbix告警