Centos7 Iptables 白名單

Iptables是Linux操作系統(tǒng)常用的防火墻軟件台舱,用于服務(wù)器的訪問控制颅湘。Centos7默認(rèn)的防火墻是Firewalle适贸。個人理解兩者都是對規(guī)則的配置馆蠕,F(xiàn)irewalle只是在Iptables基礎(chǔ)上進行封裝,讓操作更簡單是尔。內(nèi)核netfilter執(zhí)行規(guī)則殉了。

本教程使用于 RHEL 7 / CentOS 7,首選查看操作系統(tǒng)的版本:
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@localhost ~]#
本次測試用到的虛擬機地址:192.168.56.5;192.168.56.5;192.168.56.6

1 安裝iptables

首先檢查是否安裝iptables拟枚,如果已經(jīng)安裝薪铜,或者版本比Centos7低,請略過安裝Iptables的步驟恩溅,直接看使用和配置隔箍。

(1)檢查是否安裝iptables
[root@localhost ~]# service iptables status
Redirecting to /bin/systemctl status  iptables.service
Unit iptables.service could not be found.
[root@localhost ~]#

(2)安裝前的準(zhǔn)備
#停止firewalle
[root@localhost ~]# systemctl stop firewalld
#禁用firewalld服務(wù)
[root@localhost ~]# systemctl mask firewalld
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@localhost ~]#

1.1 yum安裝

如果目標(biāo)服務(wù)器能夠聯(lián)網(wǎng),可選擇yum的方式安裝脚乡。

#yum安裝iptables
[root@localhost ~]# yum install iptables-services
已加載插件:fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                     | 3.6 kB     00:00
extras                                                   | 3.4 kB     00:00
updates                                                  | 3.4 kB     00:00
(1/4): base/7/x86_64/group_gz                                                                                                                    | 166 kB  00:00:00
(2/4): extras/7/x86_64/primary_db                                                                                                                | 200 kB  00:00:00
(3/4): base/7/x86_64/primary_db                                                                                                                  | 6.0 MB  00:00:02
(4/4):                                                                                                               3/3
……省略中間安裝日志…… 
已安裝:
  iptables-services.x86_64 0:1.4.21-28.el7

作為依賴被升級:
  iptables.x86_64 0:1.4.21-28.el7

完畢蜒滩!
[root@localhost ~]#
(2)安裝后驗證
#查看是否安裝成功
[root@springboot sysconfig]# rpm -qa |grep iptables
iptables-1.4.21-28.el7.x86_64
iptables-services-1.4.21-28.el7.x86_64
[root@springboot sysconfig]#

1.2 手動安裝

