##linux防火墙
selinux
在配制远程联接时是要关掉selinux的。
临时关掉selinux:
setenforce 0
永久关掉要编辑配置文件
vim /etc/selinux/config
SELINUX=disabled
平常通常关掉selinux
2.iptables
iptables是linux的防火墙机制。在centos6时,是用的netfilter机制fedora 18 关闭防火墙,centos7时用的是firewalld机制。
因为我们装的是centos7,防火墙机制就是firewalld,想使用netfilter,就必须将firewald停掉,开启netfilter.
关掉firewalld:
不让firewalld开机启动:
[root@shuai-01 network-scripts]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
停掉firewalld服务:
[root@shuai-01 network-scripts]# systemctl stop firewalld
安装netfilter:
[root@shuai-01 network-scripts]# yum install -y iptables-services
开启iptables服务:
[root@shuai-01 network-scripts]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@shuai-01 network-scripts]# systemctl start iptables
关掉firewalld开启netfiler服务的步骤:
firewalld-->close/disable+stop
netfilter-->enable+start
netfilter的五个表:(filter,nat,mangle,raw,security)
这儿主要了解filter和nat。
filter表主要用于过滤包中国linuxandroid linux,包含三个链INPUT,OUTPUT,FORRWARD.
nat表主要用于网路地址转换,包含三个链PREROUTING,OUTPUT,POSTROUTING。
PREROUTING:数据包进来的那一刻我要对他进行修改
POSTROUTING:数据包出去的那一刻要进行修改
当数据包进来,是本机数据包:PREROUTING,INPUT,localhost,OUTPUT,POSTROUTING
不是本机数据包:PREROUTING,FORWARD,POSTROUTING
一个讲IPtables的五个表的文章:
netfilter
netfilter是linux操作系统核心层内部的一个数据包处理模块
netfilter和IPtables
iptables的4张表和5条链
4张表:filter表,nat表,mangle表,raw表
5条链:INPUT,OUTPUT,FIRWARD,PREROUTING,POSTROUTING
这张图,表示了iptables的4张表和5条链的关系,也表示数据包进来后的一些运转。
iptables的规则的组成
数据包的访问控制:ACCEPT,DROP(直接扔掉数据包),REJECT(遗弃数据包后,返回对应的消息)
数据包的改写:SNAT(对源地址的改写),DNAT(对目的地址的改写)
iptables相关的命令和用法:
防火墙的规则保存在/etc/sysconfig/iptables中
- 查看nat表的规则:
[root@shuai-01 network-scripts]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 2 packets, 152 bytes)
pkts bytes target prot opt in out source destination
- 清空filter表的规则:
[root@shuai-01 network-scripts]# iptables -F
[root@shuai-01 network-scripts]# iptables -nvL
Chain INPUT (policy ACCEPT 5 packets, 356 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 4 packets, 448 bytes)
pkts bytes target prot opt in out source
- 重启标的规则:
[root@shuai-01 network-scripts]# service iptables restart
Redirecting to /bin/systemctl restart iptables.service
[root@shuai-01 network-scripts]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
24 1688 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
0 0 REJECT all -- * * 0.0.0.0/0
##iptables小案例
需求:只针对filter表,预设策略INPUT链DROP,其他两个链ACCEPT,之后针对192.168.137.0/24开通22端口,对所有网关开放80端口,对所有网关开放21端口。
这需求不复杂,并且有许多条规则,写成脚本方式。
脚本内容:
#!/bin/bash
ipt="/usr/sbin/iptables"//定义一个变量(关于iptables命令的绝对路径)
$ipt-F//删掉规则
$ipt-PINPUTDROP//filter表INPUT链丢包
$ipt-POUTPUTACCEPT//flter表OUTPUT链容许包
$ipt-PFORWARDACCEPT//filter表的FORWARD链容许包
$ipt-AINPUT-mstate--stateRELATED,ESTABLISHED-jACCEPT//添加规则对RELATED,ESTABLISHED状态进行放行,这两个是通讯联接上了以后的包来往
$ipt-AINPUT-s192.168.137.0/24-ptcp--dport22-jACCEPT//添加规则对INPUT表的来源192.168.137.0/24网关的22端口放行
$ipt-AINPUT-ptcp--dport80-jACCEPT//对全网关80端口放行
$ipt-AINPUT-ptcp--dport21-jACCEPT//对全网端的21端口放行
2.关于icmp包(ping本机不同,本机ping内网能通)
iptables -I INPIUT -p -icmp --icmp-type 8 -j DROP
##iptablesnat表的应用
(将A机器做个路由器,能上内网fedora 18 关闭防火墙,能连外网)
给A机器添加一块网卡
选择LAN区段
给B机器添加网卡LAN区段
给A机器网卡(外网网卡)设置IP(ens37192.168.100.1)
编辑ens37的配置文件
/etc/sysconfig/network-scripts/ifcfg-ens37
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=ens37
UUID=d23205fd-677c-4490-92f0-dd3d07af7abf
DEVICE=ens37
ONBOOT=yes
IPADDR=192.168.100.1
NETMASK=255.255.255.0
保存退出后重启网路服务
[root@shuai-01 network-scripts]# service network restart
查看网卡ip
[root@shuai-01 network-scripts]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.176.135 netmask 255.255.255.0 broadcast 192.168.176.255
inet6 fe80::16f5:8ee4:b971:e6fb prefixlen 64 scopeid 0x20
ether 00:0c:29:a1:0e:2c txqueuelen 1000 (Ethernet)
RX packets 735 bytes 66440 (64.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 592 bytes 74403 (72.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163 mtu 1500
inet 192.168.176.150 netmask 255.255.255.0 broadcast 192.168.176.255
ether 00:0c:29:a1:0e:2c txqueuelen 1000 (Ethernet)
ens37: flags=4163 mtu 1500
inet 192.168.100.1 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::2224:2bb4:6539:1022 prefixlen 64 scopeid 0x20
ether 00:0c:29:a1:0e:36 txqueuelen 1000 (Ethernet)
RX packets 80 bytes 27360 (26.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 218 bytes 36144 (35.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1 (Local Loopback)
RX packets 76 bytes 6204 (6.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 76 bytes 6204 (6.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
B机器设置IP(ens37192.168.100.100),设置网段192.168.100.1
ifconfig ens37 192.168.100.100
A机器pingB机器
[root@shuai-01 network-scripts]# ping 192.168.100.100
PING 192.168.100.100 (192.168.100.100) 56(84) bytes of data.
64 bytes from 192.168.100.100: icmp_seq=1 ttl=64 time=0.668 ms
64 bytes from 192.168.100.100: icmp_seq=2 ttl=64 time=0.261 ms
64 bytes from 192.168.100.100: icmp_seq=3 ttl=64 time=0.471 ms
^C
--- 192.168.100.100 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.261/0.466/0.668/0.168 ms
B机器pingA机器都能通
要使B机器能连上内网
A机器打开路由转发(/proc/sys/net/ipv4/ip_forward为0表示没打开)
[root@shuai-01 network-scripts]# cat /proc/sys/net/ipv4/ip_forward
0
[root@shuai-01 network-scripts]# echo "1" > /proc/sys/net/ipv4/ip_forward
[root@shuai-01 network-scripts]# cat /proc/sys/net/ipv4/ip_forward
1
B机器上设置网段
查看网段命令
route -n
添加网段:
route add default gw 192.168.100.1
这是B机器能够ping通内网了。
B机器上设置网段为外网网段,A机器上网关为内网网卡的网段
需求
两台机器之间不能直接通讯要依靠另一台机器来通讯,
任意两台机器之间要通讯,要有源IP,目的IP。c和a不能直接通讯,a和b能通信,b和c能通信。c要想和a通讯,从c出去的给a的包经过b时要改变源地址(改成b的地址),从a发出来给c的包要经过b时要改变目的地址(改成c的地址)
在A机器上加两条规则
#这条是进来的包改目的地址
[root@shuai-01 ~]# iptables -t nat -A PREROUTING -d 192.168.176.135 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22
#这条是出去的包改源地址
[root@shuai-01 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.176.135
B机器添加网段
保存和备份iptables规则
防火墙规则保存在/etc/sysconfig/iptables文件中,系统启动时,会手动加载文件中的规则