網絡配置
rhel/Centos中常用的網絡管理指令:
設定ip基本資訊相關配置檔案
5,6版本:system-config-network(setup)
配置檔案位置:/etc/sysconfig/network-scripts/ifcfg-eth[0-n]
7版本:nmcil nmtui(圖形界面)
配置檔案位置:/etc/sysconfig/network-scripts/ifcfg-en*****
ifcfg-eth0,ifcfg-en******網絡配置檔案資訊
TYPE=Ethernet #接口類型為以太網卡 BOOTPROTO=none #擷取位址協定dhcp(自動擷取),static(手動配置),none HWADDR=00:0C:29:75:A1:89 #網卡MAC UUID=be9d332f-ae79-42b6-8040-d2d431e4e195 #網卡辨別 #DEVICE=eno16777736 #7系列的網卡名稱 DEVICE=eth0 #5,6系列的網卡名 ONBOOT=yes #開機自動激活網卡,no不激活 IPADDR=10.76.249.91 GATEWAY=10.76.249.2 NETMASK=255.255.255.0 DNS=8.8.4.4 |
配置IP位址
臨時添加IP位址,重新開機後失效
ip addr add 192.168.1.150/24 dev eth0 或者 # ifconfig eth0 192.168.1.150/24 up #重新開機後會失效,第一張網卡為eth0,第二張為eth1依此類推 # ifconfig eth0 1.1 Link encap:Ethernet HWaddr 00:0C:29:75:A1:89 inet addr:192.168.1.150 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe75:a189/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5682 errors:0 dropped:0 overruns:0 frame:0 TX packets:4136 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:412252 (402.5 KiB) TX bytes:4464715 (4.2 MiB) |
配置預設路由
# ip route add default via 192.168.1.1 #default via 192.168.1.1 devinterface |
網卡相關狀态檢視
1.檢視IP
# ip addr show 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 disc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host 2: eno16777736: <BROADCAST,MULTICAST,1UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 200:0c:29:e1:c5:8a brd ff:ff:ff:ff:ff:ff inet 310.76.249.91/24 brd 10.76.249.255 scope global eno16777736 inet6 fe80::20c:29ff:fee1:c58a/64 scope link 1:網卡狀态已經啟動 2:網卡的mac位址 3:網卡”eno16777736”位址 # ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:75:A1:89 inet addr:192.168.1.150 Bcast:192.168.1.255 Mask:255.255.255.0 RX packets:6386 errors:0 dropped:0 overruns:0 frame:0 TX packets:4436 errors:0 dropped:0 overruns:0 carrier:0 RX bytes:467110 (456.1 KiB) TX bytes:4502451 (4.2 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:66 errors:0 dropped:0 overruns:0 frame:0 TX packets:66 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:3482 (3.4 KiB) TX bytes:3482 (3.4 KiB) # nmcli dev show eth0 GENERAL.裝置: eth0 GENERAL.類型: ethernet GENERAL.硬碟: 00:0C:29:E1:C5:8A GENERAL.MTU: 1500 GENERAL.狀态: 100 (連接配接的) GENERAL.CONNECTION: eno16777736 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 WIRED-PROPERTIES.容器: 開 IP4.位址[1]: 10.76.249.91/24 IP4.網關: 10.76.249.2 IP6.位址[1]: fe80::20c:29ff:fee1:c58a/64 IP6.網關: |
2.檢視收發包狀态
# ip -s link show eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:75:a1:89 brd ff:ff:ff:ff:ff:ff RX: bytes packets errors dropped overrun mcast 477844 6528 0 0 0 0 TX: bytes packets errors dropped carrier collsns 4508951 4492 0 0 0 0 # ifconfig –s eth0 Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 6459 0 0 0 4455 0 0 0 BMRU |
3.顯示顯示主機預設路由
#ip route default via 10.76.249.2 dev eth0 proto static metric 100 10.76.249.0/24 dev eth0 proto kernel scope link src 10.76.249.91 metric 100 |
4.顯示連接配接狀态
常用到的選項
ss和netstat使用方法相似,但是ss效率要比較高,消耗性能比較少。
選項 | 功能 |
-l | 監聽狀态的連接配接 |
-n | 不解析服務名以數字顯示 |
-t | 顯示TCP連接配接 |
-u | 顯示UDP連接配接 |
-a | 顯示所有狀态 |
-p | 顯示以哪個程序發起的連接配接 |
常用的組合
-tan,-tanl, -tanlp, -uan
幾種狀态資訊
State: LISTEN(監聽),ESTAB(已建立的連結)
# ss -tna State Recv-Q Send-Q Local Address:Port 本地端口 Peer Address:Port 用戶端端口 LISTEN 0 128 :::22 :::* LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:6010 *:* LISTEN 0 128 ::1:6010 :::* LISTEN 0 128 127.0.0.1:6011 *:* LISTEN 0 128 ::1:6011 :::* LISTEN 0 128 127.0.0.1:6012 *:* LISTEN 0 128 ::1:6012 :::* LISTEN 0 128 127.0.0.1:6013 *:* LISTEN 0 128 ::1:6013 :::* ESTAB 0 0 192.168.1.106:22 192.168.1.103:63167 ESTAB 0 0 192.168.1.150:22 192.168.1.103:63753 ESTAB 0 52 192.168.1.150:22 192.168.1.103:54953 ESTAB 0 0 192.168.1.106:22 192.168.1.103:63134 |
Centos/Redhat 7系列回歸傳統網卡命名
- 修改網卡檔案内容
# cat /etc/sysconfig/network-scripts/ifcfg- eno16777736 TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes PEERDNS=yes PEERROUTES=yes NAME=eno16777736 UUID=be9d332f-ae79-42b6-8040-d2d431e4e195 #DEVICE=eno16777736 #注釋掉原來的命名 DEVICE=eth0 #傳統命名 ONBOOT=yes |
- 重命名網卡檔案
# mv /etc/sysconfig/network-scripts/ifcfg-eno16777736 /etc/sysconfig/network-scripts/ifcfg-eth0 |
- 編輯/etc/default/grub配置檔案
# vim /etc/default/grub GRUB_TIMEOUT=5 GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" #GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet" #修改前 GRUB_CMDLINE_LINUX="net.ifnames=0 rhgb quiet" #修改後 GRUB_DISABLE_RECOVERY="true" |
- 為grub2生成其配置檔案
# grub2-mkconfig -o /etc/grub2.cfg Generating grub configuration file ... Found linux p_w_picpath: /boot/vmlinuz-3.10.0-229.el7.x86_64 Found initrd p_w_picpath: /boot/initramfs-3.10.0-229.el7.x86_64.img Found linux p_w_picpath: /boot/vmlinuz-0-rescue-16de415bcbd643d886010c2fe954d9cb Found initrd p_w_picpath: /boot/initramfs-0-rescue-16de415bcbd643d886010c2fe954d9cb.img done |
- 重新開機系統
- 驗證結果
# ip addr show eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:e1:c5:8a brd ff:ff:ff:ff:ff:ff inet 10.76.249.91/24 brd 10.76.249.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fee1:c58a/64 scope link valid_lft forever preferred_lft forever |
nmcli
nmcli:網絡管理指令行接口:NetworkManagerCommand-Line Interface
nmtui:網絡管理文本使用者接口:NetworkManager Text-User Interface
RHEL7之後的網絡配置指令
[root@localhost conf.d]# nmcli --help Usage: nmcli [OPTIONS] OBJECT { COMMAND | help } OPTIONS -t[erse] terse output -p[retty] pretty output -m[ode] tabular|multiline output mode -f[ields] <field1,field2,...>|all|common specify fields to output -e[scape] yes|no escape columns separators in values -n[ocheck] don't check nmcli and NetworkManager versions -a[sk] ask for missing parameters -w[ait] <seconds> set timeout waiting for finishing operations -v[ersion] show program version -h[elp] print this help OBJECT g[eneral] NetworkManager's general status and operations n[etworking] overall networking control r[adio] NetworkManager radio switches c[onnection] NetworkManager's connections 簡寫為c d[evice] devices managed by NetworkManager 簡寫為d a[gent] NetworkManager secret agent or polkit agent |
重新加載網絡配置檔案
[root@localhost ~]# nmcli c reload
[root@localhost ~]# nmcli c load/etc/sysconfig/network-scripts/ifcfg-eth0
[root@localhost ~]# nmcli con mod eno16777736 ipv4.addresses '192.168.1.119/24' 網卡名使用檢視# nmcli con show |
配置DNS
[root@localhost ~]# nmcli con mod eno16777736 ipv4.dns '8.8.4.4' |
配置網關
[root@localhost ~]# nmcli con mod eno16777736 ipv4.gateway '192.168.1.1' |
停啟用網卡
[root@localhost ~]# nmcli dev dis eth0 dis 為 disconnect [root@localhost ~]# nmcli con up eth0 |
檢視相關資訊
[root@localhost ~]# nmcli device status DEVICE TYPE STATE CONNECTION eth0 ethernet connected eno16777736 lo loopback unmanaged -- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
nmtui 圖形配置界面比較簡潔
[root@localhost ~]# nmtui
