您好,登錄后才能下訂單哦!
在linux下的網絡管理大概可以通過兩種方式進行實現:命令和配置文件,大部分命令執行以后只能單次生效,而配置文件可以永久生效,根據場景的不同選擇不同的方式進行管理。
ifconfig:
ifconfig [-a] interfer 查看當前系統處于活動狀態接口
-a:顯示所有接口,包括非活動狀態。
[root@bogon yum.repos.d]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.67.128 netmask 255.255.255.0 broadcast 192.168.67.255 inet6 fe80::20c:29ff:fe74:803f prefixlen 64 scopeid 0x20<link> ether 00:0c:29:74:80:3f txqueuelen 1000 (Ethernet) RX packets 7404 bytes 6161993 (5.8 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1556 bytes 130066 (127.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eno16777736:網卡名稱
flags:標志位
UP:網卡啟用狀態
BROADCAST:支持廣播
RUNNING:正在運行中
MULTICAST:支持組播
mtu:最大傳輸單源,默認為1500
init: Ipv4地址
init6: Ipv6地址
txqueuelen:傳輸隊列長度
RX packets:接受到報文數量
RX errors:接受時的錯誤的報文數量
dropped:丟包的報文數量
overruns:溢出的報文數量
frame:幀
修改網絡配置:
[root@bogon ~]# ifconfig eno33554984 192.168.77.111/24 up [root@bogon ~]# ifconfig eno33554984 eno33554984: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::20c:29ff:fe74:8049 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:74:80:49 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 102 bytes 17988 (17.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
選項:
[-]promisc啟動/關閉混雜模式
[-]allmulti 啟動/關閉組播
up
down
route:
route 查看路由
[root@bogon ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default bogon 0.0.0.0 UG 100 0 0 eno16777736 192.168.67.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
route -n
[root@bogon ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.67.2 0.0.0.0 UG 100 0 0 eno16777736 192.168.67.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
route add [-net|-host] target [gw GW] [dev]
-net:主機路由host,目標地址為單個IP
-host:網絡路徑net,目標地址為IP網絡
[root@bogon ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default bogon 0.0.0.0 UG 100 0 0 eno16777736 192.168.67.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736 [root@bogon ~]# route add -net 12.0.0.0/8 gw 192.168.67.2 dev eno16777736 [root@bogon ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default bogon 0.0.0.0 UG 100 0 0 eno16777736 12.0.0.0 bogon 255.0.0.0 UG 0 0 0 eno16777736 192.168.67.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
[root@bogon ~]# route add -net 13.0.0.0 netmask 255.0.0.0 gw 192.168.67.4 dev eno16777736 [root@bogon ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default bogon 0.0.0.0 UG 100 0 0 eno16777736 12.0.0.0 bogon 255.0.0.0 UG 0 0 0 eno16777736 13.0.0.0 bogon 255.0.0.0 UG 0 0 0 eno16777736 192.168.67.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
route del [-net|-host] TARGET gw GATEWAY [dev IFNAME]
[root@bogon ~]# route del -net 13.0.0.0 netmask 255.0.0.0 gw 192.168.67.4 dev eno16777736 [root@bogon ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default bogon 0.0.0.0 UG 100 0 0 eno16777736 12.0.0.0 bogon 255.0.0.0 UG 0 0 0 eno16777736 192.168.67.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
netstat 命令:顯示網路連接,路由表,偽裝鏈接,多播成員關系
[root@bogon ~]# netstat Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 bogon:ssh bogon:63059 ESTABLISHED tcp 0 52 bogon:ssh bogon:54942 ESTABLISHED
netstat -r:顯示內核路由表
netstat -n: 數字格式,不反解IP地址至主機名;
[root@bogon ~]# netstat -n Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 192.168.67.128:22 192.168.67.1:63059 ESTABLISHED tcp 0 52 192.168.67.128:22 192.168.67.1:54942 ESTABLISHED
netstat -t:TCP協議的相關連接,鏈接均有其狀態
[root@bogon ~]# netstat -t Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 bogon:ssh bogon:63059 ESTABLISHED tcp 0 52 bogon:ssh bogon:54942 ESTABLISHED
netstat -u:udp協議的相關連接
[root@bogon ~]# netstat -u Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State
netstat -w:raw socket相關的鏈接
netstat -l:處于監聽的鏈接
[root@bogon yum.repos.d]# netstat -l Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN tcp 0 0 localhost:ipp 0.0.0.0:* LISTEN tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN
Proto 協議
Recv-Q 接受隊列
Send-Q 發送隊列
netstat -a:所有狀態 (運行,監聽)
netstat -n:以數字格式顯示IP和Port
netstat -e:顯示擴展格式
[root@bogon ~]# netstat -e Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State User Inode tcp 0 52 bogon:ssh bogon:56488 ESTABLISHED root
netstat -i 顯示所有接口
[root@bogon ~]# netstat -i Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eno16777 1500 300 0 0 0 137 0 0 0 BMRU
netstat -I<inteferce>
[root@bogon ~]# netstat -Ieno33554984 Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eno33554 1500 0 0 0 0 140 0 0 0 BMRU
ifup /ifdown 打開、關閉網卡
[root@bogon network-scripts]# ifup eno33554984 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/21) [root@bogon network-scripts]# ifdown eno33554984 Device 'eno33554984' successfully disconnected.
housname
CentOS6:
查看主機名:hostname
配置主機名:hostname HOSTNAME
通過修改配置文件配置文件:/etc/sysconfig/network 中 HOSTNAME=<HOSTNAME>
[root@localhost ~]# vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=localhost.localdomain
CentOS7:
hostnamectl status:顯示當前主機名設定:
[root@bogon ~]# hostnamectl status Static hostname: localhost.localdomain Transient hostname: bogon Icon name: computer-vm Chassis: vm Machine ID: 5428a4d5a4bb46b8a5d02ecc7d30d003 Boot ID: c86d23aa109846569cf578d31f2b25e5 Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-327.el7.x86_64 Architecture: x86-64
hostnamectl set-hostname 設定主機名,永久有效;
Unknown operation set-lin [root@bogon ~]# hostnamectl set-hostname lin [root@bogon ~]# hostnamectl Static hostname: lin Icon name: computer-vm Chassis: vm Machine ID: 5428a4d5a4bb46b8a5d02ecc7d30d003 Boot ID: c86d23aa109846569cf578d31f2b25e5 Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-327.el7.x86_64 Architecture: x86-64
ip:
ip link 網絡設備配置
ip link set interface down
[root@bogon ~]# ip link set eno33554984 down [root@bogon ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.67.128 netmask 255.255.255.0 broadcast 192.168.67.255 inet6 fe80::20c:29ff:fe74:803f prefixlen 64 scopeid 0x20<link> ether 00:0c:29:74:80:3f txqueuelen 1000 (Ethernet) RX packets 15526 bytes 1106373 (1.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5354 bytes 933749 (911.8 KiB) 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 0 (Local Loopback) RX packets 12 bytes 1020 (1020.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 12 bytes 1020 (1020.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip link set interface up
[root@bogon ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.67.128 netmask 255.255.255.0 broadcast 192.168.67.255 inet6 fe80::20c:29ff:fe74:803f prefixlen 64 scopeid 0x20<link> ether 00:0c:29:74:80:3f txqueuelen 1000 (Ethernet) RX packets 15568 bytes 1109751 (1.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5378 bytes 937177 (915.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eno33554984: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:74:80:49 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 254 bytes 43248 (42.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip link set interface multicast on 開啟組播
ip link set interface multicastoff 關閉組播
[root@bogon network-scripts]# ip link set eno33554984 multicast off [root@bogon network-scripts]# ifconfig eno33554984 eno33554984: flags=67<UP,BROADCAST,RUNNING> mtu 1500 ether 00:0c:29:74:80:49 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 254 bytes 43248 (42.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@bogon network-scripts]# ip link set eno33554984 multicast on [root@bogon network-scripts]# ifconfig eno33554984 eno33554984: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:74:80:49 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 254 bytes 43248 (42.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip link set interface name NAME 更改設備名稱
[root@bogon network-scripts]# ip link set eno33554984 name eth0 [root@bogon network-scripts]# ifconfig eth0 eth0: flags=4098<BROADCAST,MULTICAST> mtu 1500 ether 00:0c:29:74:80:49 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 254 bytes 43248 (42.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip link set interface mtu 設置MTU值,默認1500
root@bogon network-scripts]# ip link set eth0 mtu 1600 [root@bogon network-scripts]# ifconfig eth0 eth0: flags=4098<BROADCAST,MULTICAST> mtu 1600 ether 00:0c:29:74:80:49 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 254 bytes 43248 (42.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip netns :創建指定的空間
ip netns list 累出所有的netns
ip netns add name 增加指定的netns
ip netns del name 刪除指定的netns
ip netns exec NAME COMMAND指定的netns中運行命令
[root@bogon network-scripts]# ip netns list [root@bogon network-scripts]# ip netns add test [root@bogon network-scripts]# ip netns list test [root@bogon network-scripts]# ip link set eth0 netns test [root@bogon network-scripts]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.67.128 netmask 255.255.255.0 broadcast 192.168.67.255 inet6 fe80::20c:29ff:fe74:803f prefixlen 64 scopeid 0x20<link> ether 00:0c:29:74:80:3f txqueuelen 1000 (Ethernet) RX packets 18036 bytes 1295528 (1.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6286 bytes 1033927 (1009.6 KiB) 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 0 (Local Loopback) RX packets 12 bytes 1020 (1020.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 12 bytes 1020 (1020.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@bogon network-scripts]# ip netns del test [root@bogon network-scripts]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.67.128 netmask 255.255.255.0 broadcast 192.168.67.255 inet6 fe80::20c:29ff:fe74:803f prefixlen 64 scopeid 0x20<link> ether 00:0c:29:74:80:3f txqueuelen 1000 (Ethernet) RX packets 18104 bytes 1301156 (1.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6323 bytes 1038797 (1014.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eno33554984: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1600 inet 192.168.66.123 netmask 255.255.255.0 broadcast 192.168.66.255 inet6 fe80::20c:29ff:fe74:8049 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:74:80:49 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 262 bytes 43868 (42.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip addr:用來實現管理IP地址
ip addr add IP/MASK dev label
[root@lin ~]# ip addr add dev eth0 192.168.77.78/24 label eth0:1 [root@lin ~]# ip addr add dev eth0 192.168.77.79/24 label eth0:2 [root@lin ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.67.128 netmask 255.255.255.0 broadcast 192.168.67.255 inet6 fe80::20c:29ff:fe74:803f prefixlen 64 scopeid 0x20<link> ether 00:0c:29:74:80:3f txqueuelen 1000 (Ethernet) RX packets 19978 bytes 1455021 (1.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7267 bytes 1146820 (1.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1600 inet 192.168.77.74 netmask 255.255.255.0 broadcast 0.0.0.0 ether 00:0c:29:74:80:49 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 266 bytes 44112 (43.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1600 inet 192.168.77.78 netmask 255.255.255.0 broadcast 0.0.0.0 ether 00:0c:29:74:80:49 txqueuelen 1000 (Ethernet) eth0:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1600 inet 192.168.77.79 netmask 255.255.255.0 broadcast 0.0.0.0 ether 00:0c:29:74:80:49 txqueuelen 1000 (Ethernet)
ip addr delete 刪
ip addr show 顯示所有路由信息
[root@lin ~]# ip addr delete dev eth0 192.168.77.77/24 [root@lin ~]# ifconfig eth0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1600 inet 192.168.77.74 netmask 255.255.255.0 broadcast 0.0.0.0 ether 00:0c:29:74:80:49 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 266 bytes 44112 (43.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip addr flush 清空所有
ip route
ip route option TPYE PREFIX via[GW] [dev IFACE] [src SOUCE]
ip route add 添加路由
[root@lin ~]# ip route add 10.0.0.0/8 via 192.168.67.1 [root@lin ~]# ip route show default via 192.168.67.2 dev eno16777736 proto static metric 100 10.0.0.0/8 via 192.168.67.1 dev eno16777736
ip route delete:刪除路由
[root@lin ~]# ip route del 10.0.0.0/8 via 192.168.67.1 [root@lin ~]# ip route show default via 192.168.67.2 dev eno16777736 proto static metric 100 192.168.67.0/24 dev eno16777736 proto kernel scope link src 192.168.67.128 metric 100 192.168.77.0/24 dev eth0 proto kernel scope link src 192.168.77.74
ip route get:獲得路由嘻嘻
[root@lin ~]# ip route get 192.168.67.0 broadcast 192.168.67.0 dev eno16777736 src 192.168.67.128 cache <local,brd>
ip route flush
ss
ss [options] [FILTER]
-t:TCP協議的相關連接
-u: UDP相關連接
-w: raw socket相關的鏈接
-l:監聽狀態的了解
-n:數字格式
-p:相關的程序及其PID
-e:擴展格式信息
-m:內存用量
[root@lin ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:631 *:*
-o:及其他信息
dport=目標端口
sport=源端口
示例:'( dport=:22 sport=:22 ) '
ss -tan '( dport=:22 or sport=:22 )'
ss -tan state ESTABLISHED
通過修改配置文件來配置網絡屬性:
IP/NETMASK/GW/DNS:
/etc/sysconfig/network-scripts/ifcfg-IFACE
IFACE:接口名稱;
路由的相關配置文件:
/etc/sysconfig/network-scripts/route-IFACE
支持兩種配置方式,但不可混用
(1)每行一個路由條目
TARGET via GW
(2)沒三行一個路由條目
ADDRESS#=TARGER
NETWORK#=MASK
GATEWAY#=NEXT
配置DNS服務器指向:
配置文件: /etc/resolv.conf 最多有三個主機
nameserver DNS_SERVER_IP
如何測試:(host/nslookup/dig)
/etc/hosts
系統中ping命令是事顯查看/etc/hosts文件中是否有IP和域名對應的,沒有的話在查看resolv.conf,
因此測試的時候要跳開host文件
dig -t A 主機名 FQDN(www.baidu.com)
FQDN--> IP
dig -x IP
IP--> 主機名
nmcli
device:顯示并且網絡接口
nmcli device status
nmcli device connect
nmcli device disconnect
nmcli device show
connection:
nmcli connection show
nmcli connection up IFACE
nmcli connectionn down IFACE
nmcli connectionn modify IFACE [+|-]setting address vlue
setting:
ipv4.addresss
ipv4.gateway
ipv4.dns1
ipv4.method 配置方法
manual static?
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。