轉載 阿里雲centOS防火牆配置

雖然說阿里雲推出了雲盾服務,可是本身再加一層防火牆總歸是更安全些,下面是我在阿里雲vps上配置防火牆的過程,目前只配置INPUT。OUTPUT和FORWORD都是ACCEPT的規則mysql

1、檢查iptables服務狀態

首先檢查iptables服務的狀態web

1
2
[root@woxplife ~] # service iptables status
iptables: Firewall is not running.

說明iptables服務是有安裝的,可是沒有啓動服務。
若是沒有安裝的話能夠直接yum安裝sql

1
yum  install  -y iptables

啓動iptablestomcat

1
2
[root@woxplife ~] # service iptables start
iptables: Applying firewall rules:                         [  OK  ]

看一下當前iptables的配置狀況安全

1
[root@woxplife ~] # iptables -L -n

2、清除默認的防火牆規則

1
2
3
4
5
6
7
8
9
10
11
12
#首先在清除前要將policy INPUT改爲ACCEPT,表示接受一切請求。
#這個必定要先作,否則清空後可能會悲劇
iptables -P INPUT ACCEPT
 
#清空默認全部規則
iptables -F
 
#清空自定義的全部規則
iptables -X
 
#計數器置0
iptables -Z

3、配置規則

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#容許來自於lo接口的數據包
#若是沒有此規則,你將不能經過127.0.0.1訪問本地服務,例如ping 127.0.0.1
iptables -A INPUT -i lo -j ACCEPT
 
#ssh端口22
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
 
#FTP端口21
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
 
#web服務端口80
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
 
#tomcat
iptables -A INPUT -p tcp --dport xxxx -j ACCEPT
 
#mysql
iptables -A INPUT -p tcp --dport xxxx -j ACCEPT
 
#容許icmp包經過,也就是容許ping
iptables -A INPUT -p icmp -m icmp --icmp- type  8 -j ACCEPT
 
#容許全部對外請求的返回包
#本機對外請求至關於OUTPUT,對於返回數據包必須接收啊,這至關於INPUT了
iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
 
#若是要添加內網ip信任(接受其全部TCP請求)
iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT
 
#過濾全部非以上規則的請求
iptables -P INPUT DROP

#要封停一個IP,使用下面這條命令:bash

iptables -I INPUT -s ***.***.***.*** -j DROP服務器

#要解封一個IP,使用下面這條命令:ssh

iptables -D INPUT -s ***.***.***.*** -j DROPtcp

4、保存

首先iptables -L -n看一下配置是否正確。
沒問題後,先不要急着保存,由於沒保存只是當前有效,重啓後就不生效,這樣萬一有什麼問題,能夠後臺強制重啓服務器恢復設置。
另外開一個ssh鏈接,確保能夠登錄。阿里雲

確保沒問題以後保存

1
2
3
4
5
#保存
[root@woxplife ~] # service iptables save
 
#添加到自啓動chkconfig
[root@woxplife ~] # chkconfig iptables on

修改防火牆端口:修改/etc/sysconfig/iptables 文件

相關文章
相關標籤/搜索