4案例4:配置静态网络地址
4.1问题
本例要求为虚拟机server配置以下静态地址参数:
- 主机名:server0.example.com
- IP地址:172.25.0.11
- 子网掩码:255.255.255.0
- 默认网关:172.25.0.254
- DNS服务器:172.25.254.254
4.2方案
使用nmcli配置网络连接时的基本操作
查看网络连接、连接详情:
- nmcli con show
- nmcli con show "连接名"
修改网络连接参数:
- nmcli con modify "连接名" ipv4.method auto|manual
- nmcli con modify "连接名" ipv4.address "IP地址/掩码长度 [默认网关]" ipv4.dns DNS服务器地址
- nmcli con modify "连接名" connection.autoconnect yes|no
4.3步骤
实现此案例需要按照如下步骤进行。
步骤一:配置固定主机名
1)配置前,检查是否设置静态主机名
[root@server0 ~]# hostnamectl
Static hostname: n/a //未设置静态主机名
Transient hostname: server0.example.com
2)设置为指定的主机名
[root@server0 ~]# vim /etc/hostname //建立主机名配置文件
server0.example.com
3)配置后,检查结果
[root@server0 ~]# hostnamectl
Static hostname: server0.example.com //已设置静态主机名
Icon name: computer
.. ..
步骤二:配置静态IP地址参数
1)查看当前主机的网卡设备、网络连接
[root@server0 ~]# nmcli connection show
名称 UUID 类型 设备
System eth0 5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03 802-3-ethernet eth0
2)修改连接"system eth0"的配置
将配置方式指定为manual,指定IP地址、默认网关、DNS地址、并配置自动连接:
[root@server0 ~]# nmcli connection modify "System eth0" ipv4.method manual ipv4.addresses "172.25.0.11/24 172.25.0.254" ipv4.dns 172.25.254.254 connection.autoconnect yes
3)重新激活连接“system eth0”
通过up指令激活连接配置,必要时也可以先用down再up:
[root@server0 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.25.0.254 0.0.0.0 UG 1024 0 0 eth0
172.25.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
[root@server0 ~]# nmcli connection up "System eth0" //激活连接
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1)
确保系统服务NetworkManager开机自启:
[root@server0 ~]# systemctl restart NetworkManager
[root@server0 ~]# systemctl enable NetworkManager
4)检查修改结果,确认无误
检查IP地址:
[root@server0 ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.0.11 netmask 255.255.255.0 broadcast 172.25.0.255
inet6 fe80::5054:ff:fe00:b prefixlen 64 scopeid 0x20<link>
ether 52:54:00:00:00:0b txqueuelen 1000 (Ethernet)
RX packets 1394 bytes 138855 (135.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 944 bytes 98495 (96.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
检查默认网关地址:
[root@server0 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.25.0.254 0.0.0.0 UG 1024 0 0 eth0
172.25.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
检查DNS服务器地址:
[root@server0 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search example.com
nameserver 172.25.254.254
如果使用nmcli修改网络连接时并未指定ipv4.dns,也可以直接修改DNS客户端配置文件/etc/resolv.conf,确保添加有上述记录即可。
步骤三:验证网络配置结果
通过ssh远程访问server0:
[root@room9pc13 ~]# ssh -X root@server0.example.com
Warning: Permanently added 'server0.example.com' (ECDSA) to the list of known hosts.
Last login: Fri Dec 23 19:00:12 2016 from 172.25.0.250
[root@server0 ~]# hostname //确认自己的主机名
server0.example.com
在虚拟机server0上,可以查询server0、desktop0、content等站点:
[root@server0 ~]# host server0.example.com
server0.example.com has address 172.25.0.11
[root@server0 ~]# host desktop0.example.com
desktop0.example.com has address 172.25.0.10
desktop0.example.com mail is handled by 10 smtp0.example.com.
[root@server0 ~]# host content.example.com
content.example.com has address 172.25.254.254