日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

Linux 防火墙:Netfilter iptables

發(fā)布時(shí)間:2023/12/13 linux 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux 防火墙:Netfilter iptables 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

?

一、Netfilter 簡(jiǎn)介

(1)?Netfilter 是 Linux 內(nèi)置的一種防火墻機(jī)制,我們一般也稱之為數(shù)據(jù)包過(guò)濾機(jī)制,而 iptables 只是操作 netfilter 的一個(gè)命令行工具
(2) Netfilter 是 Linux CentOS 6 內(nèi)置的防火墻機(jī)制,Firewall 是 Linux CentOS 7 內(nèi)置的防火墻機(jī)制,如果想在 CentOS 7 中使用 netfilter 而不是 firewall,操作如下

?

[root@MongoDB ~]# systemctl stop firewalld.service // 停止firewalld [root@MongoDB ~]# systemctl disable firewalld.service // 禁止firewall開機(jī)啟動(dòng) [root@MongoDB ~]# yum install iptables-services -y [root@MongoDB ~]# systemctl start iptables.service //啟動(dòng)防火墻 [root@MongoDB ~]# systemctl enable iptables.service // 設(shè)置防火墻開機(jī)啟動(dòng)

?

?

?

?

?

centos7 iptables服務(wù)

1.查看iptables服務(wù)狀態(tài)

[root@MongoDB ~]#systemctl status iptables

?

?

systemctl start iptables // 啟動(dòng)iptables服務(wù) systemctl restart iptables // 重啟iptables服務(wù) systemctl stop iptables // 關(guān)閉iptables服務(wù)

?

centos6 iptables 服務(wù)

service iptables start // 啟動(dòng) iptables服務(wù) service iptables restart // 重啟iptables服務(wù) service iptables stop // 關(guān)閉iptables服務(wù) service iptables status // 查看iptables服務(wù)狀態(tài)

?

/etc/init.d/iptables stop // 關(guān)閉iptables服務(wù) /etc/init.d/iptables status // 查看iptables狀態(tài)

?

2.查看規(guī)則,默認(rèn)查看filter表的規(guī)則 iptables -nvL
針對(duì)INPUT鏈 默認(rèn)規(guī)則ACCEPT通過(guò)
iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) // 針對(duì)INPUT鏈 默認(rèn)規(guī)則 // INPUT鏈 規(guī)則pkts bytes target prot opt in out source destination 7589 858K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 42 2520 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:220 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:27017 30806 3205K REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited// FORWARD鏈 默認(rèn)規(guī)則 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// OUTPUT鏈 默認(rèn)規(guī)則 Chain OUTPUT (policy ACCEPT 6732 packets, 950K bytes)pkts bytes target prot opt in out source destination

?

二、iptables 常見用法:

iptables -F # 清空規(guī)則,默認(rèn)清空f(shuō)ilter表的規(guī)則 iptables -X # 刪除用戶自定義規(guī)則 iptables -Z # 清空鏈的計(jì)數(shù)器 service iptables save # 保存規(guī)則,會(huì)把當(dāng)前規(guī)則保存到/etc/sysconfig/iptables iptables-save > my.ipt # 備份規(guī)則,這里指定備份到my.ipt文件 iptables-restore < my.ipt # 恢復(fù)規(guī)則,這里指定使用my.ipt文件進(jìn)行恢復(fù)

?

?

清空所有的規(guī)則,只留下默認(rèn)規(guī)則

iptables -F??清空規(guī)則,默認(rèn)清空f(shuō)ilter表的規(guī)則

只留下默認(rèn)規(guī)則 默認(rèn)規(guī)則都是ACCEPT動(dòng)作

[root@MongoDB ~]# iptables -F [root@MongoDB ~]# iptables -nvL Chain INPUT (policy ACCEPT 6 packets, 480 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 6 packets, 528 bytes)pkts bytes target prot opt in out source destination

iptables -X? 刪除用戶定義規(guī)則

iptables -Z 清空鏈的計(jì)數(shù)器

?

?

iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 放通80端口 iptables -A INPUT -p tcp --dport 22 -j DROP # 禁用22端口 iptables -A INPUT -p tcp -m multiport --dport 80,843,443 -j ACCEPT # 放通多個(gè)端口 iptables -A INPUT -s 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT # 只允許某個(gè)IP訪問(wèn)22端口 iptables -A INPUT -s 192.168.1.1/32 -p tcp -m multiport --dport 3873,4507,3306 -j ACCEPT # 允許某個(gè)IP訪問(wèn)多個(gè)端口

?

?添加一條規(guī)則 到filter表 允許8080端口

[root@MongoDB ~]# iptables -t filter -A INPUT -p tcp --dport 8080 -j ACCEPT

刪除一條規(guī)則

先執(zhí)行 iptables -nvL --line-numbers 查看規(guī)則的序列號(hào) [root@MongoDB ~]# iptables -nvL --line-numbers Chain INPUT (policy ACCEPT 93 packets, 7775 bytes) num pkts bytes target prot opt in out source destination 1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 59 packets, 6900 bytes) num pkts bytes target prot opt in out source destination

-D 刪除規(guī)則

然后執(zhí)行 iptables -D INPUT <number> 刪除規(guī)則
[root@MongoDB ~]# iptables -D INPUT 1 [root@MongoDB ~]# [root@MongoDB ~]# iptables -nvL --line-numbers Chain INPUT (policy ACCEPT 14 packets, 1020 bytes) num pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 6 packets, 728 bytes) num pkts bytes target prot opt in out source destination

?

?
-A # 添加規(guī)則,默認(rèn)添加到最后一條規(guī)則 -I # 插入規(guī)則,默認(rèn)插入到第一條規(guī)則 -D # 刪除規(guī)則,先執(zhí)行 iptables -nvL --line-numbers 查看規(guī)則的序列號(hào),然后執(zhí)行 iptables -D INPUT <number> 刪除規(guī)則 -n # 數(shù)字 -s # 用于指定源IP地址,用法如:iptables -A INPUT -s 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT -d # 用于指定目標(biāo)IP地址,用法如:iptables -A INPUT -d 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT -p # 用于指定檢查哪個(gè)協(xié)議,用法如:iptables -A INPUT -p tcp --dport 80 -j ACCEPT -i # 用于指定入口網(wǎng)卡,用法如:iptables -A INPUT -i eth0 -j ACCEPT -o # 用于指定出口網(wǎng)卡,用法如:iptables -A FORWARD -o eth0 -j ACCEPT -j # 用于指定要進(jìn)行的處理動(dòng)作,ACCEPT表示接受數(shù)據(jù)包進(jìn)來(lái) DROP直接丟棄數(shù)據(jù)包 用法如:iptables -A INPUT -p tcp --dport 80 -j ACCEPT -P # 用于設(shè)置默認(rèn)規(guī)則,用法如:iptables -P INPUT DROP -t # 用于指定操作哪張表,用法如:iptables -t nat -nvL , iptables -t filter ,如果沒有指定默認(rèn)是filter表 -Z # 用于清空計(jì)數(shù)器,用法如:iptables -Z -L # 用于列出規(guī)則鏈中的所有規(guī)則 --sport # 用于指定源端口,用法如:iptables -A INPUT -p tcp --sport 1234 -j ACCEPT --dport # 用于指定目標(biāo)端口,用法如:iptables -A INPUT -s 192.168.1.1/32 -p tcp --dport 22 -j ACCEPT ! # 取反

?

?

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/mingerlcm/p/10685450.html

總結(jié)

以上是生活随笔為你收集整理的Linux 防火墙:Netfilter iptables的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。