一、时间设置
1.常见管理命令
1.1 date 命令
bash
[root@server1 ~ 10:19:55]# date
Tue Apr 7 10:37:11 CST 2026
[root@server1 ~ 10:37:11]# echo $LANG
en_US.UTF-8
# 设置时间显示格式
[root@server1 ~ 10:37:11]# LANG=zh_ZN.UTF-8
[root@server1 ~ 10:37:38]# date
Tue Apr 7 10:37:41 CST 2026
# 设置时间
[root@server1 ~ 10:37:41]# date -s 'Tue Apr 7 10:37:48 CST 2026'
Tue Apr 7 10:37:48 CST 2026
1.2 tzselect 命令
bash
# 查看时区对应代码。
[root@server1 ~ 10:38:11]# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
1) Africa
2) Americas
3) Antarctica
4) Arctic Ocean
5) Asia
6) Atlantic Ocean
7) Australia
8) Europe
9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5
Please select a country.
1) Afghanistan 18) Israel 35) Palestine
2) Armenia 19) Japan 36) Philippines
3) Azerbaijan 20) Jordan 37) Qatar
4) Bahrain 21) Kazakhstan 38) Russia
5) Bangladesh 22) Korea (North) 39) Saudi Arabia
6) Bhutan 23) Korea (South) 40) Singapore
7) Brunei 24) Kuwait 41) Sri Lanka
8) Cambodia 25) Kyrgyzstan 42) Syria
9) China 26) Laos 43) Taiwan
10) Cyprus 27) Lebanon 44) Tajikistan
11) East Timor 28) Macau 45) Thailand
12) Georgia 29) Malaysia 46) Turkmenistan
13) Hong Kong 30) Mongolia 47) United Arab Emirates
14) India 31) Myanmar (Burma) 48) Uzbekistan
15) Indonesia 32) Nepal 49) Vietnam
16) Iran 33) Oman 50) Yemen
17) Iraq 34) Pakistan
#? 9
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 2
The following information has been given:
China
Xinjiang Time
Therefore TZ='Asia/Urumqi' will be used.
Local time is now: Tue Apr 7 08:38:34 +06 2026.
Universal Time is now: Tue Apr 7 02:38:34 UTC 2026.
Is the above information OK?
1) Yes
2) No
#? 1
You can make this change permanent for yourself by appending the line
TZ='Asia/Urumqi'; export TZ
to the file '.profile' in your home directory; then log out and log in again.
Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Urumqi
bash
# 通过变量设置时区
[root@server1 ~ 10:38:39]# TZ='Asia/Urumqi'; export TZ
[root@server1 ~ 08:38:54]# date
Tue Apr 7 08:38:57 +06 2026
[root@server1 ~ 08:38:57]# TZ='Asia/Shanghai'; export TZ
[root@server1 ~ 10:39:11]# date
Tue Apr 7 10:39:13 CST 2026
[root@server1 ~ 10:39:13]#
[root@server1 ~ 10:39:16]#
[root@server1 ~ 10:39:22]# date
Tue Apr 7 10:39:24 CST 2026
[root@server1 ~ 10:39:24]#
二、自动对时
chrony 软件
2.1安装软件
bash
[root@server1 ~ 10:39:24]# yum install -y chrony
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00
epel | 4.3 kB 00:00
extras | 2.9 kB 00:00
updates | 2.9 kB 00:00
2.2设置与哪个服务器对时
bash
[root@server1 ~ 10:45:25]# vim /etc/chrony.conf
# 将原先server开头的行注释掉,并新增一行
# 设置与aliyun对时
server ntp.aliyun.com iburst
2.3启动服务
bash
[root@server1 ~ 10:47:50]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:chronyd(8)
man:chrony.conf(5)
[root@server1 ~ 10:48:05]# systemctl start chronyd
[root@server1 ~ 10:48:17]# date
Tue Apr 7 10:48:20 CST 2026
2.4查看与哪个服务器对时
bash
[root@server1 ~ 11:06:48]# chronyc sources -v
210 Number of sources = 1
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88 2 6 17 18 -2465us[ -232us] +/- 29ms
三、网络管理
3.1网络查看
bash
# 查看IP地址
[root@server1 ~ 11:11:36]# ip -br addr
lo UNKNOWN 127.0.0.1/8 ::1/128
ens33 UP 10.1.8.11/24 fe80::20c:29ff:fee1:f033/64
bash
# 查看特定网卡IP地址
[root@server1 ~ 11:20:31]# ip -br addr show ens33
ens33 UP 10.1.8.11/24 fe80::20c:29ff:fee1:f033/64
bash
# 查看MAC地址
[root@server1 ~ 11:20:47]# ip -br link
lo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
ens33 UP 00:0c:29:e1:f0:33 <BROADCAST,MULTICAST,UP,LOWER_UP>
[root@server1 ~ 11:20:53]# ip -br link show ens33
ens33 UP 00:0c:29:e1:f0:33 <BROADCAST,MULTICAST,UP,LOWER_UP>
bash
# 查看网关
[root@server1 ~ 11:21:03]# ip route
default via 10.1.8.2 dev ens33 proto static metric 100
10.1.8.0/24 dev ens33 proto kernel scope link src 10.1.8.11 metric 100
# default 开头是条目网关
bash
# 查看DNS
[root@server1 ~ 11:21:08]# cat /etc/resolv.conf
# Generated by NetworkManager
search jiang.cloud
nameserver 223.5.5.5
nameserver 223.6.6.6
3.2网络配置
bash
# 捅过配置文件修改
[root@server1 ~ 11:21:20]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@server1 ~ 11:25:15]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=58de2941-93aa-4a12-9739-8aaf666d9abb
DEVICE=ens33
ONBOOT=yes
IPADDR=10.1.8.11
PREFIX=24
GATEWAY=10.1.8.2
DNS1=223.5.5.5
DNS2=223.6.6.6
IPV6_PRIVACY=no
bash
#修改完文件后需要重新加载和激活配置文件
[root@server1 ~ 11:25:02]# nmcli connection reload
[root@server1 ~ 11:25:08]# nmcli connection up ens33
3.3通过命令行修改
明白两个概念:
- **device,设备,网卡。
- **connection,连接,网卡的配置。
- device和connection之间关系是,网络需要一个配置,同一时刻只能激活一个配置。
例如我在2401教室,激活2401配置,使用的是同一个网卡 以太网2。
3.4管理设备
bash
[root@server1 ~ 11:27:10]# nmcli device
DEVICE TYPE STATE CONNECTION
ens33 ethernet connected ens33
lo loopback unmanaged --
# 断开连接,相当于拔掉网线
[root@server1 ~ 11:27:13]# nmcli device disconnect ens33
# 接上连接,相当于接上网线
[root@centos7 ~ 11:28:53]# nmcli device connect ens33
# 查看网卡详细配置
[root@server1 ~ 11:28:32]# nmcli device show ens33
GENERAL.DEVICE: ens33
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:0C:29:E1:F0:33
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: ens33
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveC
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 10.1.8.11/24
IP4.GATEWAY: 10.1.8.2
IP4.ROUTE[1]: dst = 10.1.8.0/24, nh = 0.0.0.0, mt = 1
IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 10.1.8.2, mt = 10
IP4.DNS[1]: 223.5.5.5
IP4.DNS[2]: 223.6.6.6
IP6.ADDRESS[1]: fe80::20c:29ff:fee1:f033/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = ff00::/8, nh = ::, mt = 256, tabl
IP6.ROUTE[2]: dst = fe80::/64, nh = ::, mt = 256
四、管理链接
4.1实践:
-
静态配置IP
-
动态配置IP
-
给CentOS 系统添加一块网卡
bash
[root@centos7 ~ 13:44:49]# nmcli device
DEVICE TYPE STATE CONNECTION
ens33 ethernet 已连接 ens33
ens36 ethernet 已连接 Wired connection 1
lo loopback 未托管 --
# 多了一个 ens36 网卡
1.添加一个动态获取的配置
bash
[root@centos7 ~ 13:44:51]# nmcli connection up dongtai
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
2.验证网卡配置信息:自动获取了IP、网关和DNS
bash
[root@centos7 ~ 13:45:03]# nmcli device show ens36
GENERAL.DEVICE: ens36
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:0C:29:3D:87:65
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: dongtai
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/3
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 10.1.1.128/24
IP4.GATEWAY: --
IP4.ROUTE[1]: dst = 10.1.1.0/24, nh = 0.0.0.0, mt = 101
IP4.DNS[1]: 10.1.1.1
IP4.DOMAIN[1]: localdomain
IP6.ADDRESS[1]: fe80::cd4f:4480:ebb3:a66a/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 101
IP6.ROUTE[2]: dst = ff00::/8, nh = ::, mt = 256, table=255
3.查看连接清单:多了一个dongtai,而且是激活状态(关联了网卡ens36)
bash
[root@centos7 ~ 13:45:15]# nmcli connection
NAME UUID TYPE DEVICE
ens33 58de2941-93aa-4a12-9739-8aaf666d9abb ethernet ens33
dongtai 315727ac-439e-46ff-aec4-93f47dda3fb1 ethernet ens36
dongtai 562ad2c3-b8ca-4e89-9d33-c84741f318be ethernet --
Wired connection 1 97433838-a639-30be-ad1f-a7a0b7f00bf7 ethernet --
4.2添加一个静态配置
bash
[root@centos7 ~ 13:50:35]# nmcli connection add type ethernet ifname ens36 con-name jingtai ipv4.method manual ipv4.addresses 10.1.1.10/24
Connection 'jingtai' (041b237c-e226-4e28-822a-055e24081b45) successfully added.
bash
[root@centos7 ~ 13:57:54]# nmcli connection up jingtai
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
1. 网卡配置中只有IP地址,没有网关和DNS,因为没有配置。
bash
[root@centos7 ~ 13:59:21]# nmcli device show ens33
GENERAL.DEVICE: ens33
GENERAL.TYPE: ethernet
GENERAL.HWADDR: 00:0C:29:3D:87:5B
GENERAL.MTU: 1500
GENERAL.STATE: 100 (connected)
GENERAL.CONNECTION: ens33
GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER: on
IP4.ADDRESS[1]: 10.1.8.10/24
IP4.GATEWAY: 10.1.8.2
IP4.ROUTE[1]: dst = 10.1.8.0/24, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 10.1.8.2, mt = 100
IP4.DNS[1]: 223.5.5.5
IP4.DNS[2]: 223.6.6.6
IP6.ADDRESS[1]: fe80::20c:29ff:fe3d:875b/64
IP6.GATEWAY: --
IP6.ROUTE[1]: dst = fe80::/64, nh = ::, mt = 256
IP6.ROUTE[2]: dst = ff00::/8, nh = ::, mt = 256, table=255
bash
[root@centos7 ~ 13:59:31]# nmcli connection
NAME UUID TYPE DEVICE
ens33 58de2941-93aa-4a12-9739-8aaf666d9abb ethernet ens33
jingtai 041b237c-e226-4e28-822a-055e24081b45 ethernet ens36
dongtai 315727ac-439e-46ff-aec4-93f47dda3fb1 ethernet --
dongtai 562ad2c3-b8ca-4e89-9d33-c84741f318be ethernet --
Wired connection 1 97433838-a639-30be-ad1f-a7a0b7f00bf7 ethernet --
2. 修改连接配置
bash
[root@centos7 ~ 14:05:39]# nmcli connection modify jingtai ipv4.addresses 10.1.1.100/24
[root@centos7 ~ 14:06:08]# nmcli connection up jingtai
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/10)
[root@centos7 ~ 14:06:13]# ip -br a show ens36
ens36 UP 10.1.1.100/24 fe80::23ef:cdb3:9c88:2362/64
3. 删除连接
bash
[root@centos7 ~ 14:01:22]# nmcli connection
NAME UUID TYPE DEVICE
ens33 58de2941-93aa-4a12-9739-8aaf666d9abb ethernet ens33
jingtai 041b237c-e226-4e28-822a-055e24081b45 ethernet ens36
dongtai 315727ac-439e-46ff-aec4-93f47dda3fb1 ethernet --
dongtai 562ad2c3-b8ca-4e89-9d33-c84741f318be ethernet --
Wired connection 1 97433838-a639-30be-ad1f-a7a0b7f00bf7 ethernet --
# 删除dongtai
[root@centos7 ~ 14:03:56]# nmcli connection delete dongtai
Connection 'dongtai' (315727ac-439e-46ff-aec4-93f47dda3fb1) successfully deleted.
Connection 'dongtai' (562ad2c3-b8ca-4e89-9d33-c84741f318be) successfully deleted.
[root@centos7 ~ 14:08:04]# nmcli connection
NAME UUID TYPE DEVICE
ens33 58de2941-93aa-4a12-9739-8aaf666d9abb ethernet ens33
jingtai 041b237c-e226-4e28-822a-055e24081b45 ethernet ens36
Wired connection 1 97433838-a639-30be-ad1f-a7a0b7f00bf7 ethernet --
# 删除Wired\ connection\ 1
[root@centos7 ~ 14:08:17]# nmcli connection delete Wired\ connection\ 1
Connection 'Wired connection 1' (97433838-a639-30be-ad1f-a7a0b7f00bf7) successfully deleted.
[root@centos7 ~ 14:08:31]# nmcli connection
NAME UUID TYPE DEVICE
ens33 58de2941-93aa-4a12-9739-8aaf666d9abb ethernet ens33
jingtai 041b237c-e226-4e28-822a-055e24081b45 ethernet ens36
# 删除jingtai
[root@centos7 ~ 14:08:38]# nmcli connection delete jingtai
Connection 'jingtai' (041b237c-e226-4e28-822a-055e24081b45) successfully deleted.
[root@centos7 ~ 14:08:52]# nmcli connection
NAME UUID TYPE DEVICE
ens33 58de2941-93aa-4a12-9739-8aaf666d9abb ethernet ens33
4.修改dns
bash
[root@centos7 ~ 14:09:15]# nmcli connection modify ens33 ipv4.dns 223.5.5.5 +ipv4.dns 223.6.6.6
[root@centos7 ~ 14:13:37]# nmcli connection up ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[root@centos7 ~ 14:13:43]# cat /etc/resolv.conf
# Generated by NetworkManager
search jqz.cloud
nameserver 223.5.5.5
nameserver 223.6.6.6