网卡配置文件和命令
1.查看网卡:
[root@centos8 ~ 820]#ethtool -i ens160 driver: vmxnet3 version: 1.5.0.0-k-NAPI firmware-version: expansion-rom-version: bus-info: 0000:03:00.0 supports-statistics: yes supports-test: no supports-eeprom-access: no supports-register-dump: yes supports-priv-flags: no
2.临时修改网卡名:
[root@centos8 ~ 822]#ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 valid_lft forever preferred_lft forever 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:92:02:99 brd ff:ff:ff:ff:ff:ff inet 192.168.93.129/24 brd 192.168.93.255 scope global dynamic noprefixroute ens160 valid_lft 1786sec preferred_lft 1786sec inet6 fe80::20c:29ff:fe92:299/64 scope link dadfailed tentative noprefixroute valid_lft forever preferred_lft forever 3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 52:54:00:93:80:32 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global noprefixroute virbr0 valid_lft forever preferred_lft forever 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000 link/ether 52:54:00:93:80:32 brd ff:ff:ff:ff:ff:ff 5: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:92:02:a3 brd ff:ff:ff:ff:ff:ff [root@centos8 ~ 823]#ip link set ens224 down [root@centos8 ~ 824]#ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 valid_lft forever preferred_lft forever 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:92:02:99 brd ff:ff:ff:ff:ff:ff inet 192.168.93.129/24 brd 192.168.93.255 scope global dynamic noprefixroute ens160 valid_lft 1722sec preferred_lft 1722sec inet6 fe80::20c:29ff:fe92:299/64 scope link dadfailed tentative noprefixroute valid_lft forever preferred_lft forever 3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 52:54:00:93:80:32 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global noprefixroute virbr0 valid_lft forever preferred_lft forever 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000 link/ether 52:54:00:93:80:32 brd ff:ff:ff:ff:ff:ff 5: ens224: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN group default qlen 1000 link/ether 00:0c:29:92:02:a3 brd ff:ff:ff:ff:ff:ff [root@centos8 ~ 825]#ip link set ens224 name abc [root@centos8 ~ 826]#ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 valid_lft forever preferred_lft forever 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:92:02:99 brd ff:ff:ff:ff:ff:ff inet 192.168.93.129/24 brd 192.168.93.255 scope global dynamic noprefixroute ens160 valid_lft 1666sec preferred_lft 1666sec inet6 fe80::20c:29ff:fe92:299/64 scope link dadfailed tentative noprefixroute valid_lft forever preferred_lft forever 3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 52:54:00:93:80:32 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global noprefixroute virbr0 valid_lft forever preferred_lft forever 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000 link/ether 52:54:00:93:80:32 brd ff:ff:ff:ff:ff:ff 5: abc: <BROADCAST,MULTICAST> mtu 1500 qdisc mq state DOWN group default qlen 1000 link/ether 00:0c:29:92:02:a3 brd ff:ff:ff:ff:ff:ff [root@centos8 ~ 827]#ip link set abc up [root@centos8 ~ 828]#ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 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 valid_lft forever preferred_lft forever 2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:92:02:99 brd ff:ff:ff:ff:ff:ff inet 192.168.93.129/24 brd 192.168.93.255 scope global dynamic noprefixroute ens160 valid_lft 1644sec preferred_lft 1644sec inet6 fe80::20c:29ff:fe92:299/64 scope link dadfailed tentative noprefixroute valid_lft forever preferred_lft forever 3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 link/ether 52:54:00:93:80:32 brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global noprefixroute virbr0 valid_lft forever preferred_lft forever 4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000 link/ether 52:54:00:93:80:32 brd ff:ff:ff:ff:ff:ff 5: abc: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:92:02:a3 brd ff:ff:ff:ff:ff:ff
3.ifconfig命令
来自于net-tools包,建议使用 ip 代替
ifconfig修改网卡IP地址:
[root@centos8 ~ 834]#ifconfig ens224 1.1.1.1/24 [root@centos8 ~ 835]#ifconfig ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.93.129 netmask 255.255.255.0 broadcast 192.168.93.255 inet6 fe80::20c:29ff:fe92:299 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:92:02:99 txqueuelen 1000 (Ethernet) RX packets 38146 bytes 51665005 (49.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7397 bytes 510018 (498.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens224: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 1.1.1.1 netmask 255.255.255.0 broadcast 1.1.1.255 ether 00:0c:29:92:02:a3 txqueuelen 1000 (Ethernet) RX packets 4 bytes 424 (424.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 9 bytes 891 (891.0 B) 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 2 bytes 140 (140.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2 bytes 140 (140.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:93:80:32 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig禁用网卡:
[root@centos8 ~ 838]#ifconfig ens224 down [root@centos8 ~ 839]#ifconfig ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.93.129 netmask 255.255.255.0 broadcast 192.168.93.255 inet6 fe80::20c:29ff:fe92:299 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:92:02:99 txqueuelen 1000 (Ethernet) RX packets 38264 bytes 51677384 (49.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7469 bytes 523618 (511.3 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 2 bytes 140 (140.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2 bytes 140 (140.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:93:80:32 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig启用网卡:
[root@centos8 ~ 840]#ifconfig ens224 up [root@centos8 ~ 841]#ifconfig ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.93.129 netmask 255.255.255.0 broadcast 192.168.93.255 inet6 fe80::20c:29ff:fe92:299 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:92:02:99 txqueuelen 1000 (Ethernet) RX packets 38414 bytes 51692621 (49.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7566 bytes 535881 (523.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens224: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 00:0c:29:92:02:a3 txqueuelen 1000 (Ethernet) RX packets 1 bytes 106 (106.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3 bytes 126 (126.0 B) 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 2 bytes 140 (140.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 2 bytes 140 (140.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:93:80:32 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig查看当前网卡的通信情况:
[root@centos8 ~ 843]#ifconfig -s ens160 Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg ens160 1500 38500 0 0 0 7623 0 0 0 BMRU
4.netstat命令
来自于net-tools包,建议使用 ss 代替
常用选项
-t: tcp协议相关 -u: udp协议相关 -w: raw socket相关 -l: 处于监听状态 -a: 所有状态 -n: 以数字显示IP和端口 -e:扩展格式 -p: 显示相关进程及PID
常用组合:
tan, -uan, -tnl, -unl
显示路由表:
netstat {--route|-r} [--numeric|-n] -r: 显示内核路由表 -n: 数字格式
5.将网卡名修改为传统网卡名:
vim /etc/default/grub
再执行
grub2-mkconfig -o /boot/grub2/grub.cfg ;reboot
6.网络配置文件
在/etc/sysconfig/network-scripts/目录下有不少文件,绝大部分都是脚本类的文件,但有一类ifcfg开头的文件为网卡配置文件(interface-config),所有ifcfg开头的文件在启动网络服务的时候都会被加载读取,具体的文件名ifcfg-XXX的XXX可以随意命名。
通过网卡配置文件可设定IP地址、子网掩码、默认网关、DNS且永久生效。
该文件主要内容如下:
DEVICE:此配置文件应用到的设备,也是显示名称,必须/sys/class/net/目录下的某个网卡名相同;
HWADDR:对应的设备的MAC地址;
BOOTPROTO:激活此设备时使用的地址配置协议,常用的dhcp,static,none,bootp;
NM_CONTROLLED:NM是NetworkManager的简写,表示此网卡是否接受NM管理,centOS6建议设置为“no”;
ONBOOT:在系统启动时是否激活此设备,可选值为yes或no;若设置为no,则需手动启动;
TYPE:设定接口类型;常见的有Ethernet,Bridge;
UUID:设备的唯一标识;
IPADDR:指明IP地址;
NETMASK:指明子网掩码;
GATEWAY:设定默认网关;
DNS1:设定第一个DNS服务器指向;
DNS2:设定第二个DNS服务器指向;
USERCTL:普通用户是否控制此设备;
PEERDNS:设置为yes时,此文件设置的DNS将覆盖/etc/resolv.conf,若BOOTPROTO的值是“dhcp”,则该项默认为yes,所以dhcp的dns也会覆盖/etc/resolv.conf;
PEERROUTES:设置为yes时,DHCP设置的路由将覆盖route-ethX文件中的永久路由信息;
DEFROUTTE:设置为yes时,DHCP将设置默认路由。
7.查看和修改主机名
1).CentOS6中的主机名
在centOS6中使用hostname命令可以查看和修改主机名,修改时只在当前生效。
语法格式为:
hostname 【hostname】
centOS6中主机名修改如果想要永久生效,需修改配置文件/etc/sysconfig/network,其中:
HOSTNAME=
等号后填写想要设定的主机名,保存退出即可。
最后,hostname命令还有一个非常好用的选项“-I”(大写字母i),它可以列出本机除了环回地址外的所有地址,如此以来想要筛选IP地址就可以免去ifconfig加grep、sed等一堆联合操作了。
[root@localhost ~]# hostname -I 192.168.93.133 192.168.122.1 fd15:4ba5:5a2b:1008:20c:29ff:fe9f:3f73
2).centOS7中的主机名
在centOS6中,修改主机名方式很简单,临时修改主机名使用hostname命令,永久修改主机名直接写文件/etc/sysconfig/network即可。
但在CentOS7中,主机名就没那么简单了,它涉及了一些弯弯绕绕。如果化繁为简,那么直接使用hostnamectl命令查看、修改即可,它是永久生效的。此外,修改/etc/hostname也是永久生效的,而/etc/sysconfig/network在CentOS7中已经失效。
例如,hostnamectl命令设置主机名为kimmich:
[root@centos7 ~]# hostnamectl set-hostname kimmich
通过/etc/hostname查看或设置主机名:
[root@centos7 ~]# cat /etc/hostname kimmich
查看主机名:
[root@centos7 ~]# uname -n kimmich [root@centos7 ~]# hostname kimmich
hostnamectl命令也可以直接查看主机名,但是显示出来的结果及较多,这涉及到CentOS7中主机名的弯弯绕绕。
[root@centos7 ~]# hostnamectl Static hostname: kimmich Icon name: computer-vm Chassis: vm Machine ID: b57bc3ff91254e2dbb9727e060abf5c1 Boot ID: 07d449f8658b48a9855430e5144eb4c6 Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.el7.x86_64 Architecture: x86-64
hostnamectl命令话可以修改、查看远程主机的主机名,使用“-H”或“--host”选项即可,它基于ssh连接。
注意,无法远程修改CentOS5或6的主机名,因为hostnamectl使用的是systemd的功能进行修改的。
例如,使用root用户连接到192.168.93.133主机上并修改它的主机名。
[root@centos7 ~]# hostnamectl -H root@192.168.93.133 set-hostname hello59 The authenticity of host '192.168.93.133 (192.168.93.133)' can't be established. ECDSA key fingerprint is SHA256:yiPvjpdo3eOQjKTLAspCqRsTikjuP8Dsym5YsYvUr1s. ECDSA key fingerprint is MD5:89:da:3a:a0:a8:53:11:23:ff:88:78:ca:d7:96:6a:6a. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.93.133' (ECDSA) to the list of known hosts. root@192.168.93.133's password: [root@centos7 ~]# hostnamectl -H root@192.168.93.133 status root@192.168.93.133's password: Static hostname: hello59 Icon name: computer-vm Chassis: vm Machine ID: b57bc3ff91254e2dbb9727e060abf5c1 Boot ID: 07d449f8658b48a9855430e5144eb4c6 Virtualization: vmware Operating System: CentOS Linux 8 CPE OS Name: cpe:/o:centos:centos:8 Kernel: Linux 4.18.0-348.el8.x86_64 Architecture: x86-64
最后再来解释下CentOS7中的主机名。
在CentOS7中主机名分为三类:static(静态主机名)、pretty(好看易读的主机名)和transient(短暂临时的)。CentOS7中和主机名有关的文件为/etc/hostname,它是在系统初始化的时候被读取的,并且内核根据它的内容设置transient主机名。
其中:
1.static类的主机名就是原来意义上的主机名,由/etc/hostname文件决定。
2.transient类的主机名是临时主机名,它是由内核动态维护的主机名。默认在系统启动的时候根据/etc/hostname文件中的静态主机名进行初始化。
3.pretty类的主机名是给人看的,它可以提供非标准的主机名,以前版本(例如CentOS6)没有这功能。它可以包含特殊符号,例如空格。例如将pretty名称名为“MaYun‘s Host”,这种名称在以前的主机名(即static类主机名)里是不允许存在的。
/etc/hostname文件中的static主机名是瞬时生效的也是永久生效的。修改后使用hostname命令或者uname -n直接就可以读取,重启后也按照此文件的主机名进行初始化。
/etc/hostname文件没有主机名的时候,在系统启动的时候,内核会将transient初始化为localhost.localdoamin。
hostname命令修改的主机名是transient主机名,即临时生效的主机名。
使用hostnamectl命令。它可以修改并查看static、transient或pretty三种主机名。当它修改了static主机名时,会直接写入/etc/hostname文件中,因此它也是瞬时生效+永久生效的。
8.主机名解析
解析器执行正向和逆向查询,即主机名和IP的双向查询。解析器存放路径位于/etc/hosts,是本地主机名和IP地址的映像,对小型独立网络有用。
使用getent hosts命令,可查看/etc/hosts配置文件的内容,其文件内容为:
[root@centos7 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
配置/etc/resolv.conf文件,可以设置DNS服务器,即请求该服务器帮助解析主机名。
[root@centos7 ~]# cat /etc/resolv.conf # Generated by NetworkManager search localdomain nameserver 192.168.93.2
其中的nameserver192.168.93.2表示本机将找192.168.93.2来帮助查询主机名对应的IP地址。
一般情况下,是先查找/etc/hosts文件判断是否有对应主机名的记录,如果没有则根据/etc/resolv.conf中记录请求对方帮忙解析主机名。但是这种优先级是可以配置的,在/etc/nsswitch.conf文件中有一行hosts选项,其指定先查找谁。