目錄
一臀玄、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)
-
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)線。
-
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鏈介紹
- 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ī)則滴须。
- 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了解即可)
- selinux教程 http://os.51cto.com/art/201209/355490.htm
2.selinux pdf電子書 http://pan.baidu.com/s/1jGGdExK