博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
9. iptables 配置
阅读量:4616 次
发布时间:2019-06-09

本文共 1942 字,大约阅读时间需要 6 分钟。

 iptables 配置文件存放位置:

 [root@Demon yum.repos.d]# vim /etc/rc.d/init.d/iptables
 

一、只给 Centos 6.5 打开 22 和 80 端口,并且重启后有效:

1、查看所有 iptables 配置:

[root@Demon yum.repos.d]# iptables -L -n   (当前防火墙是关闭的)
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

2、只允许 80 和 22 端口通过防火墙

# 清除所有规则

[root@Demon opt]# iptables -F
# 只允许 发送的包从 22 端口进入和返回, -A 的意思是添加一条 INPUT 规则, -p 指定为什么协议,--dport 为目标端口
[root@Demon opt]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@Demon opt]# iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

# 允许本机访问本机

[root@Demon opt]# iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
[root@Demon opt]# iptables -A OUTPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

# 允许所有 IP 访问 80 端口

[root@Demon opt]# iptables -A INPUT -p tcp -s 0/0 --dport 80 -j ACCEPT
[root@Demon opt]# iptables -A OUTPUT -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
[root@Demon opt]# iptables -P INPUT DROP
[root@Demon opt]# iptables -P OUTPUT DROP
[root@Demon opt]# iptables -P FORWARD DROP

# 保存配置

[root@Demon opt]# iptables-save > /etc/sysconfig/iptables
[root@Demon opt]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp   
ACCEPT     all  --  localhost            localhost           
ACCEPT     tcp  --  anywhere             anywhere            tcp   
Chain FORWARD (policy DROP)
target     prot opt source               destination         
Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp   state ESTABLISHED 
ACCEPT     all  --  localhost            localhost           
ACCEPT     tcp  --  anywhere             anywhere            tcp   state ESTABLISHED

# 重启电脑后, ping  一下百度:

[root@Demon opt]# ping 
ping: unknown host   

二、关闭 iptables

# 打开防火墙,重启生效

# chkconfig iptables on

# 关闭防火墙,重启生效

# chkconfig iptables off
 

# 打开防火墙,立即生效,重启后不生效

# chkconfig start

 # 关闭防火墙,立即生效,重启后不生效

# chkconfig stop

转载于:https://www.cnblogs.com/kafeibuku/p/5320166.html

你可能感兴趣的文章
420. Strong Password Checker
查看>>
用字节流添加内容至txt中
查看>>
手写算式的识别与运算
查看>>
jquery 1.9 1.8 判断 浏览器(IE11,IE8,IE7,IE6)版本
查看>>
Reporting Services 的一些问题
查看>>
利用Redisson实现分布式锁及其底层原理解析
查看>>
达芬奇的十大经典名画解读
查看>>
case when then else end
查看>>
常用正则
查看>>
小程序丨嵌套循环
查看>>
基础 - arguments
查看>>
Linux的基本命令+深入一点的网址分享
查看>>
(C#) Encoding.
查看>>
BZOJ 2154: Crash的数字表格 [莫比乌斯反演]
查看>>
nrf51 SDK自带例程的解读
查看>>
SESSION技术
查看>>
数据结构(五)之直接插入排序
查看>>
SQL函数——LENGTH()和LENGTHB()
查看>>
vim - manual -个人笔记
查看>>
详解Javascript中prototype属性(推荐)
查看>>