firewalld、netfilter、 netfilter5表5链、iptables介绍
1. Linux网络相关
设定虚拟网卡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 //复制网卡配置文件,并取一个新的网卡名,这里加反斜杠(\)是因为要把:转义,不然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
[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
[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
[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
[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
[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服务
[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:[ 确定 ]
[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
[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介绍的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: MySQL修改datadir目录
- 下一篇: 用钉钉接收zabbix告警