Linux网络属性配置(三)修改配置文件&& CentOS 7 网络配置
Linux网络属性配置(三)修改配置文件
IP、MASK、GW、DNS相关配置文件:
/etc/sysconfig/network-scripts/ifcfg-IFACE
路由相关配置文件:
/etc/sysconfig/network-scripts/route-IFACE
/etc/sysconfig/network-scripts/ifcfg-IFACE
DEVICE:此配置文件应用到的设备 HWADDR:对应的设备的MAC地址 BOOTPROTO:激活此设备时用到的地址配置协议,常用的dhcp,static,none,bootp MM_CONTROLLED:MM是NetworkManager的简写,此网卡是否接受MM控制;CentOS 6建议为“no” ONBOOT:在系统因到时候是否激活此设备 TYPE:接口类型;常见的有Ethernet,Bridge UUID:设备的唯一标识 IPADDR:指明IP地址 NETMASK:子网掩码 GATEWAY:默认网关 DSN1:第一个DNS服务器指向 DNS2:第二个DNS服务器指向 USERCTL:普通用户是否可控制此设备 PEERDNS:如果BOOTPROTO的值为"dhsp",是否允许dhcp server分配的dns服务器指向信息直接覆盖至/etc/resolv.conf文件中 [root@Daniel ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=none IPADDR=192.168.98.140 PREFIX=24 GATEWAY=192.168.98.2 #DNS1=8.8.8.8 #DOMAIN=8.8.4.4 DEFROUTE=yes IPV4_FAILURE_FATAL=yes IPV6INIT=no NAME="System eth0"
/etc/sysconfig/network-scripts/route-IFACE
两种风格
TARGET via GW
每三行定义一条路由
ADDRESS#=TARGET NETMASK#=mask GATEWAY#=GW
给网卡配置多个地址
ifconfig:
ifcofnig IFACE_ALIAS
ip:
ip addr add
配置文件:
ifcfg-IFACE_ALIAS DEVICE=IFACE_ALIAS
注意:网关别名不能使用dhcp协议引导。
Linux网络属性配置的tui(text user interface)
-
system-config-network-tui
-
也可以使用setup找到
[root@Daniel ~]# setup
注意:修改之后重启网络服务才能生效
网络接口识别并命令相关的配置文件:
/etc/udev/rules.c/70-persistent-net.rules
[root@Daniel ~]# cat /etc/udev/rules.d/70-persistent-net.rules # This file was automatically generated by the /lib/udev/write_net_rules # program, run by the persistent-net-generator.rules rules file. # # You can modify it, as long as you keep each rule on a single # line, and change only the value of the NAME= key. # PCI device 0x8086:0x100f (e1000) SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:2c:81:4d", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0" [root@Daniel ~]#
卸载网卡驱动
modeprobe -r e1000
装载网卡驱动
modeprobe e1000
CentOS 7网络属性配置
CentOS 7 的网卡命名机制较之CentOS 6来说有较大不同,接下来我们来详细说说CentOS 7网卡的命名规则
-
网卡命名机制
systemd对网络设备的命名方式:
(a) 如果Firmware或BIOS为主板上集成的设备提供的索引信息可用,且可预测则根据此索引进行命名,例如eno1; (b) 如果Firmware或BIOS为PCI-E扩展槽所提供的索引信息可用,且可预测,则根据此索引进行命名,例如ens1; (c) 如果硬件接口的物理位置信息可用,则根据此信息进行命名,例如enp2s0; (d) 如果用户显式启动,也可根据MAC地址进行命名,enx2387a1dc56; (e) 上述均不可用时,则使用传统命名机制;
上述命名机制中,有的需要biosdevname程序的参与。
-
名称组成格式
en: ethernet wl: wlan ww: wwan 名称类型: o<index>: 集成设备的设备索引号; s<slot>: 扩展槽的索引号; x<MAC>: 基于MAC地址的命名; p<bus>s<slot>: enp2s1
网卡设备的命名过程:
-
第一步:
udev, 辅助工具程序/lib/udev/rename_device, /usr/lib/udev/rules.d/60-net.rules -
第二步:
biosdevname 会根据/usr/lib/udev/rules.d/71-biosdevname.rules -
第三步:
通过检测网络接口设备,根据/usr/lib/udev/rules.d/75-net-description
IDNETNAMEONBOARD, IDNETNAMESLOT, IDNETNAME_PATH
回归传统的命名方式:
[root@Daniel ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.98.141 netmask 255.255.255.0 broadcast 192.168.98.255 inet6 fe80::20c:29ff:fe82:7610 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:82:76:10 txqueuelen 1000 (Ethernet) RX packets 8936 bytes 786741 (768.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7550 bytes 515886 (503.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 0 (Local Loopback) 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 [root@Daniel ~]#
-
(1) 编辑/etc/default/grub配置文件
GRUB_CMDLINE_LINUX="net.ifnames=0 rhgb quiet" [root@Daniel ~]# cat /etc/default/grub GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet net.ifnames=0" GRUB_DISABLE_RECOVERY="true" [root@Daniel ~]#
-
(2) 为grub2生成其配置文件
grub2-mkconfig -o /etc/grub2.cfg [root@Daniel ~]# grub2-mkconfig -o /etc/grub2.cfg Generating grub configuration file ... Found linux image: /boot/vmlinuz-3.10.0-327.el7.x86_64 Found initrd image: /boot/initramfs-3.10.0-327.el7.x86_64.img Found linux image: /boot/vmlinuz-0-rescue-1935525f9d944f499859e4e93218a41e Found initrd image: /boot/initramfs-0-rescue-1935525f9d944f499859e4e93218a41e.img done [root@Daniel ~]#
-
(3) 重启系统
[root@Daniel ~]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.98.141 netmask 255.255.255.0 broadcast 192.168.98.255 inet6 fe80::20c:29ff:fe82:7610 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:82:76:10 txqueuelen 1000 (Ethernet) RX packets 38 bytes 5214 (5.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 92 bytes 15232 (14.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 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 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:e0:eb:12 txqueuelen 0 (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 [root@Daniel ~]# cat /etc/centos-release CentOS Linux release 7.2.1511 (Core) [root@Daniel ~]#
可以看到这个时候CentOS 7的网卡名字已经改回eth0.
地址配置工具:nmcli
nmcli [ OPTIONS ] OBJECT { COMMAND | help }
device - show and manage network interfaces [root@Daniel ~]# nmcli device 设备 类型 状态 CONNECTION virbr0 bridge 连接的 virbr0 eth0 ethernet 连接的 系统 eth0 virbr0-nic tap 连接的 virbr0-nic lo loopback 未管理 -- [root@Daniel ~]# connection - start, stop, and manage network connections [root@Daniel ~]# nmcli connection 名称 UUID 类型 设备 virbr0-nic 0f7f1ec2-683b-4e4a-89a0-67c3c7096a68 generic virbr0-nic virbr0 497ce206-0f80-4833-9d9f-1aa4078aae04 bridge virbr0 系统 eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 802-3-ethernet eth0 [root@Daniel ~]# 如何修改IP地址等属性: #nmcli connection modify IFACE [+|-]setting.property value setting.property: ipv4.addresses ipv4.gateway ipv4.dns1 ipv4.method manual
网络接口配置tui工具:nmtui
[root@Daniel ~]# nmtui
主机名称配置工具:hostnamectl
status [root@Daniel ~]# hostnamectl status Static hostname: Daniel Icon name: computer-vm Chassis: vm Machine ID: 1935525f9d944f499859e4e93218a41e Boot ID: 3655c6af122747d287c98daa9a3be63a 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 set-hosrname [root@Daniel ~]# hostnamectl set-hostname DanielW [root@Daniel ~]# hostname danielw [root@Daniel ~]#
原创文章,作者:Daniel-W,如若转载,请注明出处:http://www.178linux.com/23017