如果目標(biāo)服務(wù)器不能聯(lián)網(wǎng),請手工下載安裝包奶稠。下載地址
(1) iptables-1.4.21-28.el7.x86_64下載鏈接:[http://mirrors.163.com/centos/7/os/x86_64/Packages/iptables-1.4.21-28.el7.x86_64.rpm]
(2) iptables-services-1.4.21-28.el7.x86_64.rpm下載鏈接:[http://mirrors.163.com/centos/7/os/x86_64/Packages/iptables-services-1.4.21-28.el7.x86_64.rpm]

(1) 安裝前的操作
#創(chuàng)建目錄
[root@ZabbixServer ~]# cd /usr
[root@ZabbixServer usr]# mkdir iptables
#將本地下載的安裝包上傳到目標(biāo)服務(wù)器
[root@ZabbixServer iptables]# ll
總用量 488
-rw-r--r-- 1 root root 442964 5月  29 18:26 iptables-1.4.21-28.el7.x86_64.rpm
-rw-r--r-- 1 root root  53128 5月  29 18:26 iptables-services-1.4.21-28.el7.x86_64.rpm
#為安裝添加執(zhí)行權(quán)限
[root@ZabbixServer iptables]# chmod +x iptables-1.4.21-28.el7.x86_64.rpm
[root@ZabbixServer iptables]# chmod +x iptables-services-1.4.21-28.el7.x86_64.rpm
(2) 安裝iptables
#開始安裝
[root@ZabbixServer iptables]# rpm -Uvh iptables-1.4.21-28.el7.x86_64.rpm
準(zhǔn)備中...                          ################################# [100%]
正在升級/安裝...
   1:iptables-1.4.21-28.el7           ################################# [ 50%]
正在清理/刪除...
   2:iptables-1.4.21-17.el7           ################################# [100%]
[root@ZabbixServer iptables]# rpm -Uvh iptables-services-1.4.21-28.el7.x86_64.rpm
準(zhǔn)備中...                          ################################# [100%]
正在升級/安裝...
   1:iptables-services-1.4.21-28.el7  ################################# [100%]
(3) 安裝后驗證
#驗證是否安裝成功
[root@ZabbixServer iptables]# rpm -qa |grep iptables
iptables-services-1.4.21-28.el7.x86_64
iptables-1.4.21-28.el7.x86_64
[root@ZabbixServer iptables]#
#查看狀態(tài)俯艰,Active: inactive (dead)為未啟動
[root@ZabbixServer iptables]# service iptables status
Redirecting to /bin/systemctl status  iptables.service
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@ZabbixServer iptables]#

2 啟動iptables

(1) 設(shè)置開機啟動
[root@localhost ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@localhost ~]#

(2)啟動iptables
#可用命令:systemctl [stop|start|restart] iptables
[root@localhost ~]# systemctl start iptables

(3)查看狀態(tài)
#Active: active (exited)表示已經(jīng)啟動
[root@localhost ~]# service iptables status
Redirecting to /bin/systemctl status  iptables.service
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
   Active: active (exited) since 三 2019-05-29 15:06:27 CST; 6s ago
  Process: 2409 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
 Main PID: 2409 (code=exited, status=0/SUCCESS)

5月 29 15:06:27 localhost.localdomain systemd[1]: Starting IPv4 firewall with iptables...
5月 29 15:06:27 localhost.localdomain iptables.init[2409]: iptables: Applying firewall rules: [  確定  ]
5月 29 15:06:27 localhost.localdomain systemd[1]: Started IPv4 firewall with iptables.
[root@localhost ~]#

3 設(shè)置白名單

3.1 查看默認(rèn)規(guī)則

#查看iptables現(xiàn)有規(guī)則
[root@localhost ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0
ACCEPT     all  --  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
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@localhost ~]#

3.2 設(shè)置白名單

iptables配置文件目錄/etc/sysconfig/iptables。由于本次安全加固生產(chǎn)環(huán)境服務(wù)器較多窒典,所以定義腳本蟆炊,執(zhí)行腳本使策略生效。運維的同學(xué)可在目標(biāo)服務(wù)器上做如下操作:
(1) 新建腳本文件瀑志,命令:vi /opt/iptables.sh
(2) 拷貝下面的腳本內(nèi)容到/opt/iptables.sh
(3) 執(zhí)行腳本:sh /opt/iptables.sh
需修改腳本中開放的端口和白名單地址

腳本內(nèi)容:

#腳本名稱:iptables.sh涩搓,腳本內(nèi)容如下:
#!/bin/sh
iptables -P INPUT ACCEPT
#清空所有默認(rèn)規(guī)則
iptables -F
#清空所有自定義規(guī)則
iptables -X
#所有計數(shù)器歸0
iptables -Z
#定義白名單
iptables -N whitelist
#設(shè)置白名單的ip或ip段
iptables -A whitelist -s 192.168.56.3 -j ACCEPT
#iptables -A whitelist -s 192.168.56.0 -j ACCEPT
#重新設(shè)置iptables
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
#只有白名單才能訪問,測試開發(fā)了22劈猪、8010昧甘,其它應(yīng)用根據(jù)需求添加即可。
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j whitelist
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 8010 -j whitelist
iptables -A INPUT -p icmp -j whitelist
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
#配置寫入文件战得,重啟也生效充边。
service iptables save
#重啟iptables服務(wù)
systemctl restart iptables.service

3.3 查看規(guī)則

再次查看配置是否已經(jīng)生效,whitelist為白名單,下面的配置表示icmp浇冰、tcp的22贬媒、8010端口只能白名單中192.168.56.3訪問。

[root@springboot sysconfig]# iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
whitelist  tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
whitelist  tcp  --  0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:8010
whitelist  icmp --  0.0.0.0/0            0.0.0.0/0

Chain FORWARD (policy DROP)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain whitelist (3 references)
target     prot opt source               destination
ACCEPT     all  --  192.168.56.3         0.0.0.0/0
[root@springboot sysconfig]#

4 測試白名單

目標(biāo)機器:192.168.56.5【白名單生效的機器】
源機器1:192.168.56.3【在白名單中】
源機器2:192.168.56.6【不在白名單中】

4.1 測試ICMP

(1)在源機器1上可以ping通肘习,表示icmp配置生效际乘。

PS C:\WINDOWS\system32> ping 192.168.56.5

正在 Ping 192.168.56.5 具有 32 字節(jié)的數(shù)據(jù):
來自 192.168.56.5 的回復(fù): 字節(jié)=32 時間<1ms TTL=64
來自 192.168.56.5 的回復(fù): 字節(jié)=32 時間<1ms TTL=64
來自 192.168.56.5 的回復(fù): 字節(jié)=32 時間<1ms TTL=64

192.168.56.5 的 Ping 統(tǒng)計信息:
    數(shù)據(jù)包: 已發(fā)送 = 3,已接收 = 3漂佩,丟失 = 0 (0% 丟失)脖含,
往返行程的估計時間(以毫秒為單位):
    最短 = 0ms,最長 = 0ms投蝉,平均 = 0ms
Control-C
PS C:\WINDOWS\system32>

(2)在源機器2上不能ping通养葵,表示icmp配置生效。

[root@appos /]# ping 192.168.56.5
PING 192.168.56.5 (192.168.56.5) 56(84) bytes of data.

4.2 測試TCP

目標(biāo)機器192.168.56.5部署了REST測試接口瘩缆、端口是8010关拒。其它應(yīng)用根據(jù)需求添加即可。
(1)在源機器1的訪問接口咳榜,可以返回數(shù)據(jù)夏醉,表示tcp配置生效爽锥。



(2)在源機器2的訪問接口涌韩,不能返回數(shù)據(jù),表示tcp配置生效氯夷。

[root@appos /]# curl 192.168.56.5:8010/speech/test
執(zhí)行后沒有反應(yīng)臣樱。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市腮考,隨后出現(xiàn)的幾起案子雇毫,更是在濱河造成了極大的恐慌,老刑警劉巖踩蔚,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件棚放,死亡現(xiàn)場離奇詭異,居然都是意外死亡馅闽,警方通過查閱死者的電腦和手機飘蚯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來福也,“玉大人局骤,你說我怎么就攤上這事”┐眨” “怎么了峦甩?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長现喳。 經(jīng)常有香客問我凯傲,道長犬辰,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任冰单,我火速辦了婚禮忧风,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘球凰。我一直安慰自己狮腿,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布呕诉。 她就那樣靜靜地躺著缘厢,像睡著了一般。 火紅的嫁衣襯著肌膚如雪甩挫。 梳的紋絲不亂的頭發(fā)上贴硫,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天,我揣著相機與錄音伊者,去河邊找鬼英遭。 笑死,一個胖子當(dāng)著我的面吹牛亦渗,可吹牛的內(nèi)容都是我干的挖诸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼法精,長吁一口氣:“原來是場噩夢啊……” “哼多律!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起搂蜓,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤狼荞,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后帮碰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體相味,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年殉挽,在試婚紗的時候發(fā)現(xiàn)自己被綠了丰涉。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡此再,死狀恐怖昔搂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情输拇,我是刑警寧澤摘符,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響逛裤,放射性物質(zhì)發(fā)生泄漏瘩绒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一带族、第九天 我趴在偏房一處隱蔽的房頂上張望锁荔。 院中可真熱鬧,春花似錦蝙砌、人聲如沸阳堕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽恬总。三九已至,卻和暖如春肚邢,著一層夾襖步出監(jiān)牢的瞬間壹堰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工骡湖, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留贱纠,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓响蕴,卻偏偏與公主長得像谆焊,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子换途,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,941評論 2 355