Linux網絡相關
#ifconfig 查看網卡ip(如果沒有這個命令請使用yum install net-tools安裝)
#ifdown ens33 關閉網卡ens33(在使用遠程終端連接時不可使用這條命令,否則會斷掉網絡)
#ifup ens33 打開網卡ens33,用這種方式可以單獨重啓指定的網卡(有幾張網卡的情況下),而不是重啓整個網絡服務。
設定虛擬網卡ens33:1
#ifdown ens33 && ifup ens33 在遠程終端可以把兩條命令相結合使用,這樣重啓的效果更好。
示例如下:
# ifdown ens33 && ifup ens33
成功斷開設備 'ens33'。
成功激活的連接(D-Bus 激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/2)
給一個網卡設定多個IP
# cd /etc/sysconfig/network-scripts/
# cp ifcfg-ens33 ifcfg-ens33\:0 //複製網卡配置文件,並取一個新的網卡名,這裏加反斜槓(\)是因爲要把:轉義,不然linux命令無法識別。
# vi ifcfg-ens33\:0 更改完成後保存退出
# cat ifcfg-ens33\:0
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33:0 這個地方要更改成前面設置的ens33:0
UUID=3c08189a-cb3f-4879-92de-ee0691b1d4cc
DEVICE=ens33:0 這個地方要更改成前面設置的ens33:0
ONBOOT=yse
IPADDR=192.168.1.190 更改一個沒有使用的IP
NETMASK=255.255.0.0
GATEWAY=192.168.1.1
DNS1=119.29.29.29
DNS2=8.8.8.8
# ifdown ens33 && ifup ens33 重啓網卡
成功斷開設備 'ens33'。
成功激活的連接(D-Bus 激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/3)
# ifconfig //重啓後可以看到多了一個網卡ens33:0
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.185 netmask 255.255.0.0 broadcast 192.168.255.255
inet6 fe80::1ffb:cde1:5f3e:5778 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:09:e5:58 txqueuelen 1000 (Ethernet)
RX packets 3561287 bytes 361515464 (344.7 MiB)
RX errors 0 dropped 1092 overruns 0 frame 0
TX packets 113641 bytes 22059136 (21.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.190 netmask 255.255.0.0 broadcast 192.168.255.255
ether 00:0c:29:09:e5:58 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 1 (Local Loopback)
RX packets 28 bytes 2380 (2.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 28 bytes 2380 (2.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
查看網卡是否連接
# mii-tool ens33 link ok表示連接正常,如果顯示no link表示網卡壞了或沒有連接網線
ens33: negotiated 1000baseT-FD flow-control, link ok
[root@aminglinux-01 ~]# ethtool ens33 最後一行顯示link detected:yes說明網卡正常,如果顯示爲no,說明網卡壞或沒有連接網線
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
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
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
更改主機名
# hostname 查看主機名
ccj-01
# hostname ccj-02 更改主機名,但是這僅僅只是保存在內存中,重啓後失效。
# hostname 更改完成,重啓後失效。
ccj-02
# vim /etc/hostname 如果想要重啓生效,需要更改配置文件。
# hostnamectl set-hostname ccj-02 這個命令可以直接更改/ect/hostname文件 ,只適用centos7
# hostname
ccj-02
# cat /etc/hostname //顯示文件已更改生效
ccj-02
更改DNS
- 在網卡配置文件裏更改DNS是永久更改, 更改命令vi /etc/sysconfig/network-scripts/ifcfg-ens33(網卡名稱),
- 但是在/etc/resolv.conf中也可以更改DNS,這裏的更改是臨時的。一般可以在此設置多個DNS,以做備用,如果只是臨時修改DNS地址,就直接修改DNS配置文件,如果需要永久生效的話,還是要修改網卡的配置文件。
示例如下:
# cat /etc/resolv.conf 查看網卡已有的DNS
Generated by NetworkManager
nameserver 119.29.29.29
nameserver 8.8.8.8
# vim /etc/resolv.conf 臨時更改
Generated by NetworkManager
nameserver 119.29.29.29
nameserver 8.8.8.8
- /etc/hosts文件
在linux下有一個特殊文件/etc/hosts可能解析域名,不過需要我們在裏面手動添加ip與域名,它的作用是臨時解析某個域名,非常有用。
示例如下:
# cat /etc/hosts 查看文件內容
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# ping www.qq123.com
PING www.qq123.com (202.91.250.93) 56(84) bytes of data.
64 bytes from 202.91.250.93 (202.91.250.93): icmp_seq=1 ttl=231 time=30.3 ms
64 bytes from 202.91.250.93 (202.91.250.93): icmp_seq=2 ttl=231 time=30.2 ms
64 bytes from 202.91.250.93 (202.91.250.93): icmp_seq=3 ttl=231 time=30.2 ms
^C
--- www.qq123.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 30.227/30.284/30.395/0.162 ms
# vim /etc/hosts 編輯文件把上面這個域名指向的ip更改成192.168.1.190 ,保存退出
# ping www.qq123.com 驗證域名指定IP
PING www.qq123.com (192.168.1.190) 56(84) bytes of data.
64 bytes from www.qq123.com (192.168.1.190): icmp_seq=1 ttl=64 time=0.152 ms
64 bytes from www.qq123.com (192.168.1.190): icmp_seq=2 ttl=64 time=0.086 ms
64 bytes from www.qq123.com (192.168.1.190): icmp_seq=3 ttl=64 time=0.060 ms
^C
--- www.qq123.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.060/0.099/0.152/0.039 ms
注意:
1. 一個IP後面可以跟多個域名,可以是幾十個甚至上百個。
2. 每一行只能有一個IP,也就是一個域名不能對應多個IP。
3. 如果有多行中出現相同的域名(對應的IP不一樣),會按最前面出現的記錄來解析。
firewalld和netfilter
SElinux
selinux是Linux系統特有的安全機制,因爲這種機制的限制太多,配置也麻煩,所以幾乎沒有人真正的應用它。安裝完系統後我們一般會選擇關閉selinux。
示例如下:
# getenforce 查看當前SElinux的狀態
Enforcing
# setenforce 0 臨時關閉
# getenforce
Permissive
# cat /etc/selinux/config 查看配置文件
This file controls the state of SELinux on the system.
SELINUX= can take one of these three values:
enforcing - SELinux security policy is enforced.
permissive - SELinux prints warnings instead of enforcing.
disabled - No SELinux policy is loaded.
SELINUX=enforcing
SELINUXTYPE= can take one of three two values:
targeted - Targeted processes are protected,
minimum - Modification of targeted policy. Only selected processes are protected.
mls - Multi Level Security protection.
SELINUXTYPE=targeted
# vi /etc/selinux/config 更改配置文件,重啓生效
This file controls the state of SELinux on the system.
SELINUX= can take one of these three values:
enforcing - SELinux security policy is enforced.
permissive - SELinux prints warnings instead of enforcing.
disabled - No SELinux policy is loaded.
SELINUX=disabled //把enforcing改成disabled 這裏我已經更改了
SELINUXTYPE= can take one of three two values:
targeted - Targeted processes are protected,
minimum - Modification of targeted policy. Only selected processes are protected.
mls - Multi Level Security protection.
SELINUXTYPE=targeted
netfilter和firewalld
在centos版本5和6上用的防火牆是netfiler,centos7則用的是firewalld防火牆,很多人把Linux的防火牆叫作iptables,其實這不是,iptables僅僅是一個工具。目前現在很多企業依然在使用centos6,但firewalld是向下兼容netfiler的,同樣也支持之前版本的命令用法。
示例如下:
# systemctl disable firewalld 禁止firewalld服務開機啓動
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
# systemctl stop firewalld 關閉firewalld服務
# yum install -y iptables-services 安裝iptables-services,這樣就可以使用之前版本的iptables了。
# systemctl enable iptables 讓它開機啓動
# systemctl start iptables 啓動iptables服務
netfilter5表5鏈介紹
netfilter的5個表
1. filter表用於過濾包,是系統預設的表,也是最常用的表,該表有內建三個鏈INPUT、OUTPUT、FORWARD。
- INPUT鏈作用於進入本機的包。
- OUTPUT鏈作用於本機送機的包。
- FORWARD鏈作用於那些跟本機無關的包。
2. nat表用於網絡地址轉換,有PREROUTING、OUTPUT、POSTROUTING三個鏈。
- PREROUTINC鏈的作用是在包剛剛到達防火牆時改變它的目的地址(如果需要的話)
- OUTPUT鏈的作用是改變本地產生的包的目的地址。
- POSTROUTINC鏈的作用是在包即將離開防火牆時改變其源地址。
3. 數據包流向與netfilter的5個鏈
- PREROUTING:數據包進入路由表之前。
- INPUT:通過路由表後目的地爲本機。
- FORWARD:通過路由表後,目的地不爲本機。
- OUTPUT:由本機產生,向外發出。
- POSTROUTING:發送到網卡接口之前。
4. 具體的數據包流向,可以參考下圖。
![](https://i.imgur.com/SnGqpwR.png)
參考文章 http://www.cnblogs.com/metoy/p/4320813.html
iptables語法
1. iptables已經有默認規則,我們可以用以下命令查看。但是我們在設置自己的規則之前,建議先將其清除。-nvL選項表示查看規則,-F選項表示清除當前規則,但清除只是臨時的,重啓還是會加載已經保存的規則,所以需要使用service iptables save保存一下規則。通過下面這個命令輸出,我們也可以看到防火牆規則保存在/etc/sysconfig/iptables裏,可以查看下這個文件。
示例如下:
# iptables -nvL 查看iptables默認規則
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
56 4076 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
1235 94818 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 40 packets, 3788 bytes)
pkts bytes target prot opt in out source destination
# service iptables restart 重啓規則
# cat /etc/sysconfig/iptables 查看規則文件
Redirecting to /bin/systemctl restart iptables.service
# iptables -F 清空所有規則
# service iptables save 保存後規則裏就什麼規則都沒有了
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 確定 ]
2. -t後面跟表名,指定filter表
# iptables -t filter -nvl
3. iptables -Z -Z把包和流量計數器清零
4. 增加/刪除一條規則,其用法如下:
# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP 增加一條規則
# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
# iptables -nvL
Chain INPUT (policy ACCEPT 847 packets, 64330 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP tcp -- 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 25 packets, 2224 bytes)
pkts bytes target prot opt in out source destination
5. iptables -I INPUT -s 1.1.1.1 -j DROP 表示插入一條規則,丟掉所有來自1.1.1.1的數據包。
6. iptables -A INPUT -s 1.1.1.1 -j DROP 表示增加一條規則
7. iptables -D INPUT -s 1.1.1.1 -j DROP 注意刪除一條規則時,必須和插入的規則一致。
8. iptables -I INPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT //表示把來自192.168.1.0/24這個網段且作用在etho上的包放行。
9. 有時候服務器上的iptables過多了,你想刪除一某一條規則,但又不知道之前創建時的規則,你可先查看iptables規則,命令如下:
# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 9235 packets, 708K bytes)
num pkts bytes target prot opt in out source destination
1 0 0 DROP tcp -- 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 191 packets, 18404 bytes)
num pkts bytes target prot opt in out source destination
刪除一條規則,使用如下命令:
# iptables -D INPUT 1
10. iptables還有一個規則經常用支,即-P(大寫)選項,它表示預設策略,用法如下:
# iptables -P INPUT DROP
說明:-P後面跟鏈名,策略內容或爲DROP,或爲ACCEPT,默認是ACCEPT。注意:如果你在連接遠程服務器,千萬不要隨便執行這個命令,因爲一旦輸入命令並回車,遠程連接會被斷開。這個策略一旦設定成功後,只有使用命令iptables -P INPUT ACCEPT才能恢復成原始狀態。
解釋:
- -A:增加一條規則。
- -D:刪除一條規則。
- -I:插入一條規則 insert,其效果-A一樣。
- -p:指定協議,可以是tcp、udp或者icmp。
- --dport:和-p一起使用,指定目標端口destination port。
- --sport:和-p一起使用,指定源端口source port。
- -s:指定源ip(可以是一個ip段)。
- -d:指定目的ip(可以是一個ip段)。
- -j:後面跟動作,ACCEPT表示允許包,REJECT表示拒絕包,DROP表示丟掉包。
- -i:指定網卡interface(不常用,但偶爾能用到)。