本文是基於centos 配置DNS條目: 配置文件:/etc/resolv.conf 修改主機名稱: 命令: 重啟後失效 配置文件:/etc/sysconfig/network CentOS7專用命令: 配置IP地址方式: 1,靜態指定: 命令: ifcfg家族: ifconfig:配置IP,NET ...
本文是基於centos
配置DNS條目:
配置文件:/etc/resolv.conf
修改主機名稱:
- 命令:
hostname NAME。
重啟後失效 - 配置文件:/etc/sysconfig/network
- CentOS7專用命令:
hostnamectl
配置IP地址方式:
1,靜態指定:
命令:
ifcfg家族:
ifconfig:配置IP,NETMASK(子網掩碼)
route:路由查看及管理
路由條目類型:
- 主機路由:目標地址為單個ip
- 網路路由:目標地址為ip網路
- 預設路由:目標為任意網路,0.0.0.0/0.0.0.0
netstat:查看狀態及統計數據
iproute2家族:
- ip addr:配置IP,NETMASK(子網掩碼)
- ip link:介面(網卡)
- ip route:路由
- ss:查看狀態及統計數據(比netstat性能好)
CentOS7專用:nm(network manager)家族:
- nmcli:命令行工具
- nmtui:text window工具
配置文件
redhat及相關發行版:/etc/sysconfig/network-scripts/ifcfg-網卡名
ll /etc/sysconfig/network-scripts/ifcfg-* -rw-r--r--. 1 root root 282 Nov 30 12:14 /etc/sysconfig/network-scripts/ifcfg-enp0s3 -rw-r--r--. 1 root root 254 Aug 24 2018 /etc/sysconfig/network-scripts/ifcfg-lo
2,動態指定:依賴於本地網路中的DHCP伺服器
DHCP:Dynamic Host Configure Procotol
客戶端請求DHCP伺服器,給我:【IP/netmask】,【gateway】,【連接外網的路由】,使用命令:dhclient -d
客戶端想DHCP伺服器請求ip地址時,是使用RARP協議的。
RARP協議:我(客戶端)有MAC地址,誰(DHCP伺服器)能給我一個IP啊。以廣播形式發送。
如果網路里有多個DHCP伺服器的話,誰給的快,就使用誰給的ip。這個ip是有使用期限的。
到期前,客戶端給DHCP伺服器單獨發送請求(註意,這裡不是廣播了),要求延期使用。
網路介面命名方式:
1,傳統命名方式
- 乙太網:ethX[0,正無窮),例如:eth0,eth1,。。。
- PPP網路:pppX[0,正無窮),例如:ppp0,ppp1,。。。
2,可預測命名方案(centos7):支持多種不同的命名機制。
基於固件(fireware)命名,或基於主板拓撲結構命名(pci插槽號等)。
- 如果fireware或bios為主板上集成的設備,所提供的設備索引信息可用,則根據此索引進行命名。例如:eno1,eno2,。。。
- 如果fireware或bios為pci-e擴展槽的設備,所提供的設備索引信息可用,則根據此索引進行命名。例如:ens1,ens2,。。。
- 如果fireware介面的物理位置信息可用,則根據此信息命名,例如:enp2s0,enp1s1
- 如果用戶顯示定義,也可以根據MAC地址命名,例如:enx12123311
- 上述均不可用,則使用傳統命名方式。
命名格式的含義:
- en:ethernet(乙太網)
- wl:wlan(無線網)
- ww:wwan(無線互聯網)
- o<index>:基礎設備的索引號
- s<slot>:擴展槽的索引號
- x<MAC>:基於MAC地址命名
- p<bus>s<slot>:基於匯流排及槽的拓撲結構命名
激活網卡/無效網卡等:
ifconfig interface [aftype] options | address ...
查看所有激活的網卡信息:
ifconfig
查看所有的網卡信息(包括為激活的):
ifconfig -a
查看某個網卡的信息(centos6):
ifconfig 網卡名
# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 08:00:27:80:F4:35 inet addr:192.168.0.108 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe80:f435/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:570 errors:0 dropped:0 overruns:0 frame:0 TX packets:72 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:40376 (39.4 KiB) TX bytes:12224 (11.9 KiB)
查看某個網卡的信息(centos7):
ifconfig 網卡名
# ifconfig enp0s3 enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.0.113 netmask 255.255.255.0 broadcast 192.168.0.255 inet6 fe80::b497:5ec:1efb:72b5 prefixlen 64 scopeid 0x20<link> ether 08:00:27:10:c2:53 txqueuelen 1000 (Ethernet) RX packets 16814 bytes 2630803 (2.5 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2433 bytes 569211 (555.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
flags:網卡支持的功能
- up:激活的網卡
- broadcast:廣播功能
- multicast:組播功能
- mtu:單次傳輸的最大位元組數
- inet:IP地址:
- netmask:子網掩碼
- broadcast:廣播地址
- ether(centos6:HWaddr):MAC地址
- txqueuelen:排隊長度
- RX:接受到的數據信息
- TX:發送出去的數據信息
激活網卡(給網卡賦予ip地址和子網掩碼):
ipconfig eth0 192.168.0.110/24 up
24的含義是24個1,所以24個1對應的子網掩碼就是255.255.255.0.
如果是16的話,對應的子網掩碼就是255.255.0.0
ipconfig eth0 192.168.0.110 netmask 255.255.255.0 up
此命令是立即生效的,但是重啟後,失效。
給一個網卡添加多個ip地址:
ifconfig IFACE:LABEL 192.168.0.110/24
IFACE:LABEL:eth0:0或者eth0:1。必須有冒號,冒號前面必須是原來網卡的名字。
使網卡無效:
ipconfig eth0 down
添加/刪掉ipv6地址
ifconfig eth0 add addr/prefixlen
ifconfig eth0 del addr/prefixle
選項介紹:
啟用某個功能/讓某個功能無效:使用-號。
比如啟用功能:promisc
# ifconfig eth0 promisc # ifconfig eth0 eth0 Link encap:Ethernet HWaddr 08:00:27:80:F4:35 inet addr:192.168.0.110 Bcast:192.168.0.255 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe80:f435/64 Scope:Link UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 Metric:1 RX packets:2116 errors:0 dropped:0 overruns:0 frame:0 TX packets:715 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:185897 (181.5 KiB) TX bytes:116601 (113.8 KiB)
多了PROMISC。
比如禁用廣播和組播功能:
# ifconfig eth0 -allmulti
設置路由及查看路由條目
以數字格式查看路由條目:
route -n
$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 enp0s3 192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
反解數字成主機名,查看路由條目:
route
$ route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 100 0 0 enp0s3 192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 enp0s3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
添加路由
路由條目類型:
- 主機路由:目標地址為單個ip
- 網路路由:目標地址為特定的ip網路
- 預設路由:目標為任意網路,0.0.0.0/0.0.0.0
route add [-net|-host] target [netmask Nm][gw Gw] [[dev] If]
net:代表目標是ip網路
host:代表目標是單個
Gw:下一跳的地址
Flags:
- U:網卡是啟用狀態
- G:此條是網關
Metric:度量值,到達這個網路需要的開銷。
Iface:經過本地的哪個介面(網卡)
route結果里的Destination:target(目標網路的IP地址)
route結果里的Genmask:目標網路的掩碼
route結果里的Gateway:下一跳的地址
實例:添加目標網路為10.0.0.0/8(因為目標是網路,所以使用-net),下一跳的地址是192.168.0.1,經過本地的eth0網卡(本地的網卡可以省略,會根據下一跳的地址,自動找出來:使用本地哪個網卡)。
註意:下一跳的IP必須和本地的網卡在同一個網路里。
# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 # route add -net 10.0.0.0/8 gw 192.168.0.1 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0 # route add -net 10.0.0.0/16 gw 192.168.0.1 dev eth0 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.0.0.0 192.168.0.1 255.255.0.0 UG 0 0 0 eth0 10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0
添加目標為單個IP
# route add -host 192.168.0.111 gw 192.168.0.1 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.111 192.168.0.1 255.255.255.255 UGH 0 0 0 eth0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.0.0.0 192.168.0.1 255.255.0.0 UG 0 0 0 eth0 10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0
註意:使用-host時,不能指定子網掩碼;Flags處有了H標識。
刪除路由:
route del addr/prefixlen gw GW dev if
刪除單個IP:
# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.111 192.168.0.1 255.255.255.255 UGH 0 0 0 eth0 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.0.0.0 192.168.0.1 255.255.0.0 UG 0 0 0 eth0 10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0 # route del 192.168.0.111 gw 192.168.0.1 dev eth0 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.0.0.0 192.168.0.1 255.255.0.0 UG 0 0 0 eth0 10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0
刪除預設網關:
# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0 0.0.0.0 192.168.0.2 0.0.0.0 UG 0 0 0 eth0 # route del default # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 0.0.0.0 192.168.0.2 0.0.0.0 UG 0 0 0 eth0
刪除網路:
# route del -net 10.0.0.0 gw 192.168.0.1 netmask 255.255.0.0 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 10.0.0.0 192.168.0.1 255.0.0.0 UG 0 0 0 eth0 # route del -net 10.0.0.0/8 gw 192.168.0.1 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
如何讓機器連接上互聯網?一般都是網關機器是連著外網的,所以只需要添加一個路由體哦條目,讓目的是外網的訪問,都扔給網關就好了。
查看路由信息
顯示路由信息:
-r
# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0
-n:以數字方式顯示。
查看網路連接信息
顯示網路連接信息
顯示tcp協議的連接信息:
-t
# netstat -t Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 64 192.168.0.111:ssh 192.168.0.110:56087 ESTABLISHED
顯示udp協議的連接信息:
-u
# netstat -u Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State
以數字形式顯示連接信息:
-n
$ netstat -tn Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 64 192.168.0.113:22 192.168.0.110:64168 ESTABLISHED
Proto:協議類型
Recv-Q:接受隊列的排隊數量
Send-Q:發送隊列的排隊數量
Lacal Address:這個連接的本機地址和埠號
Foreign Address:這個連接的另一端的地址和埠號
State:連接的狀態
顯示sctp協議的連接信息:
-S
顯示raw(裸套接字)的連接信息:
-w
顯示所有狀態的連接信息:
-a
不加a的話,只顯示state是非listen狀態的連接;加上a就意味著顯示所有狀態的tcp連接信息。
# netstat -tan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:53824 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 192.168.0.111:22 192.168.0.110:56087 ESTABLISHED tcp 0 0 :::43002 :::* LISTEN tcp 0 0 :::111 :::* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:631 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN
顯示處於監聽狀態下的連接信息:
-l
$ netstat -tln Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
顯示連接是由哪個進程(並顯示pid)發起的:
-p
# netstat -tpe Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name tcp 0 64 192.168.0.111:ssh 192.168.0.110:56087 ESTABLISHED root 14440 1874/sshd
顯示擴展信息:
-e
顯示user和Inode
user:由哪個用戶打開的連接
Inode:打開的連接在linux里都存在一個與之對應的套接字文件,這個套接字文件的inode號碼。
常用選項的組合:
-tan:顯示所有狀態的tcp連接,以數字形式表示。
-uan:顯示所有狀態的udp連接,以數字形式表示。
-tnl:只顯示處於監聽狀態下的tcp連接,以數字形式表示。
-unl:只顯示處於監聽狀態下的udp連接,以數字形式表示。
-tunlp:只顯示處於監聽狀態下的tcp或udp連接,以數字形式表示,並顯示是由哪個進程(並顯示pid)發起的。
查看網卡接收和發送數據的統合信息
查看所有網卡的接收和發送的信息:
-i
$ netstat -i Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg enp0s3 1500 2472 0 0 0 1003 0 0 0 BMRU lo 65536 84 0 0 0 84 0 0 0 LRU virbr0 1500 0 0 0 0 0 0 0 0 BMU
查看指定網卡的接收和發送的信息:
-I網卡名
。註意中間沒有空格$ netstat -Ienp0s3 Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg enp0s3 1500 2464 0 0 0 998 0 0 0 BMRU
Iface:網卡名
MTU:單詞傳輸的單位位元組數
RX/TX-OK:接收/發送成功的位元組數
RX/TX-ERR:接收/發送錯誤的位元組數
RX/TX-DRP:接收/發送丟棄的位元組數
RX/TX-OVR:接收/發送溢出的位元組數
FLG:此網卡啟用的功能
- B:廣播
- M:組播
- R:運行
- U:處於激活狀態
centos里啟用/禁用網卡的命令:
註意:要想執行下麵2個命令,必須存在/etc/sysconfig/network-scripts/ifcfg-網卡名文件才可以。
- ifup
- ifdown
修改主機名稱
hostname命令:
立即生效,重啟後失效。
- 查看主機名:
hostname
- 修改主機名:
hostname HOSTNAME
- 查看主機名:
hostnamectl命令:centos7才可以使用。
顯示當前主機名的相信信息:
hostnamectl status
$ hostnamectl status Static hostname: localhost.localdomain Icon name: computer-vm Chassis: vm Machine ID: 49e09ca283034d21b72a490d0560bad0 Boot ID: cbec389bce8c495c8ae9fefa9f2331e1 Virtualization: kvm Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-957.el7.x86_64 Architecture: x86-64
修改主機名:
hostnamectl set-hostname
立即生效,並且永久有效。
hostnamectl的使用幫助:
hostnamectl --help
通過修改配置文件,修改主機名稱。
文件:/etc/sysconfig/network。
修改HOSTNAME=後面的內容,就修改了主機名稱。但不能立即生效,重啟後會生效,並永久有效。
# cat /etc/sysconfig/network NETWORKING=yes HOSTNAME=c6
配置DNS
1,通過本地的/etc/hosts
添加一個條目:192.168.0.113 centos7 cent.com。一個IP地址後面可以有多個別名。
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.0.113 centos7 cent.com
# ping centos7
PING centos7 (192.168.0.113) 56(84) bytes of data.
64 bytes from centos7 (192.168.0.113): icmp_seq=1 ttl=64 time=0.644 ms
# ping cent.com
PING centos7 (192.168.0.113) 56(84) bytes of data.
64 bytes from centos7 (192.168.0.113): icmp_seq=1 ttl=64 time=0.352 ms
2,通過配置文件,指定DNS伺服器的IP地址。這個DNS伺服器自己如果沒有維護,它就會自動去互聯網上找別的DNS伺服器去解析網址,所以這個DNS伺服器要能連接上外網!
配置文件:/etc/resolv.conf
nameserver就是DNS伺服器的IP,最多可以指定3個nameserver。下麵的192.168.0.1是網關,可以連接外網。
# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 192.168.0.1
3,測試DNS伺服器是否好用:dig -t A www.baidu.com
dig命令不會去先找/etc/hosts文件,它直接從dns伺服器上找。
# dig -t A www.baidu.com
; <<>> DiG 9.8.2rc1-RedHat-9.8.2-0.68.rc1.el6 <<>> -t A www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 14893
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;www.baidu.com. IN A
;; ANSWER SECTION:
www.baidu.com. 933 IN CNAME www.a.shifen.com.
www.a.shifen.com. 50 IN A 39.156.66.18
www.a.shifen.com. 50 IN A 39.156.66.14
;; Query time: 19 msec
;; SERVER: 192.168.0.1#53(192.168.0.1)
;; WHEN: Sat Jan 4 21:43:54 2020
;; MSG SIZE rcvd: 90
iproute2家族命令介紹
ip命令後面緊跟,起名叫object。iproute2家族命令未來可能取代ifconfig命令。
object有:link,netns,route等等。
每個object基本都有:add,del,set,show/list等操作。
而且,object和操作命令都支持簡寫。
1,添加,刪除,修改網卡(介面):ip link...
- 添加網路介面:
ip link add
- 刪除網路介面:
ip link del
- 修改網路介面:
ip link set
- 顯示網路介面:
ip link show/list
link可以簡寫成li
list可以簡寫成li
show可以簡寫成sh
# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff
# ip link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff
<BROADCAST,MULTICAST,UP,LOWER_UP>:啟用的功能
qdisc:隊列
pfifo_fast:先進先出隊列
qlen:隊列長度
brd:廣播地址
啟用/禁用網路介面:
ip link set eth1 up/down
# ip li li eth1.1 4: eth1.1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff # ip link set eth1.1 up # ip li li 4: eth1.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff # ip link set eth1.1 down # ip li li eth1.1 4: eth1.1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff
啟用/禁用多播功能:
ip link set eth1 multicast on/off
# ip li li eth1.1 4: eth1.1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff # ip link set eth1.1 multicast off # ip li li eth1.1 4: eth1.1: <BROADCAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff # ip link set eth1.1 multicast on # ip li li eth1.1 4: eth1.1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff
修改網路介面的名稱:
ip link set eth1 name NAME
# ip link list 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff 3: veth1.2: <NO-CARRIER,BROADCAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether d2:01:ed:42:dd:67 brd ff:ff:ff:ff:ff:ff 4: eth1.1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff # ip link set eth1.1 name eth3.1 # ip link list 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff 3: veth1.2: <NO-CARRIER,BROADCAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether d2:01:ed:42:dd:67 brd ff:ff:ff:ff:ff:ff 4: eth3.1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff
修改mtu:
ip link set eth1.1 mtu 1000
乙太網的最大mtu值是1500
# ip link set eth3.1 mtu 1000 [root@c6 ~]# ip li li eth3.1 4: eth3.1: <BROADCAST,MULTICAST> mtu 1000 qdisc pfifo_fast state DOWN qlen 1000 link/ether 5e:17:b9:5e:b7:ed brd ff:ff:ff:ff:ff:ff
顯示幫助:
ip link help
2,在指定網卡上添加,刪除,顯示ip地址:ip addr...
在同一個網卡上可以添加多個不同的ip地址!!
從指定網卡(介面)上刪除ip地址:
ip addr del ipaddr/len dev eth0
例子:從網卡eth0上刪除ip地址192.168.0.11/24
# ip ad li 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff inet 192.168.0.111/24 scope global eth0 inet 192.168.0.11/24 scope global secondary eth0 inet6 fe80::a00:27ff:fe80:f435/64 scope link valid_lft forever preferred_lft forever # ip addr del 192.168.0.11/24 dev eth0 # ip ad li 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff inet 192.168.0.111/24 scope global eth0 inet6 fe80::a00:27ff:fe80:f435/64 scope link valid_lft forever preferred_lft forever
一次刪除網卡上的所有ip
# ip addr flush dev eth0
顯示網卡(介面)上的ip地址:
ip addr show/list
其他選項:
給添加的ip地址起一個別名,不起別名的話,使用
ifconfig
命令,只能看到一個ip地址。別名不能亂起,格式:原來的網卡:冒號後面隨意!
格式不對會出下麵的錯誤:
# ip addr add 192.168.0.13/24 dev eth0 label sec "dev" (eth0) must match "label" (sec).
不加冒號,也可以成功,但是
ifconfig
命令就會出錯。# ip addr add 192.168.0.14/24 dev eth0 label eth011 # ifconfig eth0 Link encap:Ethernet HWaddr 08:00:27:80:F4:35 inet addr:192.168.0.111 Bcast:0.0.0.0 Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe80:f435/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:3930 errors:0 dropped:0 overruns:0 frame:0 TX packets:1314 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:316975 (309.5 KiB) TX bytes:205600 (200.7 KiB) eth011: error fetching interface information: Device not found
例子:給添加的ip一個label叫eth0:1。
# ip addr add 192.168.0.12/24 dev eth0 label eth0:1 # ip ad sh 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff inet 192.168.0.111/24 scope global eth0 inet 192.168.0.11/24 scope global secondary eth0 inet 192.168.0.12/24 scope global secondary eth0:1 inet6 fe80::a00:27ff:fe80:f435/64 scope link valid_lft forever preferred_lft forever # ifconfig eth0:1 eth0:1 Link encap:Ethernet HWaddr 08:00:27:80:F4:35 inet addr:192.168.0.12 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
給新添加的IP一個廣播地址:
例子:給ip一個廣播地址192.168.0.255。
# ip addr add 192.168.0.13/24 dev eth0 label eth0:2 broadcast 192.168.0.255 # ip ad li 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff inet 192.168.0.111/24 scope global eth0 inet 192.168.0.11/24 scope global secondary eth0 inet 192.168.0.12/24 scope global secondary eth0:1 inet 192.168.0.13/24 brd 192.168.0.255 scope global secondary eth0:2 inet6 fe80::a00:27ff:fe80:f435/64 scope link valid_lft forever preferred_lft forever # ifconfig eth0:2 eth0:2 Link encap:Ethernet HWaddr 08:00:27:80:F4:35 inet addr:192.168.0.13 Bcast:192.168.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
給新添加的IP,一個可使用的限定範圍:
例子:添加一個IP,使用範圍是global。還可以使用的範圍:link,host
# ip addr add 192.168.0.14/24 dev eth0 label eth0:3 broadcast 192.168.0.255 scope global # ip ad li 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:f4:35 brd ff:ff:ff:ff:ff:ff inet 192.168.0.111/24 scope global eth0 inet 192.168.0.11/24 scope global secondary eth0 inet 192.168.0.12/24 scope global secondary eth0:1 inet 192.168.0.13/24 brd 192.168.0.255 scope global secondary eth0:2 inet 192.168.0.14/24 brd 192.168.0.255 scope global secondary eth0:3 inet6 fe80::a00:27ff:fe80:f435/64 scope link valid_lft forever preferred_lft forever
3,在指定的網卡上添加,刪除,修改路由:ip route ...
添加路由:必須指明目的,下一跳在哪,通過哪個網卡,通過網卡上的哪個ip(因為同一個網卡可以添加多個ip地址)
下麵例子的目的是:0.0.0.0/0。
註意,和
route add
命令不同的是:目的不需要指明是單一ip還是網路,會自動識別。下一跳:192.168.0.1
通過哪個網卡:eth0
通過eth0上的哪個ip:192.168.0.11
# ip route add default via 192.168.0.1 dev eth0 src 192.168.0.111 # ip route list default via 192.168.0.1 dev eth0 src 192.168.0.111
修改路由/替換:
ip route change/replace
用法和add一樣。
刪除路由:
ip route del 192.168.0.12/24
如果目的地(192.168.0.12/24)是唯一的,則後面的dev,src等可以省略。
顯示路由:
ip route show/list
清空指定網段的路由:
ip route flush 192/24
顯示特定的路由:
ip route get 192.168.3.3/24
查看網卡接收和發送數據的統合信息
命令:ss
。和netstat
命令很像,未來可能取代netstat
命令。
上面netstat命令的選項,ss也都有,用法也一樣。
查看連接耗費的記憶體:
-m
查看連接的計時器信息:
-o
使用過濾功能,去特定想看的信息:
FILTER := [ state STATE-FILTER ][ EXPRESSION ]
# ss -tan State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :::111 :::* LISTEN 0 128 *:111 *:* LISTEN 0 128 :::22 :::* LISTEN 0 128 *:22 *:* LISTEN 0 128 *:39286 *:* LISTEN 0 128 127.0.0.1:631 *:* LISTEN 0 128 ::1:631 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 :::54873 :::* ESTAB 0 0 10.247.237.54:22 10.247.237.16:61550 # ss -tan '( dport = :22 or sport = :22 )' State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 :::22 :::* LISTEN 0 128 *:22 *:* ESTAB 0 64 10.247.237.54:22 10.247.237.16:61550 # ss -tan state ESTABLISHED '( dport = :22 or sport = :22 )' Recv-Q Send-Q Local Address:Port Peer Address:Port 0 64 10.247.237.54:22 10.247.237.16:61550
'( dport = :22 or sport = :22 )':只看目標埠是22或者源埠是22的連接。
state ESTABLISHED:只看tcp連接狀態是ESTABLISHED的連接。
TCP的狀態:
- LISTEN:連接處於監聽狀態
- ESTABLISHED:連接處於建立成功,通信的狀態
- FIN_WAIT_1:一方提出斷開連接(分手),等待對方回應(歸還照片)。
- FIN_WAIT_2:
- SYN_SENT:被分手了,頭也不回的走掉
- SYN_RECV:
- CLOSED:
通過配置文件永久修改網路配置
上面介紹的修改網路配置的命令,都是臨時有效的,系統重啟後,就都沒了。
要想永久有效,修改修改配置文件或者使用特殊的命令。
特殊的命令:
- centos6:
system-config-network
或者setup
- centos7:
nmtui
配置文件:
ip/netmask/gw/dns等相關的配置文件:/etc/sysconfig/network-scripts/ifcfg-介面名
centos6配置文件示例:
DEVICE=eth0 HWADDR=08:00:27:80:F4:35 TYPE=Ethernet UUID=f303b98f-14d7-4198-900a-0b2329bb8cae ONBOOT=no NM_CONTROLLED=yes BOOTPROTO=dhcp
centos7配置文件示例:
TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=dhcp 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=enp0s3 UUID=e2d60303-d70d-4335-aabd-f6c4cf86fade DEVICE=enp0s3 ONBOOT=yes
DEVICE:設備名字,也就是ifcfg-DEVICE。
ONBOOT:系統系統後,是否自動激活此介面。
NETBOOT:網路引導:
UUID:此設別的唯一標識
IPV6INIT:是否初始化IPv6協議
BOOTPROTO:激活此介面時使用上面協議來激活此介面。
常用的有:dhcp,bootp,static,none。
如果指定成了dhcp,並且還手動指定了ip地址,則手動指定的ip地址不生效,dhcp生效。
TYPE:介面類型,常見的有:Ethernet,Bridge
DNS1:第一DNS伺服器指向。
DNS2:第二(備用)DNS伺服器指向。
DOMAIN:DNS搜索域
DEFROUTE:是否是預設路由所使用的介面(網卡)。
GATEWAY:預設網關
IPADDR:IP地址
NETMASK/PREFIX:子網掩碼。centos6是NETMASK;centos7是PREFIX
IPV4_FAILURE_FATAL:ipv4出故障後,是否是嚴重錯誤。
USERCTL:是否允許普通用戶控制此設備
PEERDNS:如果BOOTPROTO的值為dhcp
預設是yes:允許dhcp伺服器給的dns伺服器指向,覆蓋本地的dns伺服器指向。
no:不允許dhcp伺服器給的dns伺服器指向,覆蓋本地的dns伺服器指向。目的是使用本地的dns伺服器指向。
NM_CONTROLLED:是否允許使用networkManage服務來控制此介面。
HWADDR:MAC地址。
修改配置文件後,必須重啟網路服務,才能讓修改生效。
centos6的網路服務有2個:network和NetworkManager。但是NetworkManager不穩定,不要使用。
centos7的網路服務有2個:network和NetworkManager。NetworkManager已經很穩定,推薦使用。
centos6重啟網路服務的命令:
service network restart
centos6開始/停止網路服務的命令:
service network start|stop
查看centos6網路服務的命令:
service network status
# service network status Configured devices: lo eth0 Currently active devices: lo eth0 eth1
Configured devices:有配置文件的介面(網卡),由於系統里存在文件:/etc/sysconfig/network-scripts/ifcfg-eth0;不存在/etc/sysconfig/network-scripts/ifcfg-eth1文件,所有這裡沒有顯示eth1.
Currently active devices:處於激活的介面(網卡)
添加/etc/sysconfig/network-scripts/ifcfg-eth1文件後,再使用
service network status
,eth1就出現了。# service network status Configured devices: lo eth0 eth1 Currently active devices: lo eth0 eth1
centos7重啟網路服務的命令也可以使用:
service network restart
但是最佳用法:
systemctl restart|start|stop|status network[.service]
通過配置文件,給同一網卡添加多個ip地址。
創建一個/etc/sysconfig/network-scripts/ifcfg-網卡名字:0的文件,文件里的DEVICE名字,要修改成和文件名ifcfg-後面的部分相同。
註意:網卡別名的ip地址不能是從dhcp獲取,所以BOOTPROTO的值不可以是dhcp,只能是static或none。ip地址必須手動指定。
路由相關的配置文件:/etc/sysconfig/network-scripts/route-介面名
支持2種方式,但不可以混用。
方式1:每行一個路由條目:TARGET VIA
例子:/etc/sysconfig/network-scripts/route-eth0文件的內容:
192.168.0.10/24 via 10.247.236.1
方式2:每2行一個路由條目
ADDRESS#=IP
NETMASK#=NETMASK
GATEWAY#=NEXTHOP
例子:/etc/sysconfig/network-scripts/route-eth0文件的內容:
ADDRESS0=20.0.0.0 METMASK0=255.0.0.0 GATEWAY0=10.247.236.1
CentOS7以後才能用的命令
nmcli,nmtui
nmcli device:相當於ip link,控制介面相關的連接。
- nmcli device show
- nmcli device status
# nmcli device status DEVICE TYPE STATE CONNECTION enp0s3 ethernet connected enp0s3 virbr0 bridge connected virbr0 lo loopback unmanaged -- virbr0-nic tun unmanaged --
- nmcli device disconnect virbr0
# nmcli device disconnect virbr0 Device 'virbr0' successfully disconnected.
- nmcli device connect virbr0
# nmcli device connect virbr0 Device 'virbr0' successfully activated with '11f16b3f-e0fd-4b46-9f7f-b786feb8489b'.
nmcli connection:管理介面上的地址的。相當於ip addr命令。
- nmcli connection show
# nmcli connection show NAME UUID TYPE DEVICE enp0s3 e2d60303-d70d-4335-aabd-f6c4cf86fade ethernet enp0s3 virbr0 11f16b3f-e0fd-4b46-9f7f-b786feb8489b bridge virbr0
- nmcli connection down virbr0
# nmcli connection down virbr0 Connection 'virbr0' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
- 給網卡上添加網址
# nmcli connection modify enp0s3 +ipv4.address 10.247.236.12
- 從網卡上刪除網址
# nmcli connection modify enp0s3 -ipv4.address 10.247.236.12