學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 電腦安全 > 防火墻知識(shí) > linux防火墻怎么樣設(shè)置

linux防火墻怎么樣設(shè)置

時(shí)間: 林輝766 分享

linux防火墻怎么樣設(shè)置

  linux系統(tǒng)是很容易受到病毒侵害的,那么linux防火墻要怎么樣去設(shè)置呢?下面由學(xué)習(xí)啦小編給你做出詳細(xì)的linux防火墻設(shè)置方法介紹!希望對(duì)你有幫助!

  linux防火墻設(shè)置方法一:

  linux防火墻設(shè)置一:安裝并啟動(dòng)防火墻

  [root@linux ~]# /etc/init.d/iptables start

  當(dāng)我們用iptables添加規(guī)則,保存后,這些規(guī)則以文件的形勢(shì)存在磁盤上的,以CentOS為例,文件地址是/etc/sysconfig/iptables,我們可以通過命令的方式去添加,修改,刪除規(guī)則,也可以直接修改/etc/sysconfig/iptables這個(gè)文件就行了。

  1.加載模塊

  /sbin/modprobe ip_tables

  2.查看規(guī)則

  iptables -L -n -v

  3.設(shè)置規(guī)則

  #清除已經(jīng)存在的規(guī)則

  iptables -F

  iptables -X

  iptables -Z

  #默認(rèn)拒絕策略(盡量不要這樣設(shè)置,雖然這樣配置安全性高,但同時(shí)會(huì)拒絕包括lo環(huán)路在內(nèi)的所#有網(wǎng)絡(luò)接口,導(dǎo)致出現(xiàn)其他問題。建議只在外網(wǎng)接口上做相應(yīng)的配置)

  iptables -P INPUT DROP

  iptables -P OUTPUT DROP

  iptables -P FORWARD DROP

  #ssh 規(guī)則

  iptables -t filter -A INPUT -i eth0 -p tcp –dport 22 -j ACCEPT

  iptables -t filter -A OUTPUT -o eth0 -p tcp –sport 22 -j ACCEPT

  #本地還回及tcp握手處理

  iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

  iptables -A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT

  #www-dns 規(guī)則

  iptables -I INPUT -p tcp –sport 53 -j ACCEPT

  iptables -I INPUT -p udp –sport 53 -j ACCEPT

  iptables -t filter -A INPUT -i eth0 -p tcp –dport 80 -j ACCEPT

  iptables -t filter -A OUTPUT -o eth0 -p tcp –sport 80 -j ACCEPT

  #ICMP 規(guī)則

  iptables -A INPUT -p icmp –icmp-type echo-request-j ACCEPT

  iptables -A INPUT -p icmp –icmp-type echo-reply -j ACCEPT

  iptables -A OUTPUT -p icmp –icmp-type echo-request -j ACCEPT

  iptables -A OUTPUT -p icmp –icmp-type echo-reply -j ACCEPT

  linux防火墻設(shè)置二:添加防火墻規(guī)則

  1,添加filter表

  1.[root@linux ~]# iptables -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT //開放21端口

  出口我都是開放的iptables -P OUTPUT ACCEPT,所以出口就沒必要在去開放端口了。

  2,添加nat表

  1.[root@linux ~]# iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j MASQUERADE

  將源地址是 192.168.10.0/24 的數(shù)據(jù)包進(jìn)行地址偽裝

  3,-A默認(rèn)是插入到尾部的,可以-I來(lái)插入到指定位置

  1.[root@linux ~]# iptables -I INPUT 3 -p tcp -m tcp --dport 20 -j ACCEPT

  2.[root@linux ~]# iptables -L -n --line-number

  3.Chain INPUT (policy DROP)

  4.num target prot opt source destination

  5.1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0

  6.2 DROP icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 8

  7.3 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:20 //-I指定位置插的

  8.4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22

  9.5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80

  10.6 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED

  11.7 DROP all -- 0.0.0.0/0 0.0.0.0/0 state INVALID,NEW

  12.8 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:21 //-A默認(rèn)插到最后

  13.Chain FORWARD (policy ACCEPT)

  14.num target prot opt source destination

  15.Chain OUTPUT (policy ACCEPT)

  16.num target prot opt source destination

  linux防火墻設(shè)置三:查下iptable規(guī)則

  1,查看filter表

  1.[root@linux ~]# iptables -L -n --line-number |grep 21 //--line-number可以顯示規(guī)則序號(hào),在刪除的時(shí)候比較方便

  2.5 ACCEPT tcp -- 192.168.1.0/24 0.0.0.0/0 tcp dpt:21

  如果不加-t的話,默認(rèn)就是filter表,查看,添加,刪除都是的

  2,查看nat表

  1.[root@linux ~]# iptables -t nat -vnL POSTROUTING --line-number

  2.Chain POSTROUTING (policy ACCEPT 38 packets, 2297 bytes)

  3.num pkts bytes target prot opt in out source destination

  4.1 0 0 MASQUERADE all -- * * 192.168.10.0/24 0.0.0.0/0

  linux防火墻設(shè)置四:修改規(guī)則

  1.[root@linux ~]# iptables -R INPUT 3 -j DROP //將規(guī)則3改成DROP

  linux防火墻設(shè)置五:刪除iptables規(guī)則

  1.[root@linux ~]# iptables -D INPUT 3 //刪除input的第3條規(guī)則

  2.[root@linux ~]# iptables -t nat -D POSTROUTING 1 //刪除nat表中postrouting的第一條規(guī)則

  3.[root@linux ~]# iptables -F INPUT //清空 filter表INPUT所有規(guī)則

  4.[root@linux ~]# iptables -F //清空所有規(guī)則

  5.[root@linux ~]# iptables -t nat -F POSTROUTING //清空nat表POSTROUTING所有規(guī)則

  六,設(shè)置默認(rèn)規(guī)則

  1.[root@linux ~]# iptables -P INPUT DROP //設(shè)置filter表INPUT默認(rèn)規(guī)則是 DROP

  所有添加,刪除,修改后都要保存起來(lái),/etc/init.d/iptables save.上面只是一些最基本的操作,要想靈活運(yùn)用,還要一定時(shí)間的實(shí)際操作。

  iptables配置常規(guī)映射及軟路由

  作用:虛擬化云平臺(tái)服務(wù)器網(wǎng)段192.168.1.0/24 通過一臺(tái)linux服務(wù)器(eth0:192.168.1.1、eth1:10.0.0.5)做軟路由達(dá)到訪問10.0.0.5能訪問的網(wǎng)絡(luò)范圍,并且通過iptables的NAT映射提供服務(wù)。

  NAT 映射網(wǎng)絡(luò)端口:

  效果: 10.0.0.5:2222 —-》 192.168.1.2:22

  命令:iptable -t nat -A PREROUTING -D 10.0.0.5 -p tcp –dport 2222 -j DNAT –to-destination 192.168.1.2:22

  service iptables save

  service iptables restart

  注意:1.在192.168.1.2的網(wǎng)絡(luò)配置上需要將NAT主機(jī)的內(nèi)網(wǎng)ip即192.168.1.1作為默認(rèn)網(wǎng)關(guān),如果10.0.0.5具有公網(wǎng)訪問權(quán)限,dns則設(shè)置成公網(wǎng)對(duì)應(yīng)dns

  2. echo 1 》 /proc/sys/net/ip_forward 在NAT 主機(jī)上需要開啟轉(zhuǎn)發(fā)才能生效

  軟路由192.168.1.0/24通過10.0.0.5訪問外網(wǎng):

  命令:iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT –to-source 10.0.0.5

  service iptables save

  service iptables restart

  linux防火墻設(shè)置方法二:

  以mysql服的3306端口為例。

  1、直接打開端口:

  iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

  2、永久打開某端口

  首先,用vim打開防火墻配置文件:

  vim /etc/sysconfig/iptables

  然后,在iptables文件內(nèi)容中加入如下內(nèi)容:

  -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

  最后,保存配置文件后,執(zhí)行如下命令重啟防火墻:

  service iptables restart

  

  看了“linux防火墻怎么樣設(shè)置 ”文章的還看了:

1.Linux操作系統(tǒng)下如何設(shè)置防火墻

2.如何在RedHat Linux設(shè)置防火墻

3.如何關(guān)閉linux的防火墻

4.LINUX防火墻常用操作

5.如何檢查linux防火墻是否開啟

6.ftp防火墻怎么樣設(shè)置linux

7.Linux防火墻怎么開放特定端口

787381