网卡配置文件的基本属性:将Linux主机接入到网络,需要配置网络相关设置。一般包括如下内容:IP地址、NETMASK、Gateway、Route、Dns及主机名
IP/NETMASK主要用于确定自身所处的网络,以及对方主机是否与本主机在同一网络中
Gateway,网关,主要用于本主机与其他网络主机通讯使用。
Route,路由,建立路径条目,分三种类型
(1)网络路由:目标是一个网络
(2)主机路由:目标是一个主机
(3)默认路由:目标是所有的非本地网络,通常默认路由的下一跳为主机上某块网卡的网关地址
Dns,用于对域名进行解析。其中包括
(1)主DNS服务器
(2)备用DNS服务器
(3)第三备用DNS服务器
查看网卡信息:
dmesg | grep -i eth
[root@dashui ~]# dmesg | grep -i eth [ 4.694979] e1000 0000:02:01.0 eth0: (PCI:66MHz:32-bit) 00:0c:29:9a:bf:3a [ 4.695013] e1000 0000:02:01.0 eth0: Intel(R) PRO/1000 Network Connection [ 5.498186] e1000 0000:02:05.0 eth1: (PCI:66MHz:32-bit) 00:0c:29:9a:bf:44 [ 5.498196] e1000 0000:02:05.0 eth1: Intel(R) PRO/1000 Network Connection [ 45.921206] IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready [ 45.969677] IPv6: ADDRCONF(NETDEV_UP): eth0: link is not ready [ 45.972994] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None [ 45.982936] IPv6: ADDRCONF(NETDEV_UP): eth1: link is not ready [ 45.998182] e1000: eth1 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None [ 46.042210] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready [ 2937.512292] e1000: eth0 NIC Link is Down [ 2955.567387] e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None [ 2955.569181] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
配置IP地址:
◆ifconfig
◆ip命令
◆GUI工具
◆TUI工具
◆编辑配置文件
■ifconfig命令:
命令行上直接输入ifconfig显示当前工作网卡的信息
[root@dashui ~]# ifconfig eth0: flags=4163mtu 1500 inet 10.1.0.26 netmask 255.255.0.0 broadcast 10.1.255.255 inet6 fe80::20c:29ff:fe9a:bf3a prefixlen 64 scopeid 0x20 ether 00:0c:29:9a:bf:3a txqueuelen 1000 (Ethernet) RX packets 30878 bytes 2775118 (2.6 MiB) RX errors 0 dropped 25 overruns 0 frame 0 TX packets 2427 bytes 585920 (572.1 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth1: flags=4163 mtu 1500 inet 172.18.19.105 netmask 255.255.255.0 broadcast 172.18.19.255 inet6 fe80::20c:29ff:fe9a:bf44 prefixlen 64 scopeid 0x20 ether 00:0c:29:9a:bf:44 txqueuelen 1000 (Ethernet) RX packets 30001 bytes 2693956 (2.5 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 11 bytes 1650 (1.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 0 (Local Loopback) RX packets 452 bytes 45456 (44.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 452 bytes 45456 (44.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
禁用或启用:ifconfig 网卡名 up|down
[root@dashui ~]# ifconfig eth1 down [root@dashui ~]# ifconfig eth0: flags=4163mtu 1500 inet 10.1.0.26 netmask 255.255.0.0 broadcast 10.1.255.255 inet6 fe80::20c:29ff:fe9a:bf3a prefixlen 64 scopeid 0x20 ether 00:0c:29:9a:bf:3a txqueuelen 1000 (Ethernet) RX packets 31608 bytes 2844259 (2.7 MiB) RX errors 0 dropped 25 overruns 0 frame 0 TX packets 2482 bytes 593828 (579.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 0 (Local Loopback) RX packets 452 bytes 45456 (44.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 452 bytes 45456 (44.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
设定IP地址: ifconfig 网卡名 IP/NETMASK 或者 ifconfig 网卡名 IP netmask NETMASK
[root@dashui ~]# ifconfig eth1 10.0.0.100/16 [root@dashui ~]# ifconfig eth1 eth1: flags=4163mtu 1500 inet 10.0.0.100 netmask 255.255.0.0 broadcast 10.0.255.255 inet6 fe80::20c:29ff:fe9a:bf44 prefixlen 64 scopeid 0x20 ether 00:0c:29:9a:bf:44 txqueuelen 1000 (Ethernet) RX packets 30662 bytes 2756949 (2.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 19 bytes 2278 (2.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
■ip命令
ip [OPTIONS] OPJECT {COMMAND}
OBJECT={link|addr|route}
link:链接接口,比如eth0
addr:管理接口上的地址
route:管理路由
▲Link:
set:设定接口属性
ip link set DEVICE up|down 禁用或启用接口
[root@dashui ~]# ip link set eth1 up [root@dashui ~]# ifconfig eth0: flags=4163mtu 1500 inet 10.1.0.26 netmask 255.255.0.0 broadcast 10.1.255.255 inet6 fe80::20c:29ff:fe9a:bf3a prefixlen 64 scopeid 0x20 ether 00:0c:29:9a:bf:3a txqueuelen 1000 (Ethernet) RX packets 34941 bytes 3137065 (2.9 MiB) RX errors 0 dropped 25 overruns 0 frame 0 TX packets 2639 bytes 608570 (594.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth1: flags=4163 mtu 1500 inet 10.0.0.100 netmask 255.255.0.0 broadcast 10.0.255.255 inet6 fe80::20c:29ff:fe9a:bf44 prefixlen 64 scopeid 0x20 ether 00:0c:29:9a:bf:44 txqueuelen 1000 (Ethernet) RX packets 33412 bytes 2997333 (2.8 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 21 bytes 2418 (2.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
promisc {on|off}:是否支持混杂模式
name NEWNAME:设定接口的新名字
show:显示接口状态
[root@dashui ~]# ip link show eth1 3: eth1:mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:9a:bf:44 brd ff:ff:ff:ff:ff:ff
▲addr:管理地址
add:添加地址
del:删除地址
[root@dashui ~]# ip addr add 192.168.0.2/24 dev eth1 [root@dashui ~]# ip addr show eth1 3: eth1:mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:9a:bf:44 brd ff:ff:ff:ff:ff:ff inet 10.0.0.100/16 brd 10.0.255.255 scope global eth1 valid_lft forever preferred_lft forever inet 192.168.0.2/24 scope global eth1 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe9a:bf44/64 scope link valid_lft forever preferred_lft forever
show:显示该网卡的所有地址信息
flush:清除协议地址
[root@dashui ~]# ip addr flush dev eth1 [root@dashui ~]# ip addr show eth1 3: eth1:mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:9a:bf:44 brd ff:ff:ff:ff:ff:ff
▲管理路由
list|show:显示路由
[root@dashui ~]# ip route list default via 10.1.0.1 dev eth0 proto static metric 100 10.1.0.0/16 dev eth0 proto kernel scope link src 10.1.0.26 metric 100
add:添加路由
[root@localhost ~]# ip route add 10.1.0.17 via 10.1.0.1 [root@localhost ~]# ip route show 10.1.0.17 via 10.1.0.1 dev eth0 10.1.0.0/16 dev eth0 proto kernel scope link src 10.1.252.122 169.254.0.0/16 dev eth0 scope link metric 1002 default via 10.1.0.1 dev eth0
del:删除路由
[root@localhost ~]# ip route del 10.1.0.17 [root@localhost ~]# ip route show 10.1.0.0/16 dev eth0 proto kernel scope link src 10.1.252.122 169.254.0.0/16 dev eth0 scope link metric 1002 default via 10.1.0.1 dev eth0
■GUI方式:图形用户界面,简称GUI,指采用图形方式显示计算机操作系统用户界面
■TUI:
CentOS 6:命令行输入setup,选择Network Configurations
CentOS 7:命令行输入nmtui命令
■通过修改对应网卡的配置文件
每个网卡都有一个配置文件,/etc/sysconfig/network-scripts/ifcfg-NAME,其常用参数如下
DEVICE:网卡名,通常与配置文件名称相同
BOOTPROTO:静态启动还是dhcp获取地址static和none为手工配置IP,dhcp为动态分配IP
HWADDR:网卡的mac地址
NM_CONTROLLED:即NetworkManager的简写,建议设为no
ONBOOT:是否随系统启动激活
TYPE:网卡类型,通常有Ethernet,Bridge
UUID:网卡设备的唯一识别码
IPADDR:此接口的IP地址
NETMSK:子网掩码
GATEWAY:网关
DNS1:主DNS服务器
DNS2:备用DNS服务器
DNS3:第三备用DNS服务器
USERCTL:是否允许普通用户管理此接口
注意:重启网络服务后方能生效
路由管理:路由管理主要通过route命令来管理。
(1)查看本机内核路由表:route-n
[root@dashui ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.1.0.1 0.0.0.0 UG 100 0 0 eth0 10.1.0.0 0.0.0.0 255.255.0.0 U 100 0 0 eth0
(2)添加路由条目
route add [net|host] TARGET gw GATEWAY [dev NAME]
-net:后面跟一个网络地址,表示网络路由
-host:后面跟一个主机地址,表示主机路由
[root@localhost ~]# route add -host 10.1.0.17 gw 10.1.0.1 [root@localhost ~]# route add -net 10.1.0.0/16 gw 10.1.0.1 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.1.0.17 10.1.0.1 255.255.255.255 UGH 0 0 0 eth0 10.1.0.0 10.1.0.1 255.255.0.0 UG 0 0 0 eth0 10.1.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 10.1.0.1 0.0.0.0 UG 0 0 0 eth0
(3)删除路由
route del [-net|-host] TARGET gw GATEWAY [dev NAME]
[root@localhost ~]# route del -net 10.1.0.0/16 gw 10.1.0.1 [root@localhost ~]# route del -host 10.1.0.17 gw 10.1.0.1 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.1.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 10.1.0.1 0.0.0.0 UG 0 0 0 eth0 [root@localhost ~]#
注意:所有配置配置立即发往内核去运行,所以会立即生效,但只是临时生效,如果想要永久生效,需修改配置文件。配置文件路径为/etc/sysconfig/network-scripts/route-NAME
两种风格:
(1) TARGET via GW
10.1.0.0/16 via 10.1.0.1 10.1.0.17 via 10.1.0.1
(2) 每三行定义一条路由
ADDRESS#=TARGET
NETMASK#=mask
GATEWAY#=GW
nmcli命令:NetworkManager这是一个动态的网络控制和配置守护进程,nmcli就是控制NetworkManager的命令行工具。
语法:
SYNOPSIS nmcli [ OPTIONS ] OBJECT { COMMAND | help } OBJECT := { general | networking | radio | connection | device | agent } OPTIONS := { -t[erse] -p[retty] -m[mode] tabular | multiline -f[ields]| all | common -e[scape] yes | no -n[ocheck] -a[sk] -w[ait] -v[ersion] -h[elp] }
◆显示所有连接
[root@dashui ~]# nmcli connection show NAME UUID TYPE DEVICE myen a9471bc1-6e31-48c9-ba6c-8dc1540be943 802-3-ethernet -- Wired connection 1 d5e59663-432e-4441-b634-ac084c568831 802-3-ethernet -- System eth0 3f906d5a-de3b-4f40-b64f-5dd2c026e0d4 802-3-ethernet eth0 eth1 7a38daa3-4e50-4513-af57-be6afd7b9ee8 802-3-ethernet eth1 [root@dashui ~]#
◆显示当前所有活动的连接
[root@dashui ~]# nmcli connection show --active NAME UUID TYPE DEVICE System eth0 3f906d5a-de3b-4f40-b64f-5dd2c026e0d4 802-3-ethernet eth0 eth1 7a38daa3-4e50-4513-af57-be6afd7b9ee8 802-3-ethernet eth1
◆显示由 NetworkManager 识别到设备及其状态
[root@dashui ~]# nmcli device status DEVICE TYPE STATE CONNECTION eth0 ethernet connected System eth0 eth1 ethernet connected eth1 lo loopback unmanaged --
◆创建新连接a1,IP 自动通过dhcp
[root@dashui ~]# nmcli connection add con-name a1 type ethernet ifname eth1 Connection 'a1' (16154e90-86cc-4a21-97f6-eedb304de2c0) successfully added.
◆指定静态IP
[root@dashui ~]# nmcli connection add con-name b1 autoconnect no ifname eth1 type ethernet ip4 172.10.0.22 gw4 171.16.0.1 Connection 'b1' (baadd5bf-f85d-4fb2-9d69-8d89f82ed7a3) successfully added.
◆添加DNS
[root@dashui ~]# nmcli connection modify b1 ipv4.dns 8.8.8.8 [root@dashui ~]# nmcli connection show b1 ..........省略............. 802-3-ethernet.wake-on-lan-password: -- ipv4.method: manual ipv4.dns: 8.8.8.8 ipv4.dns-search: ipv4.addresses: 172.10.0.22/32 ipv4.gateway: 171.16.0.1 ipv4.routes: ..........省略.............
注意:如果想使上述操作生效,则需执行systemctl restart network 或者nmcli connection reload
谢谢浏览,因为nmcli命令是在太多,这里不做过多的例子,nmcli支持命令不全,或者提示你下步的输入,如果哪点忘了直接tab补全,以后会不定时更新。