Linux基础——网络管理之网络接口(NIC)管理
一、网络管理
1. 网络状态查看:ip addr(ifconfig)
1.1 net-tools(centos 7版本以前) VS iproute2(Centos 7之后推荐)
1.1.1 net-tools(centos 7版本以前)
-
ifconfig #网络接口管理命令
网络接口命名规则:CentOS 7使用一致性网络设备命名网络接口名称 网卡类型 en01
板载网卡 ens33
PCI-E网卡 enp0s3
无法获取物理网卡PCI-E网卡 eth0
除以上网卡类型
示例:
`查看所有接口信息`
[root@localhost ~]# ifconfig -a
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.5.128 netmask 255.255.255.0 broadcast 192.168.5.255
inet6 fe80::20c:29ff:fe12:dd9c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:12:dd:9c txqueuelen 1000 (Ethernet)
RX packets 373 bytes 35292 (34.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 322 bytes 48941 (47.7 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 1000 (Local Loopback)
RX packets 140 bytes 14788 (14.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 140 bytes 14788 (14.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
"网络接口启用/停用":'使用远程终端连接小心停用网络接口'
[root@localhost ~]# ifconfig [dev-name] [up|down]
dev-name :网络接口名称,如,ens32
-
route #路由设置命令
route
命令默认将IP解析成域名或服务名称,因此速度会很慢,使用route -n
可以不解析域名,加快响应速度;如下所示:
[root@localhost ~]# time route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default gateway 0.0.0.0 UG 100 0 0 eth0 192.168.5.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 real 0m0.010s user 0m0.000s sys 0m0.008s [root@localhost ~]# time route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.5.2 0.0.0.0 UG 100 0 0 eth0 192.168.5.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 real 0m0.003s user 0m0.000s sys 0m0.004s
-
netstat #网络Socket查看
1.1.2 iproute2(Centos 7之后推荐,优于net-tools)
- ip 详见:Linux 基础之基础网络ip命令
- ss 详见:Linux 基础之基础网络ss命令
1.1.3 网络接口命名修改为eth0
修改网络接口命令思路:
-
网卡命名规则受 biosdevname 和 net.ifnames 参数限制
-
修改
/etc/default/grub
文件,添加 上面参数(biosdevname=0 net.ifnames=0),如下所示:
-
使用
grub
文件重新生成/boot/grub2/grub.cfg
引导文件,注意grub.cfg文件位置[root@localhost ~]# grub2-mkconfig -o /boot/grub2/grub.cfg Generating grub configuration file ... Found linux image: /boot/vmlinuz-3.10.0-957.el7.x86_64 Found initrd image: /boot/initramfs-3.10.0-957.el7.x86_64.img Found linux image: /boot/vmlinuz-0-rescue-b21899b2f3ce4b5c8d131cd0cb02811f Found initrd image: /boot/initramfs-0-rescue-b21899b2f3ce4b5c8d131cd0cb02811f.img done
-
重启生效
reboot
-
查看修改后的网卡命名
[root@localhost ~]# ifconfig `eth0`: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.5.128 netmask 255.255.255.0 broadcast 192.168.5.255 inet6 fe80::f43f:e585:bceb:5680 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:12:dd:9c txqueuelen 1000 (Ethernet) RX packets 50 bytes 6690 (6.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 67 bytes 11488 (11.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
附表:biosdevname 和 net.ifnames 参数配置:
网卡命名 biosname net.ifnames ens33
(默认)0 0 em1
1 0 eth0
0 0 注:有的提示修改网卡配文件的NAME 参数为
eth0
,为此不需要只需要在grub下添加 biosdevname 和 net.ifnames 参数配置即可
1.1.4 查看网卡物理连接情况(即网线连接状态
):mii-tool
2. 网络配置:/etc/sysconfig/network-script/
2.1 修改网卡配置
- ifconfig <Interface 接口> <IP address> [netmask] #修改网络接口IP
- ifup <Interface 接口> #启用网络接口
- ifdown <Interface 接口> #禁用网络接口
2.2 网络命令对照:ip
命令 VS ss
命令
- ip addr # 查看网卡信息
- ifconfig
- ip link set dev eth0 up #启用网卡
- ifup eth0
- ip addr add 192.168.5.130/24 dev eth0 #给网卡添加地址
- ifconfig eth0 192.168.5.130 netmask 255.255.255.0
- ip route add 192.168.6/24 via 192.168.0.1 #添加网关
- route add -net 192.168.6.0 netmask 255.255.255.0 gw 192.168.0.1
3. 路由命令:ip route
查看路由信息:
[root@localhost ~]# ip route show|column -t
default via 192.168.5.2 dev eth0
default via 192.168.5.2 dev eth0 proto dhcp metric 100
192.168.5.0/24 dev eth0 proto kernel scope link src 192.168.5.128 metric 100
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1
4. 网络故障排除
- ping #网络连通性
- traceroute #路由状态
- mtr #丢包状态
- nslookup # 域名解析 dig
- tcpdump #抓取网络Package
- netstat #查看应用端口状态
- ss #查看应用端口状态(推荐)
[root@localhost ~]# ss -ntpl |column -t State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:111 *:* users:(("rpcbind",pid=6174,fd=4),("systemd",pid=1,fd=49)) LISTEN 0 5 192.168.122.1:53 *:* users:(("dnsmasq",pid=7165,fd=6)) LISTEN 0 128 *:22 *:* users:(("sshd",pid=6773,fd=3)) LISTEN 0 128 127.0.0.1:631 *:* users:(("cupsd",pid=6774,fd=12)) LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=7176,fd=13)) LISTEN 0 128 127.0.0.1:6010 *:* users:(("sshd",pid=6857,fd=9)) LISTEN 0 128 :::111 :::* users:(("rpcbind",pid=6174,fd=6),("systemd",pid=1,fd=51)) LISTEN 0 128 :::22 :::* users:(("sshd",pid=6773,fd=4)) LISTEN 0 128 ::1:631 :::* users:(("cupsd",pid=6774,fd=11)) LISTEN 0 100 ::1:25 :::* users:(("master",pid=7176,fd=14)) LISTEN 0 128 ::1:6010 :::* users:(("sshd",pid=6857,fd=8))