linux中VPS 上配置 iptables 防火牆方法

來源:互聯網
上載者:User

linux中VPS 上配置 iptables 防火牆,只有linux中的apache才可以使用這個功能,在windows中不具體此功能哦。

ssh都登入不上去,上去很艱難的查了日誌,發現幾個ip訪問異常,應該是某外掛程式的爬蟲,之前把圖簡單iptables禁用了,只能再次開啟,主要命令如下:

 代碼如下 複製代碼

# 查看狀態
service iptables status
 
# 查看規則
iptables -L -n
 
# 清除預設規則
iptables -F
iptables -X
iptables -Z
 
#####建立新的規則######
# 允許本地迴環 127.0.0.1
iptables -A INPUT -i lo -p all -j ACCEPT
 
# 允許已經建立的所有串連
iptables -A INPUT -p all -m state --state ESTABLISHED,RELATED -j ACCEPT
 
# 允許所有向外發起的串連
iptables -A OUTPUT -j ACCEPT
 
# 拒絕 ping
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j REJECT
 
# 允許 SSH 服務連接埠(一定要開啟,不然就不能ssh了)
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
 
# 允許 Web 服務連接埠
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
 
# 拒絕其他所有未被允許的串連
iptables -A INPUT -j REJECT
iptables -A FORWARD -j REJECT
 
# 禁用ip
iptables -I INPUT -s 124.115.0.199 -j DROP
# 封IP段的命令是
iptables -I INPUT -s 124.115.0.0/16 -j DROP
# 封整個段的命令是
iptables -I INPUT -s 194.42.0.0/8 -j DROP
# 封幾個段的命令是
iptables -I INPUT -s 61.37.80.0/24 -j DROP
iptables -I INPUT -s 61.37.81.0/24 -j DROPdport表示目的,sport表示來源,output表示本機出,input表示訪問本機

然後就是儲存和開機啟動centos

 代碼如下 複製代碼

service iptables save # 儲存
service iptables restart # 重啟
chkconfig iptables on # 確認服務會隨開機自動啟動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.