linux服務器默認通過22埠用ssh協議登錄,這種不安全,今天想做限制,即允許部分來源ip連接服務器,
案例目標:通過iptables規則限制對linux服務器的登錄,
處理方法:撰寫為sh腳本,以便多次執行,iptables.sh :
iptables -I INPUT -p tcp --dport 22 -j DROP -m comment --comment "ssh"
# 按ip范圍區間開放
iptables -I INPUT -p tcp -m iprange --src-range 172.18.163.227-172.18.163.232 --dport 22 -j ACCEPT -m comment --comment "ssh"
# 按網段開放
iptables -I INPUT -p tcp -s 10.99.193.0/24 --dport 22 -j ACCEPT -m comment --comment "ssh"
簡要說明:這里默認使用filter表的INPUT鏈,使用-I插入方式,第一條DROP操作順序不能錯,必須是首條,
對于已經插入的規則,可以使用下面的命令進行查看:
iptables -t filter -nvL --line-number |grep ssh
如果后面需要洗掉規則,可以按照下面的方式處理:
iptables -t filter -D INPUT 3
說明一下:這里洗掉iptables規則,指定了filter表的INPUT鏈,避免出錯,
根據上一步查看的規則的行號來洗掉,查看到相應的規則編號之后,最好從最大的編號開始逐條洗掉,
示例:禁止所有型別鏈接,允許特別定來源ip鏈接 iptables-myrules.sh
#! /bin/bash
# author: xiongzaiqiren
# date: 2023-03-20
# usage: sh iptables-myrules.sh
# 設定服務器安全,允許特定來源ip訪問請執行我,
# 每次改完需要執行iptables-save > /etc/iptables-myrules.conf 備份規則哦
#允許ping
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# 拒絕所有鏈接
iptables -P INPUT DROP;
# 指定ip及范圍允許鏈接
iptables -A INPUT -s 10.99.193.243 -p tcp -j ACCEPT
iptables -A INPUT -s 10.90.5.0/24 -p tcp -j ACCEPT
iptables -A INPUT -s 10.99.193.0/24 -p tcp -j ACCEPT
iptables -nvL --line-numbers
#iptables -t filter -D INPUT 3 #表示洗掉filter表中的FORWARD鏈的第一條規則
參考:iptables使用詳解(示例如何屏蔽docker 暴露的埠)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/547420.html
標籤:Linux
