Linux網(wǎng)絡(luò)相關(guān)缀辩、firewalld和netfilter、netfilter5表5鏈介紹踪央、iptables語法

目錄

一臀玄、Linux網(wǎng)絡(luò)相關(guān)
二、firewalld和netfilter
三畅蹂、netfilter5表5鏈介紹
四健无、iptables語法

一、Linux網(wǎng)絡(luò)相關(guān)

  • ifconfig 命令查看網(wǎng)卡 IP
[root@minglinux-01 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.89.130  netmask 255.255.255.0  broadcast 192.168.89.255
        inet6 fe80::20c:29ff:fe46:125  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:46:01:25  txqueuelen 1000  (Ethernet)
        RX packets 38830  bytes 36052844 (34.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 13961  bytes 2437860 (2.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 修改配置文件/etc/sysconfig/network-scripts/ifcfg-ens33設(shè)置IP

  • ifdown ens33; ifup ens33
    ifdown即停用網(wǎng)卡液斜,ifup即啟動網(wǎng)卡累贤。遠(yuǎn)程連接服務(wù)器時停用網(wǎng)卡后會導(dǎo)致斷連,所以重啟網(wǎng)卡盡量使用命令systemctl restart network少漆。

  • 添加虛擬網(wǎng)卡
    在Linux系統(tǒng)中臼膏,網(wǎng)卡是可以設(shè)定多重IP的,設(shè)置的過程如下:

[root@minglinux-01 ~]# cd /etc/sysconfig/network-scripts/
[root@minglinux-01 ~]# cd /etc/sysconfig/network-scripts/

編輯ifcfg-ens33:1這個配置文件示损。修改一下NAME渗磅、DEVICE、IPADDR检访,設(shè)置完畢重啟網(wǎng)卡始鱼,如下所示:

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33:
UUID=c0f01c3c-028a-4219-86db-bc442037da9b
DEVICE=ens33:
ONBOOT=yes
IPADDR=192.168.89.139
GATEWAY=192.168.89.2
NETMASK=255.255.255.0
BROADCAST=192.168.89.255
DNS1=119.29.29.29

重啟網(wǎng)卡后再查看網(wǎng)卡IP

systemctl restart network
[root@minglinux-01 network-scripts]# ifconfig 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.89.130  netmask 255.255.255.0  broadcast 192.168.89.255
        inet6 fe80::20c:29ff:fe46:125  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:46:01:25  txqueuelen 1000  (Ethernet)
        RX packets 40070  bytes 36144711 (34.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14378  bytes 2487404 (2.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.89.139  netmask 255.255.255.0  broadcast 192.168.89.255
        ether 00:0c:29:46:01:25  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 查看網(wǎng)卡連接狀態(tài)
  1. mii-tool ens33命令
[root@minglinux-01 ~]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok

這里顯示link ok,就說明網(wǎng)卡為連接狀態(tài)脆贵。如果顯示no link医清,說明網(wǎng)卡壞了或者沒有連接網(wǎng)線。

  1. ethtool ens33命令
[root@minglinux-01 ~]# ethtool ens33
Settings for ens33:
    Supported ports: [ TP ]
    Supported link modes:   10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Supported pause frame use: No
    Supports auto-negotiation: Yes
    Supported FEC modes: Not reported
    Advertised link modes:  10baseT/Half 10baseT/Full 
                            100baseT/Half 100baseT/Full 
                            1000baseT/Full 
    Advertised pause frame use: No
    Advertised auto-negotiation: Yes
    Advertised FEC modes: Not reported
    Speed: 1000Mb/s
    Duplex: Full
    Port: Twisted Pair
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: on
    MDI-X: off (auto)
    Supports Wake-on: d
    Wake-on: d
    Current message level: 0x00000007 (7)
                   drv probe link
    Link detected: yes

如果網(wǎng)卡沒有連接卖氨,最后面一行Link detected顯示為no会烙。

  • 更改主機名
    使用hostname命令可以查看主機名:
[root@minglinux-01 network-scripts]# hostname
minglinux-01

使用hostname命令可以更改主機名,不過這樣的修改保存在內(nèi)存中筒捺,如果重啟持搜,主機名還會變成改動之前的名稱。

使用hostnamectl set-hostname aminglinux-0更改主機名的同時還需要更改相關(guān)
的配置文件/etc/hostname焙矛,這樣可以永久修改葫盼,重啟不影響,實力命令如下:

[root@minglinux-01 network-scripts]# hostnamectl set-hostname minglinux-0
[root@minglinux-01 network-scripts]# hostname
minglinux-0
[root@minglinux-01 network-scripts]# cat /etc/hostname 
minglinux-0
  • 設(shè)置 DNS
    在Linux下設(shè)置DNS非常簡單村斟,只要把DNS地址寫到配置文件/etc/resolv.conf中即可贫导。
[root@minglinux-01 network-scripts]# cat /etc/resolv.conf

nameserver 119.29.29.29

如果只是臨時修改DNS IP地址抛猫,就直接改/etc/resolv.conf;如果是永久生效的話孩灯,還是要修改網(wǎng)卡的配置文件闺金。

文件/etc/hosts也能解析域名,我們可以修改該文件自定義域名峰档,示例命令如下:

[root@minglinux-01 network-scripts]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.43.66  www.taobao.com
[root@minglinux-01 network-scripts]# ping -c 2 www.taobao.com
PING www.taobao.com (192.168.43.66) 56(84) bytes of data.
64 bytes from www.taobao.com (192.168.43.66): icmp_seq=1 ttl=128 time=1.28 ms
64 bytes from www.taobao.com (192.168.43.66): icmp_seq=2 ttl=128 time=0.693 ms

--- www.taobao.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.693/0.986/1.280/0.295 ms

二败匹、firewalld和netfilter

  • SELinux

臨時關(guān)閉SELinux使用setenforce 0

永久關(guān)閉需要更改配置文件/etc/selinux/config,把SELINUX= enforcing
改成SELINUX=disabled讥巡。

更改完該配置文件后掀亩,重啟系統(tǒng)方可生效』肚辏可以使用getenforce命令獲得當(dāng)前SELinux的狀態(tài):

[root@minglinux-01 network-scripts]# getenforce
Permissive
[root@minglinux-01 ~]# getenforce     //修改配置文件后
Disabled
  • netfilter
    在之前的CentOS版本(比如5和6)的防火墻為netfilter槽棍,CentOS7的防火墻為firewalld。

首先抬驴,把firewalld關(guān)閉炼七,然后開啟之前版本的iptables,示例命令如下:

[root@minglinux-01 ~]# systemctl stop firewalld // 關(guān)閉firewalld服務(wù)
[root@minglinux-01 ~]# systemctl disable firewalld // 禁止firewalld服務(wù)開機啟動
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@minglinux-01 ~]# yum install -y iptables-services // 安裝iptables-services布持,這樣就可以使用之前版本的iptables了
[root@minglinux-01 ~]# systemctl enable iptables // 讓它開機啟動
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to
/usr/lib/systemd/system/iptables.service.
[root@minglinux-01 ~]# systemctl start iptables // 啟動iptables服務(wù)

三豌拙、netfilter5表5鏈介紹

  1. netfilter的5個表
  • filter表主要用于過濾包,是系統(tǒng)預(yù)設(shè)的表题暖,這個表也是阿銘用得最多的表姆蘸。該表內(nèi)建3個鏈:INPUT、OUTPUT以及FORWARD芙委。INPUT鏈作用于進入本機的包逞敷,OUTPUT鏈作用于本機送出的包,F(xiàn)ORWARD鏈
    作用于那些跟本機無關(guān)的包灌侣。
  • nat表主要用于網(wǎng)絡(luò)地址轉(zhuǎn)換推捐,它也有3個鏈。PREROUTING鏈的作用是在包剛剛到達(dá)防火墻時改變它的目的地址(如果需要的話)侧啼,OUTPUT鏈的作用是改變本地產(chǎn)生的包的目的地址牛柒,POSTROUTING鏈的作用是在包即將離開防火墻時改變其源地址。
  • mangle表主要用于給數(shù)據(jù)包做標(biāo)記痊乾,然后根據(jù)標(biāo)記去操作相應(yīng)的包皮壁。
  • raw表可以實現(xiàn)不追蹤某些數(shù)據(jù)包,默認(rèn)系統(tǒng)的數(shù)據(jù)包都會被追蹤哪审,但追蹤勢必消耗一定的資源蛾魄,所以可以用raw表來指定某些端口的包不被追蹤。
  • security表在CentOS 6中是沒有的,它用于強制訪問控制(MAC)的網(wǎng)絡(luò)規(guī)則滴须。
  1. netfilter的5個鏈

PREROUTING:數(shù)據(jù)包進入路由表之前舌狗。
INPUT:通過路由表后目的地為本機。
FORWARDING:通過路由表后扔水,目的地不為本機痛侍。
OUTPUT:由本機產(chǎn)生,向外轉(zhuǎn)發(fā)魔市。
POSTROUTING:發(fā)送到網(wǎng)卡接口之前主届。

四、iptables語法

介紹幾種常用的語法待德。

  • 查看iptables規(guī)則:iptables -nvL
    默認(rèn)查看的是filter表
    規(guī)則保存在/etc/sysconfig/iptables文件中
[root@minglinux-01 ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptato /usr/lib/systemd/system/iptables.service.
[root@minglinux-01 ~]#  systemctl start iptables
[root@minglinux-01 ~]# 
[root@minglinux-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   13  1048 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    1   229 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 10 packets, 956 bytes)
 pkts bytes target     prot opt in     out     source               destination  

-nvL表示查看該表的規(guī)則君丁,其中-n表示不針對IP反解析主機名,-L表示列出磅网,-v表示列出的信息更加詳細(xì)谈截。

加-t選項可以指定要查看的表筷屡,如打印nat表的相關(guān)信息:

[root@minglinux-01 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination      
  • iptables -F 清除規(guī)則
    清除規(guī)則的兩個命令:
    iptables -F
    iptables -Z

這里-F表示把所有規(guī)則全部刪除涧偷,如果不加-t指定表,默認(rèn)只清除filter表的規(guī)則毙死。-Z表示把包以及流量計數(shù)器置零燎潮。

  • service iptables save 保存規(guī)則
  • 增加/刪除一條規(guī)則
    iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
    規(guī)則中選項的作用:

? -A/-D:表示增加/刪除一條規(guī)則。
? -I:表示插入一條規(guī)則扼倘,其實效果跟-A一樣确封。
? -p:表示指定協(xié)議,可以是tcp再菊、udp或者icmp爪喘。
? --dport:跟-p一起使用,表示指定目標(biāo)端口纠拔。
? --sport:跟-p一起使用秉剑,表示指定源端口。
? -s:表示指定源IP(可以是一個IP段)稠诲。

? -d:表示指定目的IP(可以是一個IP段)侦鹏。
? -j:后面跟動作,其中ACCEPT表示允許包臀叙,DROP表示丟掉包略水,REJECT表示拒絕包。
? -i:表示指定網(wǎng)卡劝萤。

  • 插入一條規(guī)則渊涝,把來自1.1.1.1的所有數(shù)據(jù)包丟掉:
    iptables -I INPUT -s 1.1.1.1 -j DROP
  • 刪除剛插入的規(guī)則:
    iptables -D INPUT -s 1.1.1.1 -j DROP
  • 把來自2.2.2.2并且是TCP協(xié)議到本機80端口的數(shù)據(jù)包丟掉:
    iptables -I INPUT -s 2.2.2.2 -p tcp --dport 80 -j DROP
    --dport/--sport必須和-p選項一起使用

  • 把發(fā)送到10.0.1.14的22端口的數(shù)據(jù)包丟掉:
    iptables -I OUTPUT -p tcp --dport 22 -d 10.0.1.14 -j DROP

  • 把來自192.168.1.0/24這個網(wǎng)段且作用在eth0上的包放行:
    iptables -A INPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT

  • 按編號列出規(guī)則:iptables -nvL --line-numbers

[root@minglinux-01 ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 DROP       tcp  --  *      *       2.2.2.2              0.0.0.0/0            tcp dpt:80
2       80  5776 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
3        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
5        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
6        2   458 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
7        0     0 ACCEPT     all  --  eth0   *       192.168.1.0/24       0.0.0.0/0           

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 3 packets, 356 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 DROP       tcp  --  *      *       0.0.0.0/0            10.0.1.14            tcp dpt:22
  • 按編號刪除某一規(guī)則:
    iptables -D INPUT 1
    這里-D后面依次跟鏈名、規(guī)則num。這個num就是查看iptables規(guī)則時第1列的值驶赏。

  • 預(yù)設(shè)某一鏈的策略:
    iptables -P INPUT DROP
    即-P(大寫)選項炸卑,它表示預(yù)設(shè)策略。策略內(nèi)容或為DROP煤傍,或為ACCEPT盖文,默認(rèn)是ACCEPT。這個策略一旦設(shè)定后蚯姆,只有使用命令iptables -P INPUT ACCEPT才能恢復(fù)成原始狀態(tài)五续。

擴展(selinux了解即可)
  1. selinux教程 http://os.51cto.com/art/201209/355490.htm
    2.selinux pdf電子書 http://pan.baidu.com/s/1jGGdExK
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市龄恋,隨后出現(xiàn)的幾起案子疙驾,更是在濱河造成了極大的恐慌,老刑警劉巖郭毕,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件它碎,死亡現(xiàn)場離奇詭異,居然都是意外死亡显押,警方通過查閱死者的電腦和手機扳肛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來乘碑,“玉大人挖息,你說我怎么就攤上這事∈薹簦” “怎么了套腹?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長资铡。 經(jīng)常有香客問我电禀,道長,這世上最難降的妖魔是什么笤休? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任尖飞,我火速辦了婚禮,結(jié)果婚禮上宛官,老公的妹妹穿的比我還像新娘葫松。我一直安慰自己,他們只是感情好底洗,可當(dāng)我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布腋么。 她就那樣靜靜地躺著,像睡著了一般亥揖。 火紅的嫁衣襯著肌膚如雪珊擂。 梳的紋絲不亂的頭發(fā)上圣勒,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天,我揣著相機與錄音摧扇,去河邊找鬼圣贸。 笑死,一個胖子當(dāng)著我的面吹牛扛稽,可吹牛的內(nèi)容都是我干的吁峻。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼在张,長吁一口氣:“原來是場噩夢啊……” “哼用含!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起帮匾,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤啄骇,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后瘟斜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體缸夹,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年螺句,在試婚紗的時候發(fā)現(xiàn)自己被綠了虽惭。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡壹蔓,死狀恐怖趟妥,靈堂內(nèi)的尸體忽然破棺而出猫态,到底是詐尸還是另有隱情佣蓉,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布亲雪,位于F島的核電站勇凭,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏义辕。R本人自食惡果不足惜虾标,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望灌砖。 院中可真熱鬧璧函,春花似錦、人聲如沸基显。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽撩幽。三九已至库继,卻和暖如春箩艺,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背宪萄。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工艺谆, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人拜英。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓静汤,卻偏偏與公主長得像,于是被迫代替她去往敵國和親居凶。 傳聞我的和親對象是個殘疾皇子撒妈,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容