Linux日常运维技巧-2
关于SELINUX
简介:
安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内核模块,也是 Linux 的一个安全子系统。
SELinux 主要由美国国家安全局开发。2.6 及以上版本的 Linux 内核都已经集成了 SELinux 模块。
SELinux 的结构及配置非常复杂,而且有大量概念性的东西,要学精难度较大。很多 Linux 系统管理员嫌麻烦都把 SELinux 关闭了。
如何关闭SELinux
临时关闭SELINUX的方式:
setenforce 0 #此命令可以临时关闭SELINUX,重启后SELINUX还会自动启动。
getenforce #getenforce可以获取SELINUX的运行状态。
[root@long01 ~]# setenforce 0
[root@long01 ~]# getenforce
Permissive
关于SELINUX的运行状态
1:enforcing #开启状态,会强制执行SELINUX的安全策略
2:permissive #提示状态,会打印触发SELINUX安全策略的警告,但是不会执行相应的策略。
3:disabled #关闭SELINUX,重启后也不会再启动SELINUX。
永久关闭SELINUX:
永久关闭SELINUX需要修改SELINUX的配置文件/etc/selinux/config
修改方法1:
vim /etc/selinux/config #使用vim编辑器编辑/etc/selinux/config文件。
将文件中的SELINUX=enforce修改为SELINUX=disabled后,保存退出。
修改方法2:执行下面这条命令:
sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
检查修改结果:
[root@long01 ~]# grep -i "selinux=disabled" /etc/selinux/config
SELINUX=disabled
grep能过滤出结果,表示修改正确。然后重启系统就能完全关闭SELINUX了。
[root@long01 ~]# getenforce
Disabled
firewalld、iptables和netfilter之间的关系
在centos6上,我们用的是iptables服务,而在centos7上,我们用的是firewalld服务。
同样的,centos6上安装的是iptables包,而centos7上安装的是firewalld包。
不管是centos6还是centos7,核心其实都是netfilter,
netfilter是linux的一个内核模块,iptables命令是linux内核自带的。
centos6上的iptables服务和centos7上的firewalld服务,其实都是用来定义防火墙规则功能的防火墙管理工具。
它们都是将定义好的规则交由内核中的netfilter即网络过滤器来读取,从而真正实现防火墙功能,
所以其实在配置规则的思路上是完全一致的。
iptables
CentOS7默认使用的时Firewalld工具,学些iptables前需要将Firewalld关闭并设置开机不启动。
[root@long0000 ~]# systemctl disable firewalld #先禁止firewalld开机自启动
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@long0000 ~]# systemctl stop firewalld #再关闭firewalld服务。
CentOS7默认是没有安装iptables服务的。需要安装iptables服务才能使用iptables。
[root@long0000 ~]# yum install -y iptables-service
设置开机自启动iptables服务,并启动iptables服务
[root@long0000 ~]# systemctl enable iptables && systemctl start iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
查看iptables服务是否正常开启。
[root@long0000 ~]# systemctl status iptables
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
Active: active (exited) since 一 2018-06-11 22:19:18 CST; 43s ago
(#显示active就是正常开启了)
Process: 1964 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
Main PID: 1964 (code=exited, status=0/SUCCESS)
6月 11 22:19:18 long0000 systemd[1]: Starting IPv4 firewall with iptables...
6月 11 22:19:18 long0000 iptables.init[1964]: iptables: Applying firewall rules: [ 确定 ]
6月 11 22:19:18 long0000 systemd[1]: Started IPv4 firewall with iptables.
netfilter5表5链介绍
1.filter表——三个链:INPUT、FORWARD、OUTPUT
作用:过滤数据包
内核模块:iptables_filter.
2.Nat表——三个链:PREROUTING、POSTROUTING、OUTPUT
作用:用于网络地址转换(IP、端口)
内核模块:iptable_nat
3.Mangle表——五个链:PREROUTING、POSTROUTING、INPUT、OUTPUT、FORWARD
作用:修改数据包的服务类型、TTL、并且可以配置路由实现QOS
内核模块:iptable_mangle(别看这个表这么麻烦,咱们设置策略时几乎都不会用到它)
4.Raw表——两个链:OUTPUT、PREROUTING
作用:决定数据包是否被状态跟踪机制处理
内核模块:iptable_raw
5.Security表——三个链:INPUT、OUTPUT和FORWARD
作用:Security表在centos6中并没有,用于强制访问控制(MAC)的网络规则
内核模块:iptable_security
netfilter的5个链
PREROUTING:数据包进入路由表之前
INPUT:通过路由表后目的地为本机
FORWARD:通过路由表后,目的地不为本机
OUTPUT:由本机产生,向外发出
POSTROUTING:发送到网卡接口之前
数据包的传输过程:
1:当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去。
2:如果数据包就是进入本机的,它就会到达INPUT链。数据包到了INPUT链后,任何进程都会收到它。
本机上运行的程序可以发送数据包,这些数据包会经过OUTPUT链,然后到达POSTROUTING链输出。
3:如果数据包是要转发出去的,且内核允许转发,数据包就会经过FORWARD链,然后到达POSTROUTING链输出。
如下图:
iptables语法
iptables一般语法格式:
iptables的命令格式较为复杂,一般的格式如下:
iptables [-t table] 命令 [chain] [rules] [-j target]
格式说明:
table————指定表名,iptables内置包括filter表、nat表、mangle、raw表和security表。
命令—————对链的操作命令
chain————链名
rules————匹配规则
target————动作如何进行
操作命令选项说明
命令 说明
-P或–policy <链名>:定义默认策略
-L或–list <链名>:查看iptables规则列表
-A或—append <链名>:在规则列表的最后增加1条规则
-I或–insert <链名>:在指定的位置插入1条规则
-D或–delete <链名>:从规则列表中删除1条规则
-R或–replace <链名>:替换规则列表中的某条规则
-F或–flush <链名>:删除表中所有规则
-Z或–zero <链名>:将表中数据包计数器和流量计数器归零
规则选项说明
-i或–in-interface <网络接口名>:指定数据包从哪个网络接口进入,如ppp0、eth0和eth1等
-o或–out-interface <网络接口名>:指定数据包从哪块网络接口输出,如ppp0、eth0和eth1等
-p或—proto协议类型 < 协议类型>:指定数据包匹配的协议,如TCP、UDP和ICMP等
-s或–source <源地址或子网>:指定数据包匹配的源地址
–sport <源端口号>:指定数据包匹配的源端口号,可以使用“起始端口号:结束端口号”的格式指定一个范围的端口
-d或–destination <目标地址或子网>:指定数据包匹配的目标地址
–dport <目标端口号>:指定数据包匹配的目标端口号,可以使用“起始端口号:结束端口号”的格式指定一个范围的端口
动作选项说明
ACCEPT:接受数据包
DROP:丢弃数据包
REDIRECT:与DROP基本一样,区别在于它除了阻塞包之外, 还向发送者返回错误信息。
SNAT:源地址转换,即改变数据包的源地址
DNAT:目标地址转换,即改变数据包的目的地址
MASQUERADE:
IP伪装,即是常说的NAT技术,
MASQUERADE只能用于ADSL等拨号上网的IP伪装,也就是主机的IP是由ISP分配动态的;
如果主机的IP地址是静态固定的,就要使用SNAT
LOG日志功能,将符合规则的数据包的相关信息记录在日志中,以便管理员的分析和排错
iptables常用操作示例:
查看iptables规则:
命令:iptables -nvL
选项说明:
n:数字输出。IP地址和端口会以数字的形式打印
v:详细输出。这个选项让list命令显示接口地址、规则选项等信息
L -list:显示所选链的所有规则。如果没有选择链,则会显示所有链的所有规则
[root@long0000 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1612 157K 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
2 104 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
500 239K 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 1459 packets, 394K bytes)
pkts bytes target prot opt in out source destination
清空iptables规则:
命令:iptables -F
选项说明:
F:清空iptables的规则。
[root@long0000 ~]# iptables -F
[root@long0000 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 6 packets, 428 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 destination
注:这里清空的只是当前iptables中设置的规则,不会清空以及保存的规则。
重启iptables服务即可恢复保存过的规则。
[root@long0000 ~]# systemctl restart iptables #重启iptables服务。
保存iptables规则:
命令:service iptables save
说明:执行这条命令会把当前iptables的所有规则保存到/etc/sysconfig/iptables文件中。
[root@long0000 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 确定 ]
[root@long0000 ~]# cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
以上这些就是iptables最初的规则了。
数据包计数器清零
命令:iptables -Z
选项说明:
Z选项可以清空所有规则相关的数据包数量
[root@long0000 ~]# iptables -Z
[root@long0000 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes) ###packets和pkts都是规则相关的数据包数量。
pkts bytes target prot opt in out source destination
30 2028 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
1 32 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 17 packets, 1772 bytes)
pkts bytes target prot opt in out source destination
增加一条规则:
##丢弃来自10.1.1.169的icmp协议数据包。可以使10.1.1.169这个台主机ping不通这台服务器。
[root@long0000 ~]# iptables -F #先清空iptables的规则。
[root@long0000 ~]# iptables -A INPUT -s 10.1.1.169 -p icmp -j DROP
[root@long0000 ~]# iptables -nvL INPUT #查看INPUT链的规则
Chain INPUT (policy ACCEPT 60 packets, 6016 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP icmp -- * * 10.1.1.169 0.0.0.0/0
验证:
C:\Users\LongGe>ping 10.1.1.25
正在 Ping 10.1.1.25 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。
10.1.1.25 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),
删除一条规则,方法1:
##删除刚才添加的那一条规则
[root@long0000 ~]# iptables -D INPUT -s 10.1.1.169 -p icmp -j DROP
[root@long0000 ~]# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 6 packets, 428 bytes)
pkts bytes target prot opt in out source destination
这样删除需要知道添加这一条规则的完整命令,非常不方便。
删除一条规则,方法2:
第二种方法是通过规则编号来删除规则。
查看规则编号的选项是 --line-numbers
[root@long0000 ~]# iptables -A INPUT -s 10.1.1.169 -p icmp -j DROP
[root@long0000 ~]# iptables -nvL INPUT --line-numbers
Chain INPUT (policy ACCEPT 41 packets, 2704 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP icmp -- * * 10.1.1.169 0.0.0.0/0
#第一列的 num便是规则编号了。
##下面通过规则编号删除规则就方便多了
[root@long0000 ~]# iptables -D INPUT 1 #-D选项 跟上链名,和规则编号即可删除指定的规则
[root@long0000 ~]# iptables -nvL INPUT --line-numbers
Chain INPUT (policy ACCEPT 10 packets, 724 bytes)
num pkts bytes target prot opt in out source destination
添加默认规则:
说明:默认规则就是除了可以匹配iptables规则的数据包以外的所有数据包都交给默认规则来处理。
[root@long0000 ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#实验需要首先添加允许22端口的数据包通过,否则下面的规则会让远程连接断开再也连不上。
[root@long0000 ~]# iptables -P OUTPUT ACCEPT #默认允许所有OUTPUT链的数据包通过。
[root@long0000 ~]# iptables -P FORWARD ACCEPT #默认允许所有FORWARD链的数据包通过
[root@long0000 ~]# iptables -P INPUT DROP #默认丢弃INPUT链的数据包
添加这些规则以后,就只能访问这台服务器的22端口了。
[root@long0000 ~]# ping 10.1.1.1
PING 10.1.1.1 (10.1.1.1) 56(84) bytes of data.
^C
--- 10.1.1.1 ping statistics ---
10 packets transmitted, 0 received, 100% packet loss, time 9002ms
#就连网关都ping不通。Windows同样也ping不通这台服务器。
##再添加允许icmp包通过的规则
[root@long0000 ~]# iptables -A INPUT -p icmp -j ACCEP
##添加允许icmp包通过之后就能ping通网关了。但是还ping不通外网。
[root@long0000 ~]# ping 10.1.1.1
PING 10.1.1.1 (10.1.1.1) 56(84) bytes of data.
64 bytes from 10.1.1.1: icmp_seq=1 ttl=64 time=0.939 ms
64 bytes from 10.1.1.1: icmp_seq=2 ttl=64 time=0.997 ms
^C
--- 10.1.1.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.939/0.968/0.997/0.029 ms
iptables filter表案例
案例需求1:放行端口
只放行80、21、22三个端口的数据包,并且22端口只有指定的IP段才能通过。
这个案例我们可以编写一个shell脚本来实现。
vim /usr/local/sbin/iptables.sh ##编辑脚本文件,加入以下内容
#! /bin/bash #定义执行脚本的shell
IPT="/usr/sbin/iptables" #定义变量,iptables命令的绝对路径。
$IPT -F #清空iptables规则
$IPT -P INPUT DROP #默认规则,丢弃所有数据包。
$IPT -P OUTPUT ACCEPT #默认规则,放行所有OUTPUT链的数据包
$IPT -P FORWARD ACCEPT #默认规则,放行所有FORWARD链的数据包
##下面这条规则允许通过RELATED和ESTABLISHED状态的数据包,这条规则必加,否则可能导致某些服务连不上。
$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A INPUT -s 10.1.1.0/24 -p tcp --dport 22 -j ACCEPT #仅允许10.1.1.0/24网段链接22端口
$IPT -A INPUT -p tcp --dport 80 -j ACCEPT #允许通过所有80端口的数据包
$IPT -A INPUT -p tcp --dport 21 -j ACCEPT #允许通过所有21端口的数据包
[root@long0000 ~]# vi /usr/local/sbin/iptables.sh
[root@long0000 ~]# cat /usr/local/sbin/iptables.sh
#! /bin/bash
IPT="/usr/sbin/iptables"
$IPT -F
$IPT -P INPUT DROP
$IPT -P OUTPUT ACCEPT
$IPT -P FORWARD ACCEPT
$IPT -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
$IPT -A INPUT -s 10.1.1.0/24 -p tcp --dport 22 -j ACCEPT
$IPT -A INPUT -p tcp --dport 80 -j ACCEPT
$IPT -A INPUT -p tcp --dport 21 -j ACCEPT
执行这个脚本后查看添加的规则。
[root@long0000 ~]# bash /usr/local/sbin/iptables.sh
[root@long0000 ~]# iptables -nvL
Chain INPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
30 2000 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT tcp -- * * 10.1.1.0/24 0.0.0.0/0 tcp dpt:22
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:21
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 17 packets, 1580 bytes)
pkts bytes target prot opt in out source destination
案例需求2:禁ping
让本机可以ping通别的机器,但是别的机器ping不通本机。
#首先清空之前的规则,并且允许所有数据包通过INPUT链,必须一条命令执行,否则远程连接会断开
[root@long0000 ~]# iptables -F ; iptables -P INPUT ACCEPT
#丢弃icmp包类型为8的数据包。icmp类型为8的数据包是icmp请求,类型为0的是应答包。
##添加这条规则后,可以本机可以ping通其他ip,但是其他机器ping不通本机了。
[root@long0000 ~]# iptables -A INPUT -p icmp --icmp-type 8 -j DROP
##本机ping网关。
[root@long0000 ~]# ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=63 time=1.42 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=63 time=1.46 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=63 time=1.21 ms
^C
--- 192.168.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 1.216/1.371/1.469/0.115 ms
#其他机器ping本机。
[C:\~]$ ping 10.1.1.25
正在 Ping 10.1.1.25 具有 32 字节的数据:
请求超时。
请求超时。
10.1.1.25 的 Ping 统计信息:
数据包: 已发送 = 2,已接收 = 0,丢失 = 2 (100% 丢失),
##添加下面这条规则后,本机也ping不通其他ip了,注意和前面那条规则只有一字之差。
[root@long0000 ~]# iptables -A INPUT -p icmp --icmp-type 0 -j DROP
[root@long0000 ~]# ping 10.1.1.1
PING 10.1.1.1 (10.1.1.1) 56(84) bytes of data.
^C
--- 10.1.1.1 ping statistics ---
26 packets transmitted, 0 received, 100% packet loss, time 25004ms
关于icmp包类型参考说明:https://blog.csdn.net/noooooorth/article/details/51636482
iptables nat表案例。
案例需求1:网络地址转换
通过一台能上外网的主机,让另一台内网主机也可以上外网。
案例通过VMware虚拟机模拟实现。
案例需要两台主机,一台可以上外网,两块网卡,
另一台不能上外网,一块网卡。
虚拟机设置
可以上外网的主机:
主机名:long0000,关闭系统后设置网卡
网卡1设置如下图:网卡使用桥接或者NAT模式可以连接外网。
网卡2设置:添加一个网络适配器,模式选择LAN区段,LAN区段的作用类似于交换机。
点击 LAN区段(S) 按钮可以添加一个LAN区段,区段名随意填写。
不可连接外网的主机:
主机名:test01,克隆快照快速建立一台主机。
网卡的设置与外网机的网卡2设置一样。需要选择与外网机相同的LAN区段。如下图:
系统网卡配置:
long0000主机网卡配置:
开机后将/etc/sysconfig/network-scripts/ifcfg-ens33配置文件复制一份命名为ifcfg-ens37
[root@long0000 ~]# cd /etc/sysconfig/network-scripts/
[root@long0000 network-scripts]# cp ifcfg-ens33 ifcfg-ens37
[root@long0000 network-scripts]# ls -d ./ifcfg*
./ifcfg-ens33 ./ifcfg-ens37 ./ifcfg-lo
然后修改ifcfg-ens37配置文件如下:
[root@long0000 network-scripts]# cat ifcfg-ens37
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
NAME=ens37
DEVICE=ens37
ONBOOT=yes
IPADDR=192.168.100.1
修改完重启 ens37 网卡
[root@long0000 network-scripts]# ifdown ens37 ; ifup ens37
成功断开设备 'ens37'。
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/14)
[root@long0000 network-scripts]# ifconfig ens37
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.1 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::20c:29ff:fed3:3e4b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:d3:3e:4b txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 121 bytes 19222 (18.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
test01主机网卡配置:
test01主机无法远程连,接所以只能在虚拟机里面操作
首先修改主机名
hostnamectl set-hostname test01
然后退出shell重新登录。
exit
修改ifcfg-ens33文件配置如下图,然后重启网络服务
-
到这里192.168.100.1和192.168.100.100就可以相互ping通了。
但是test01还不能ping通外网网卡10.1.1.25。
iptables规则配置
这里就需要用到NAT表了。如果要实现转发功能还需要修改内核参数。
将long0000主机上 /proc/sys/net/ipv4/ip_forward 文件的内容修改为1 就可以开启路由转发了。
[root@long0000 ~]# echo "1" > /proc/sys/net/ipv4/ip_forward
[root@long0000 ~]# cat /proc/sys/net/ipv4/ip_forward
1
然后在long0000主机上添加iptables规则
[root@long0000 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
再将test01主机网卡的网关设置为192.168.100.1,并设置一个DNS
route add default gw 192.168.100.1 #设置一个默认网关
echo "nameserver 223.5.5.5" > /etc/resolv.conf #添加一个临时的DNS服务器
这样192.168.100.100就可以ping通外网了。
这样设置后,long0000主机就扮演了路由器的角色,但是PC机还是不能直接连接test01主机
案例需求2:端口转发
让PC能远程登录test01
这个需求只需要在案例需求1的基础上,在long0000主机上添加两条规则即可。
添加规则1,将访问10.1.1.25:22222端口的数据包转发给192.168.100.100:22端口
[root@long0000 ~]# iptables -t nat -A PREROUTING -d 10.1.1.25 -p tcp --dport 22222 -j DNAT --to 192.168.100.100:22
添加规则2,将来源于192.168.100.100的数据包的源地址转换为10.1.1.25
[root@long0000 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.1 -j SNAT --to 10.1.1.25
然后使用Xshell连接10.1.1.25:22222就可以间接的远程登录test01主机了。
[C:\~]$ ssh root@10.1.1.25 22222
Connecting to 10.1.1.25:22222...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
Last login: Tue Jun 12 22:51:10 2018
扩展
selinux教程 http://os.51cto.com/art/201209/355490.htm
selinux pdf电子书 http://pan.baidu.com/s/1jGGdExK
iptables应用在一个网段 http://www.aminglinux.com/bbs/thread-177-1-1.html
sant,dnat,masquerade http://www.aminglinux.com/bbs/thread-7255-1-1.html
iptables限制syn速率 http://www.aminglinux.com/bbs/thread-985-1-1.html http://jamyy.us.to/blog/2006/03/206.html