本章主要介绍网络配置方法
一台主机需要配置必要的网络信息,才可以连接到互联网。需要的配置网络信息包括IP地址,子网掩码,网关和DNS。
在计算机中对IP地址标记使用的是32bit的二进制,例如:11000000 10101000 00011010 01100100
这里共有32位由1和0组成的二进制数字,这样的地址对于人类来说并不好记忆,所已用3 个点把这32位的二进制隔成4个部分,每个部分8个二进制数字。
[root@red01 ~]# ifconfig
docker0: flags=4099 mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:f5:f4:cd:5f 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
ens160: flags=4163 mtu 1500
inet 192.168.182.200 netmask 255.255.255.0 broadcast 192.168.182.255
inet6 fe80::20c:29ff:fe4f:12a8 prefixlen 64 scopeid 0x20
ether 00:0c:29:4f:12:a8 txqueuelen 1000 (Ethernet)
RX packets 2538 bytes 221720 (216.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 863 bytes 57369 (56.0 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 1000 (Local Loopback)
RX packets 6 bytes 496 (496.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6 bytes 496 (496.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099 mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:96:ef:60 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
[root@red01 ~]#
[root@red01 ~]# ifconfig ens160
ens160: flags=4163 mtu 1500
inet 192.168.182.200 netmask 255.255.255.0 broadcast 192.168.182.255
inet6 fe80::20c:29ff:fe4f:12a8 prefixlen 64 scopeid 0x20
ether 00:0c:29:4f:12:a8 txqueuelen 1000 (Ethernet)
RX packets 2609 bytes 227014 (221.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 901 bytes 62441 (60.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@red01 ~]#
[root@red01 ~]# ip address show ens160
2: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:4f:12:a8 brd ff:ff:ff:ff:ff:ff
inet 192.168.182.200/24 brd 192.168.182.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe4f:12a8/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@red01 ~]#
[root@red01 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.182.2 0.0.0.0 UG 100 0 0 ens160
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
192.168.182.0 0.0.0.0 255.255.255.0 U 100 0 0 ens160
[root@red01 ~]#
[root@red01 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 202.96.128.86
nameserver 119.29.29.29
[root@red01 ~]#
[root@red01 ~]# nmcli connection
NAME UUID TYPE DEVICE
ens160 92239b09-7a8b-4718-86d0-f2b0ee80ee57 ethernet ens160
docker0 1879b8f3-f71d-4dba-ba4d-adbaf5b399ea bridge docker0
virbr0 4b33414d-65a9-4627-ac0f-2f1c2332f2ee bridge virbr0
[root@red01 ~]#
[root@red01 ~]# nmcli connection show ens160 | grep ipv4
ipv4.method: manual
ipv4.dns: 202.96.128.86,119.29.29.29
ipv4.dns-search: --
ipv4.dns-options: --
ipv4.dns-priority: 0
ipv4.addresses: 192.168.182.200/24
ipv4.gateway: 192.168.182.2
可以看到,ens160获取IP方式是mannal.
注意:应为我们要练习创建连接,所以这里先把此连接删除。
[root@red01 ~]# nmcli connection delete ens160
成功删除连接 "ens160" (92239b09-7a8b-4718-86d0-f2b0ee80ee57)。
[root@red01 ~]#
[root@red01 ~]# ifconfig ens160
ens160: flags=4163 mtu 1500
inet 192.168.182.200 netmask 255.255.255.0 broadcast 192.168.182.255
inet6 fe80::7452:2f13:8d33:bf16 prefixlen 64 scopeid 0x20
ether 00:0c:29:4f:12:a8 txqueuelen 1000 (Ethernet)
RX packets 3409 bytes 286137 (279.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1215 bytes 96354 (94.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@red01 ~]#
也可以通过查看连接ens160的属性来查看IP信息,这里可以看到IP地址、网关、DNS等信息。因为这个连接是新创建的,所以创建好之后可以立即生效。
[root@red01 ~]# nmcli connection show ens160 | grep ipv4
ipv4.method: manual
ipv4.dns: 202.96.128.86,119.29.29.29
ipv4.dns-search: --
ipv4.dns-options: --
ipv4.dns-priority: 0
ipv4.addresses: 192.168.182.200/24
ipv4.gateway: 192.168.182.2
[root@red01 ~]# nmtui-edit
[root@red01 ~]# nmcli connection show ens160 | grep ipv4
ipv4.method: manual
ipv4.dns: 202.96.128.86,119.29.29.29
ipv4.dns-search: --
ipv4.dns-options: --
ipv4.dns-priority: 0
ipv4.addresses: 192.168.182.200/24
ipv4.gateway: 192.168.182.2
root@red01 ~]# nmcli connection delete ens160 //删除网卡
成功删除连接 "ens160" (dee33246-5a6d-4416-bff8-e42e110f3f0b)。
[root@red01 ~]#
[root@red01 ~]#
[root@red01 ~]# nmcli connection //查看网卡是否删除
NAME UUID TYPE DEVICE
docker0 1879b8f3-f71d-4dba-ba4d-adbaf5b399ea bridge docker0
virbr0 4b33414d-65a9-4627-ac0f-2f1c2332f2ee bridge virbr0
[root@red01 ~]#
[root@red01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens160
[root@red01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens160
DEVICE=ens160
NAME=ens160
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.182.200
NATMASK=255.255.255.0
GATEWAY=192.168.182.2
DNS1=202.96.128.86
DNS2=119.29.29.29
[root@red01 ~]#
这里字段的含义如下:
[root@red01 ~]# nmcli connection reload
[root@red01 network-scripts]# nmcli device reapply ens160
成功重新应用连接到设备 "ens160"。
[root@red01 ~]# ifconfig ens160
ens160: flags=4163 mtu 1500
inet 192.168.182.200 netmask 255.255.255.0 broadcast 192.168.182.255
ether 00:0c:29:4f:12:a8 txqueuelen 1000 (Ethernet)
RX packets 3921 bytes 321958 (314.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1367 bytes 115267 (112.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@red01 ~]#
[root@red01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens160
DEVICE=ens160
NAME=ens160
ONBOOT=yes
BOOTPROTO=none
IPADDR=192.168.182.201 //修改IP
NATMASK=255.255.255.0
GATEWAY=192.168.182.2
DNS1=202.96.128.86
DNS2=119.29.29.29
[root@red01 ~]#
6)修改之后执行 nmcli connect reload 命令是没用的,还要执行 nmcli device reapply ens160 命令。
[root@red01 ~]# nmcli connection reload
[root@red01 ~]# nmcli device reapply ens160
成功重新应用连接到设备 "ens160"。
总结如下:
root@red01 ~]# nmcli connection
NAME UUID TYPE DEVICE
ens160 ea74cf24-c2a2-ecee-3747-a2d76d46f93b ethernet ens160
docker0 1879b8f3-f71d-4dba-ba4d-adbaf5b399ea bridge docker0
virbr0 4b33414d-65a9-4627-ac0f-2f1c2332f2ee bridge virbr0
[root@red01 ~]#
[root@red01 ~]# nmcli connection delete ens160 //删除网卡
成功删除连接 "ens160" (ea74cf24-c2a2-ecee-3747-a2d76d46f93b)。
[root@red01 ~]# nmcli connection //查看ens160是否删除
NAME UUID TYPE DEVICE
docker0 1879b8f3-f71d-4dba-ba4d-adbaf5b399ea bridge docker0
virbr0 4b33414d-65a9-4627-ac0f-2f1c2332f2ee bridge virbr0
[root@red01 ~]#
[root@red01 ~]# nmcli connection add type ethernet con-name ens160 ifname ens160 ipv4.addresses 192.168.182.200/24 ipv4.gateway 192.168.182.2 ipv4.dns 202.96.128.86 autoconnect yes
连接 "ens160" (f75547c3-2963-40c8-a599-4ed518f2925c) 已成功添加。
[root@red01 ~]#
[root@red01 ~]# nmcli connection show ens160 | grep ipv4
ipv4.method: auto
ipv4.dns: 202.96.128.86
ipv4.dns-search: --
ipv4.dns-options: --
ipv4.dns-priority: 0
ipv4.addresses: 192.168.182.200/24
ipv4.gateway: 192.168.182.2
[root@red01 ~]# nmcli connection modify ens160 ipv4.addresses 192.168.182.201/24
[root@red01 ~]#
[root@red01 ~]# nmcli device reapply ens160
成功重新应用连接到设备 "ens160"。
[root@red01 ~]#
[root@red01 ~]# nmcli connection modify ens160 ifname ens160 ipv4.method manual +ipv4.addresses 192.168.182.200/24
[root@red01 ~]# nmcli device reapply ens160
成功重新应用连接到设备 "ens160"。
[root@red01 ~]#
[root@red01 ~]# ip addr show ens160
2: ens160: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:4f:12:a8 brd ff:ff:ff:ff:ff:ff
inet 192.168.182.201/24 brd 192.168.182.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.182.200/24 brd 192.168.182.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::ae53:b446:9d9a:e8fb/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@red01 ~]#
[root@red01 ~]# nmcli connection modify ens160 ifname ens160 ipv4.method manual -ipv4.addresses 192.168.182.201/24
[root@red01 ~]# nmcli device reapply ens160
成功重新应用连接到设备 "ens160"。
[root@red01 ~]#