Linux网络服务_防火墙_iptables设定
2018-02-28 17:20
716 查看
防火墙_iptables设定
1.超级火墙管理
查看软件yum search iptablesyum list iptables-services.x86_64
安装软件yum install iptables -y
关闭普通火墙systemctl stop firewalld
systemctl mask firewalld
systemctl start iptables.service
systemctl enable iptables.service
不解析列出火墙设置iptables -nL //n不解析,L列出
配置文件vim /etc/sysconfig/iptables
刷新,临时清空iptables -F
清空> /etc/sysconfig/iptables
方法二,临时清空后service iptables save保存设定
添加火墙策略设置iptables -A INPUT -j ACCEPT
策略保存service iptables save
【实验】
[root@server103 ~]# yum search iptables
iptables-services.x86_64
[root@server103 ~]# yum list iptables-services.x86_64
[root@server103 ~]# systemctl stop firewalld
[root@server103 ~]# systemctl mask firewalld
ln -s '/dev/null' '/etc/systemd/system/firewalld.service'
[root@server103 ~]# systemctl start iptables.service
[root@server103 ~]# systemctl enable iptables.service
ln -s '/usr/lib/systemd/system/iptables.service' '/etc/systemd/system/basic.target.wants/iptables.service'
[root@server103 ~]# iptables -nL
[root@server103 ~]# vim /etc/sysconfig/iptables
[root@server103 ~]# iptables -F
[root@server103 ~]# iptables -nL
[root@server103 ~]# iptables -F
[root@server103 ~]# iptables -nL //此时为临时,重启动后失效
[root@server103 ~]# systemctl restart iptables.service
[root@server103 ~]# iptables -nL
[root@server103 ~]# > /etc/sysconfig/iptables //清空
[root@server103 ~]# iptables -F
[root@server103 ~]# iptables -nL
[root@server103 ~]# systemctl restart iptables.service
[root@server103 ~]# iptables -nL
添加火墙策略设置
[root@server103 ~]# iptables -A INPUT -j ACCEPT[root@server103 ~]# iptables -nL
[root@server103 ~]# service iptables save//策略保存
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
2.iptables的设定
查看帮助man ipitables-t指定表名filter -s来源sorce -i匹配端口eg:-i lo lo回环接口
-A添加 -d目的地 -o端口
-I插入INPUT -p方式port --dport端口
-D删除 -j动作 ACCEPT|REJECT|DROP
filter所有经过本机内核的数据
nat从本机通过的数据不经过内核
iptables -t filter -A INPUT -i lo -j ACCEPT
iptables -t filter -A INPUT -s 172.25.254.3 -j ACCEPT
【实验】
查看三个表
[root@server103 ~]# iptables -t nat -nL[root@server103 ~]# iptables -t filter -nL
[root@server103 ~]# iptables -t mangle -nL
设置端口设定
[root@server103 ~]# iptables -t filter -A INPUT -i lo -j ACCEPT[root@server103 ~]# iptables -t filter -A INPUT -s 172.25.254.3 -j ACCEPT
[root@server103 ~]# iptables -nL
[root@server103 ~]# iptables -A INPUT -j REJECT
[root@server103 ~]# iptables -D INPUT 1
[root@server103 ~]# iptables -D INPUT 2
[root@desktop203 westos]# ssh root@172.25.254.103//此时无法连接
ssh: connect to host 172.25.254.103 port 22: Connection refused
[root@server103 ~]# iptables -A INPUT -s 172.25.254.203 -p tcp --dport 22 -j ACCEPT
[root@server103 ~]# iptables -nL
ACCEPT tcp -- 172.25.254.203 0.0.0.0/0 tcp dpt:22
插入
[root@server103 ~]# iptables -I INPUT 1 -s 172.25.254.20 -p tcp --dport 22 -j ACCEPT
[root@server103 ~]# iptables -nL
ACCEPT tcp -- 172.25.254.20 0.0.0.0/0 tcp dpt:22
ACCEPT tcp -- 172.25.254.203 0.0.0.0/0 tcp dpt:22
[root@server103 ~]# systemctl start smb
[root@desktop203 westos]# smbclient -L //172.25.254.103
Enter root's password:
Connection to 172.25.254.103 failed (Error NT_STATUS_CONNECTION_REFUSED)
此时需要开启139和445端口
[root@server103 ~]# iptables -I INPUT 1 -s 172.25.254.203 -p tcp --dport 139 -j ACCEPT
[root@server103 ~]# iptables -I INPUT 1 -s 172.25.254.203 -p tcp --dport 445 -j ACCEPT
[root@desktop203 westos]# smbclient -L //172.25.254.103
Enter root's password:
Domain=[WESTOS_SERVER103] OS=[Unix] Server=[Samba 4.1.1]
Sharename Type Comment
--------- ---- -------
DATA Disk local sambadir
SYSTEMCTLDATA Disk /mnt
IPC$ IPC IPC Service (hello world 103)
Domain=[WESTOS_SERVER103] OS=[Unix] Server=[Samba 4.1.1]
Server Comment
--------- -------
Workgroup Master
--------- -------
!!!!!!!!!!!!!!!!
iptables -nL 中是从上往下依次执行,要注意顺序
7.iptables策略设置二
-R修改策略信息-P(大写)更改默认设定Policy,默认只能用ACCEPT和DROP
-N添加
-E更名
-X删除
【实验】
[root@server103 ~]# iptables -R INPUT 2 -s 172.25.254.203 -p tcp --dport 53 -j ACCEPT[root@server103 ~]# iptables -nL
ACCEPT tcp -- 172.25.254.203 0.0.0.0/0 tcp dpt:53
[root@server103 ~]# iptables -P INPUT DROP
[root@server103 ~]# iptables -nL
Chain INPUT (policy DROP)
[root@server103 ~]# iptables -P INPUT ACCEPT
[root@server103 ~]# iptables -t filter -N westos
[root@server103 ~]# iptables -nL
Chain westos (0 references)
target prot opt source destination
[root@server103 ~]# iptables -t filter -E westos WESTOS
[root@server103 ~]# iptables -nL
Chain WESTOS (0 references)
[root@server103 ~]# iptables -t filter -X WESTOS
[root@server103 ~]# iptables -nL
此时的WESTOS消失
8.超级火墙的优化
iptables -Fnetstat -antlupe
-m
iptables -A INPUT -m state --state NEW -i lo -j ACCEPT //允许回环接口接入
iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT //允许ssh接口接入
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT //允许dns接口接入
iptables -A INPUT -m state --state NEW -p tcp --dport 139 -j ACCEPT //允许smb接口接入
iptables -A INPUT -m state --state NEW -p tcp --dport 445 -j ACCEPT //允许smb接口接入
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT //允许http接口接入
iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT //允许https接口接入
iptables -A INPUT ! -s 172.25.254.3 -j ACCEPT //不允许不是3的机器接入,满足3或者满足以上所有才可接入
iptables -nL
service iptables save //保存
//RELATED 以前来过的端口,历史记录存在过的
//ESTABLISHED 正在进行的端口
【实验】
[root@server103 ~]# iptables -F[root@server103 ~]# iptables -nL
[root@server103 ~]# netstat -antlupe
tcp 0 0 172.25.254.103:22 172.25.254.3:40876 ESTABLISHED 0 98920 5720/sshd: root@pts
[root@server103 ~]# iptables -A INPUT -m state --state NEW -i lo -j ACCEPT [root@server103 ~]# iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT
[root@server103 ~]# iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
[root@server103 ~]# iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT
[root@server103 ~]# iptables -A INPUT -m state --state NEW -p tcp --dport 443 -j ACCEPT
[root@server103 ~]# iptables -A INPUT ! -s 172.25.254.3 -j ACCEPT
[root@server103 ~]# iptables -nL
//RELATED 以前来过的端口,历史记录存在过的
//ESTABLISHED 正在进行的端口
[root@server103 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
10.NAT的设置 源地址转换
【准备】
virt-manager虚拟机管理器server加一个网卡,设置为192.168.0.103
desktop单网卡设置为192.168.0.203
检测desktop ping方法
route -n检测网关设置
ifconfig检测ip设置
iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 172.25.254.103
把192.168.0.203发送来的数据包来源转换为172.25.254.103
在desktop加入网关192.168.0.103,重启网络route -n查看
【实验】
[root@server103 ~]# iptables -t nat -nL //查看
Chain POSTROUTING (policy ACCEPT) //表示在路由后
[root@server103 ~]# iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 172.25.254.103 //SNAT源地址转换
[root@server103 ~]# iptables -t nat -nL
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- 0.0.0.0/0 0.0.0.0/0 to:172.25.254.103
在desktop加入网关192.168.0.103,重启网络route -n查看 ping172.25.254.103检测(可通)
ping172.25.254.3不通
------------>原因:内核功能未开启(路由)
[root@server103 ~]# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 0
[root@server103 ~]# vim /etc/sysctl.conf
5 net.ipv4.ip_forward = 1
[root@server103 ~]# sysctl -p
net.ipv4.ip_forward = 1
此时用desktop客户机ping172.25.254.3可通
检测:
用192.168.0.203 ssh root@172.25.254.3
后用w查看访问的数据来源
[root@server103 ~]# iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-dest 192.168.0.203 //-i input 数据入口 数据入口eth1看不到,不能用eth1
[root@server103 ~]# iptables -nL
[root@server103 ~]# iptables -nL -t nat
相关文章推荐
- Linux网络服务_防火墙_firewalld设定
- Linux网络服务_防火墙_tcpwarp的安全设定
- linux系统之网络防火墙(firewalld服务和iptables服务)
- linux命令:iptables、modprobe装载模块、网络防火墙服务
- linux 网络管理 防火墙 iptables
- Linux服务--iptables之nat转发和构建简单的DMZ防火墙
- 【linux基础】22、iptables实现网络防火墙及地址转换
- Linux命令:iptables网络防火墙
- 安装linux下面用来配置网络,防火墙,系统服务等设置的图形小工具Setup
- Linux网络,防火墙,netfilter,iptables语法
- Linux分享之iptables:防火墙以及网络协议基本原理
- 【转】Linux防火墙iptables开放网络端口的方法
- Linux网络服务_防火墙_iscsi设备共享
- iptables 防火墙在网络中的应用及设置ftp服务的外网访问
- linux网络防火墙-iptables配置详解
- Linux的网络防火墙iptables实战
- Linux网络服务_防火墙_守护进程xinted,客户端telnet
- iptables实战系列:公共网络服务防火墙 推荐
- Linux网络防火墙【4】 Linux内核网络 iptables 之filter分析
- Linux防火墙iptables开放网络端口的方法