【原创】Linux基础之iptables
iptables 1.4.21

官方:https://www.netfilter.org/projects/iptables/index.html
iptables is the userspace command line program used to configure the Linux 2.4.x and later packet filtering ruleset. It is targeted towards system administrators.
iptables是一个命令行工具,与netfilter一起组成linux服务器的防火墙,通过iptables可以设置管理各种ip包过滤规则;
查看当前配置,以下为初始配置:
# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destinationChain FORWARD (policy ACCEPT)
target prot opt source destinationChain OUTPUT (policy ACCEPT)
target prot opt source destination
policy有两种,一种是ACCEPT(默认放开,需要加黑名单,初始配置为全部放开),一种是DROP(默认拒绝,需要加白名单),常用的是后一种
服务器常见的策略是放开内网访问,限制外网访问:
#允许内网和本机访问
iptables -A INPUT -p tcp -s 192.168.0.0/24 -j ACCEPT
iptables -A INPUT -p tcp -s 127.0.0.1 -j ACCEPT#允许ssh登录
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#允许访问dns、curl外网等
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT#允许访问80端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#允许ping
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
#允许keepalived
iptables -I INPUT -d 224.0.0.0/8 -j ACCEPT
iptables -A INPUT -p vrrp -j ACCEPTiptables -P INPUT DROP
注意在执行最后一句之前,一定要先执行各种ACCEPT,否则执行之后服务器直接远程直接登录不了;
策略生效之后是这样的:
# iptables -nL
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 192.168.0.0/24 0.0.0.0/0
ACCEPT tcp -- 127.0.0.1 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp spt:53
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHEDChain FORWARD (policy ACCEPT)
target prot opt source destinationChain OUTPUT (policy ACCEPT)
target prot opt source destination
如果想删除某条规则,增加--line-number
# iptables -nL --line-number
Chain INPUT (policy DROP)
num target prot opt source destination
1 ACCEPT tcp -- 192.168.0.0/24 0.0.0.0/0
2 ACCEPT tcp -- 127.0.0.1 0.0.0.0/0
3 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
5 ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp spt:53
6 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHEDChain FORWARD (policy ACCEPT)
num target prot opt source destinationChain OUTPUT (policy ACCEPT)
num target prot opt source destination
然后指定行号删除
# iptables -D INPUT $line
【原创】Linux基础之iptables的更多相关文章
- Linux基础精华
Linux基础精华 (继续跟新中...) 常用命令: Linux shell 环境 让你提升命令行效 率的 Bash 快捷键 [完整版] 设置你自己的liux alias Linux的Find使用 L ...
- linux基础简答(1)
linux基础简答题 扇区及其4个主分区的原因 在第一个扇区中,保存着引导记录和分区信息,容量为512bytes,主引导记录(相当于MBR)446 bytes,分区表64bytes,记录每个分区信息要 ...
- Linux基础-最基础
Linux基础 为了更好的学习知识,开通此博客,以前博客丢了...记录一下知识点,希望能在这里与大家互相学习交流. 20171113 14:00 Linux基础-基本知识 Linux树状文件系统结构 ...
- Linux基础知识梳理
Linux基础知识梳理 Linux内核最初只是由芬兰人林纳斯?托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的.Linux是一套免费使用和自由传播的类Unix操作系统,是 ...
- Linux基础 - 系统优化及常用命令
目录 Linux基础系统优化及常用命令 Linux基础系统优化 网卡配置文件详解 ifup,ifdown命令 ifconfig命令 ifup,ifdown命令 ip命令 用户管理与文件权限篇 创建普通 ...
- Linux基础系统优化及常用命令
# Linux基础系统优化及常用命令 [TOC] ## Linux基础系统优化 Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. - ...
- Linux基础命令和NAT技术
yum yellowdog updater,modified是一种用python写的基于rpm的管理工具 用于解决rpm包的依赖性 要安装编译工具 yum install gcc 库函数:静态库 ...
- Linux基础入门教程
Linux基础入门教程 --------- Linux学习路径 Linux学习者,常常不知道自己改怎么学习linux:Linux初级,也就是入门linux前提是需要有一些计算机硬件相关的知识或是有一下 ...
- Linux基础命令-Nginx-正则表达式( grep sed awk )-Shell Script--etc
Linux基础使用 学习内容博客 内存 查看swap分区信息 > swapon -s 添加swap分区 > mkswap /dev/sdb2 > 激活 swapon -a /dev/ ...
随机推荐
- DependencyInjection源码解读之ServiceProvider
var services = new ServiceCollection(); var _serviceProvider = services.BuildServiceProvider(); serv ...
- UE、UI、UCD、UED?职责划分?
UE.UI.UCD.UED?你知道你是干啥的吗 名词 UE (User Experience) : 用户体验 UI (User Interface) : 用户界面 UCD (User-Centered ...
- 292. Nim Game(easy)
You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...
- PTA 天梯赛练习 7-11 玩转二叉树-二叉树重建
以前就思考过这个问题,但是没有深入的想过,这是一种叫二叉树重建的典型题目 如果给出中序和前序,求出后序遍历. 这道题则求的是交换儿子节点的层序遍历. 二叉树的重建应该怎么重建,首先我们知道,先根遍历, ...
- java list map set array 转换
1.list转set Set set = new HashSet(new ArrayList()); 2.set转list List list = new ArrayList(new HashSet( ...
- git 版本管控 发布
https://www.cnblogs.com/charlesblc/p/6051569.html http://www.ruanyifeng.com/blog/2012/07/git.html 1. ...
- Git里有些费解的术语和设计
关于暂存区, 好几个地方都写到了 正在编辑的文件 --> Unchacked/Modified, 而Unchacked/Modified, 的状态也可以叫 to be committed . 这 ...
- python常用函数用法整理
1,zeros函数(同理的还有ones函数) http://www.jb51.net/article/127649.htm 注意: (m,n)是生成m行n列的矩阵,但要生成二维矩阵的时候要用两层括号, ...
- Spring MVC 使用介绍(七)—— 注解式控制器(三):生产者与消费者模型
一.MIME类型 MIME类型格式:type/subtype(;parameter)? type:主类型,任意的字符串,如text,如果是*号代表所有 subtype:子类型,任意的字符串,如html ...
- 满汉全席[2-SAT]
题面 对不起我又写了一个板题qvq 和洛谷那道模板题没区别...两样菜至少做一样即可 不过注意define和函数的区别!!! #include <cmath> #include <c ...