精品丰满熟女一区二区三区_五月天亚洲欧美综合网_亚洲青青青在线观看_国产一区二区精选

  • <menu id="29e66"></menu>

    <bdo id="29e66"><mark id="29e66"><legend id="29e66"></legend></mark></bdo>

  • <pre id="29e66"><tt id="29e66"><rt id="29e66"></rt></tt></pre>

      <label id="29e66"></label><address id="29e66"><mark id="29e66"><strike id="29e66"></strike></mark></address>
      學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 電腦安全 > 防火墻知識 > iptables防火墻如何設(shè)置

      iptables防火墻如何設(shè)置

      時間: 林輝766 分享

      iptables防火墻如何設(shè)置

        iptables防火墻是我們電腦的防線,怎么樣設(shè)置最好呢?下面由學(xué)習(xí)啦小編給你做出詳細(xì)的iptables防火墻設(shè)置方法介紹!希望對你有幫助!

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

        iptables防火墻設(shè)置方一,安裝并啟動防火墻

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

        當(dāng)我們用iptables添加規(guī)則,保存后,這些規(guī)則以文件的形勢存在磁盤上的,以CentOS為例,文件地址是/etc/sysconfig/iptables

        我們可以通過命令的方式去添加,修改,刪除規(guī)則,也可以直接修改/etc/sysconfig/iptables這個文件就行了。

        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è)置,雖然這樣配置安全性高,但同時會拒絕包括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

        iptables防火墻設(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來插入到指定位置

        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

        iptables防火墻設(shè)置方三,查下iptable規(guī)則

        1,查看filter表

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

        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

        四,修改規(guī)則

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

        五,刪除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

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

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

        作用:虛擬化云平臺服務(wù)器網(wǎng)段192.168.1.0/24 通過一臺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主機的內(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)對應(yīng)dns

        2. echo 1 》 /proc/sys/net/ip_forward 在NAT 主機上需要開啟轉(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

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

        Linux系統(tǒng)內(nèi)核內(nèi)建了netfilter防火墻機制。Netfilter(數(shù)據(jù)包過濾機制),所謂的數(shù)據(jù)包過濾,就是分析進(jìn)入主機的網(wǎng)絡(luò)數(shù)據(jù)包,將數(shù)據(jù)包的頭部數(shù)據(jù)提取出來進(jìn)行分析,以決該連接為放行或阻擋的機制。Netfilter提供了iptables這個程序來作為防火墻數(shù)據(jù)包過濾的命令。Netfilter是內(nèi)建的,效率非常高。

        我們可以通過iptables命令來設(shè)置netfilter的過濾機制。

        iptables里有3張表:

        > Filter(過濾器),進(jìn)入Linux本機的數(shù)據(jù)包有關(guān),是默認(rèn)的表。

        > NAT(地址轉(zhuǎn)換),與Linux本機無關(guān),主要與Linux主機后的局域網(wǎng)內(nèi)計算機相關(guān)。

        > Mangle(破壞者),這個表格主要是與特殊的數(shù)據(jù)包的路由標(biāo)志有關(guān)(通常不用涉及到這個表的修改,對這個表的修改破壞性很大,慎改之)。

        每張表里都還有多條鏈:

        Filter:INPUT, OUTPUT, FORWARD

        NAT:PREROUTING, POSTROUTING, OUTPUT

        Mangle:PREROUTING, OUTPUT, INPUT, FORWARD

        iptables命令的使用

        基本格式:iptables [-t table] -CMD chain CRETIRIA -j ACTION

        -t table:3張表中的其中一種filter, nat, mangle,如果沒有指定,默認(rèn)是filter。

        CMD:操作命令。查看、添加、替換、刪除等。

        chain:鏈。指定是對表中的哪條鏈進(jìn)行操作,如filter表中的INPUT鏈。

        CRETIRIA:匹配模式。對要過濾的數(shù)據(jù)包進(jìn)行描述

        ACTION:操作。接受、拒絕、丟棄等。

        查看

        格式:iptables [-t table] -L [-nv]

        修改

        添加

        格式:iptables [-t table] -A chain CRETIRIA -j ACTION

        將新規(guī)則加入到表table(默認(rèn)filter)的chain鏈的最后位置

        插入

        格式:iptables [-t table] -I chain pos CRETIRIA -j ACTION

        將新規(guī)則插入到table表(默認(rèn)filter)chain鏈的pos位置。原來之后的規(guī)則都往后推一位。pos的有效范圍為:1 ~ num+1

        替換

        格式:iptables [-t table] -R chain pos CRETIRIA -j ACTION

        用新規(guī)則替換table表(默認(rèn)filter)chain鏈的pos位置的規(guī)則。pos的有效范圍為:1 ~ num

        刪除

        格式:iptables [-t table] -D chain pos

        刪除table表(默認(rèn)filter)chain鏈的pos位置的規(guī)則。pos的有效范圍為:1 ~ num

        包匹配(CRETIRIA)

        上面沒有介紹CRETIRIA的規(guī)則,在這小節(jié)里詳細(xì)介紹。包匹配就是用于描述需要過濾的數(shù)據(jù)包包頭特殊的字段。

        指定網(wǎng)口:

        -i :數(shù)據(jù)包所進(jìn)入的那個網(wǎng)絡(luò)接口,例如 eth0、lo等,需與INPUT鏈配合

        -o: 數(shù)據(jù)包所傳出的那么網(wǎng)絡(luò)接口,需與OUTPUT鏈配合

        指定協(xié)議:

        -p:tcp, udp, icmp或all

        指定IP網(wǎng)絡(luò):

        -s:來源網(wǎng)絡(luò)??梢允荌P或網(wǎng)絡(luò)

        IP: 192.168.0.100

        網(wǎng)絡(luò): 192.168.0.0/24 或 192.168.0.0/255.255.255.0 均可

        可以在前加 ! 表示取反

        -d:目標(biāo)網(wǎng)格。同 -s

        指定端口:

        --sport:指定來源端口??梢允菃蝹€端口,還可以是連續(xù)的端口,例如:1024:65535。

        --dport:指定目標(biāo)端口。同--sport

        注意:要指定了tcp或udp協(xié)議才會有效。

        指定MAC地址:

        -m mac --mac-source aa:bb:cc:dd:ee:ff

        指定狀態(tài):

        -m state --state STATUS

        STATUS可以是:

        > INVALID,無效包

        > ESTABLISHED,已經(jīng)連接成功的連接狀態(tài)

        > NEW,想要新立連接的數(shù)據(jù)包

        > RELATED,這個數(shù)據(jù)包與主機發(fā)送出去的數(shù)據(jù)包有關(guān),(最常用)

        例如:只要已建立連接或與已發(fā)出請求相關(guān)的數(shù)據(jù)包就予以通過,不合法數(shù)據(jù)包就丟棄

        -m state --state RELATED,ESTABLISHED

        ICMP數(shù)據(jù)比對

        ping操作發(fā)送的是ICMP包,如果不想被ping到,就可以拒絕。

        --icmp-type TYPE

        TYPE如下:

        8 echo-request(請求)

        0 echo-reply(響應(yīng))

        注意:需要與 -p icmp 配合使用。

        操作(ACTION)

        DROP,丟棄

        ACCEPT,接受

        REJECT,拒絕

        LOG,跟蹤記錄,將訪問記錄寫入 /var/log/messages

        保存配置

        將新設(shè)置的規(guī)則保存到文件

        格式:iptables-save [-t table]

        將當(dāng)前的配置保存到 /etc/sysconfig/iptables

        其它

        格式:iptables [-t table] [-FXZ]

        -F :請除所有的已制訂的規(guī)則

        -X :除掉所有用戶“自定義”的chain

        -Z :將所有的統(tǒng)計值清0

        iptables防火墻設(shè)置方法三:

        1、配置好后,注意保存

        /etc/rc.d/init.d/iptables save

        2、或者你直接編輯iptables 的配置文件,編輯好之后 ,保存重啟,這樣重啟后,就不會還原了!

        配置文件位置: /etc/sysconfig/iptables

        看了“iptables防火墻如何設(shè)置 ”文章的還看了:

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

      2.防火墻Firewall設(shè)置方法

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

      4.怎么樣查看centos防火墻狀態(tài)

      5.centos setup 無法配置防火墻怎么辦

      6.CentOS6.5怎么樣設(shè)置

      781304