您好,登錄后才能下訂單哦!
在linux下的網(wǎng)絡(luò)管理大概可以通過(guò)兩種方式進(jìn)行實(shí)現(xiàn):命令和配置文件,大部分命令執(zhí)行以后只能單次生效,而配置文件可以永久生效,根據(jù)場(chǎng)景的不同選擇不同的方式進(jìn)行管理。
ifconfig:
ifconfig [-a] interfer 查看當(dāng)前系統(tǒng)處于活動(dòng)狀態(tài)接口
-a:顯示所有接口,包括非活動(dòng)狀態(tài)。
[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:網(wǎng)卡名稱
flags:標(biāo)志位
UP:網(wǎng)卡啟用狀態(tài)
BROADCAST:支持廣播
RUNNING:正在運(yùn)行中
MULTICAST:支持組播
mtu:最大傳輸單源,默認(rèn)為1500
init: Ipv4地址
init6: Ipv6地址
txqueuelen:傳輸隊(duì)列長(zhǎng)度
RX packets:接受到報(bào)文數(shù)量
RX errors:接受時(shí)的錯(cuò)誤的報(bào)文數(shù)量
dropped:丟包的報(bào)文數(shù)量
overruns:溢出的報(bào)文數(shù)量
frame:幀
修改網(wǎng)絡(luò)配置:
[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
選項(xiàng):
[-]promisc啟動(dòng)/關(guān)閉混雜模式
[-]allmulti 啟動(dòng)/關(guān)閉組播
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:主機(jī)路由host,目標(biāo)地址為單個(gè)IP
-host:網(wǎng)絡(luò)路徑net,目標(biāo)地址為IP網(wǎng)絡(luò)
[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 命令:顯示網(wǎng)路連接,路由表,偽裝鏈接,多播成員關(guān)系
[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:顯示內(nèi)核路由表
netstat -n: 數(shù)字格式,不反解IP地址至主機(jī)名;
[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協(xié)議的相關(guān)連接,鏈接均有其狀態(tài)
[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協(xié)議的相關(guān)連接
[root@bogon ~]# netstat -u Active Internet connections (w/o servers) Proto Recv-Q Send-Q Local Address Foreign Address State
netstat -w:raw socket相關(guān)的鏈接
netstat -l:處于監(jiān)聽(tīng)的鏈接
[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 協(xié)議
Recv-Q 接受隊(duì)列
Send-Q 發(fā)送隊(duì)列
netstat -a:所有狀態(tài) (運(yùn)行,監(jiān)聽(tīng))
netstat -n:以數(shù)字格式顯示IP和Port
netstat -e:顯示擴(kuò)展格式
[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 打開(kāi)、關(guān)閉網(wǎng)卡
[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:
查看主機(jī)名:hostname
配置主機(jī)名:hostname HOSTNAME
通過(guò)修改配置文件配置文件:/etc/sysconfig/network 中 HOSTNAME=<HOSTNAME>
[root@localhost ~]# vim /etc/sysconfig/network NETWORKING=yes HOSTNAME=localhost.localdomain
CentOS7:
hostnamectl status:顯示當(dāng)前主機(jī)名設(shè)定:
[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 設(shè)定主機(jī)名,永久有效;
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 網(wǎng)絡(luò)設(shè)備配置
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 開(kāi)啟組播
ip link set interface multicastoff 關(guān)閉組播
[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 更改設(shè)備名稱
[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 設(shè)置MTU值,默認(rèn)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 :創(chuàng)建指定的空間
ip netns list 累出所有的netns
ip netns add name 增加指定的netns
ip netns del name 刪除指定的netns
ip netns exec NAME COMMAND指定的netns中運(yùn)行命令
[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:用來(lái)實(shí)現(xiàn)管理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協(xié)議的相關(guān)連接
-u: UDP相關(guān)連接
-w: raw socket相關(guān)的鏈接
-l:監(jiān)聽(tīng)狀態(tài)的了解
-n:數(shù)字格式
-p:相關(guān)的程序及其PID
-e:擴(kuò)展格式信息
-m:內(nèi)存用量
[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=目標(biāo)端口
sport=源端口
示例:'( dport=:22 sport=:22 ) '
ss -tan '( dport=:22 or sport=:22 )'
ss -tan state ESTABLISHED
通過(guò)修改配置文件來(lái)配置網(wǎng)絡(luò)屬性:
IP/NETMASK/GW/DNS:
/etc/sysconfig/network-scripts/ifcfg-IFACE
IFACE:接口名稱;
路由的相關(guān)配置文件:
/etc/sysconfig/network-scripts/route-IFACE
支持兩種配置方式,但不可混用
(1)每行一個(gè)路由條目
TARGET via GW
(2)沒(méi)三行一個(gè)路由條目
ADDRESS#=TARGER
NETWORK#=MASK
GATEWAY#=NEXT
配置DNS服務(wù)器指向:
配置文件: /etc/resolv.conf 最多有三個(gè)主機(jī)
nameserver DNS_SERVER_IP
如何測(cè)試:(host/nslookup/dig)
/etc/hosts
系統(tǒng)中ping命令是事顯查看/etc/hosts文件中是否有IP和域名對(duì)應(yīng)的,沒(méi)有的話在查看resolv.conf,
因此測(cè)試的時(shí)候要跳開(kāi)host文件
dig -t A 主機(jī)名 FQDN(www.baidu.com)
FQDN--> IP
dig -x IP
IP--> 主機(jī)名
nmcli
device:顯示并且網(wǎng)絡(luò)接口
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?
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。