设置linux时间为北京时间

本文详细介绍了在Linux系统中手动设置系统时间和硬件时间的方法,以及如何通过NTP与时间服务器同步,确保网络内各服务器时间一致。还提供了将一台服务器配置为NTP服务器的步骤,以便其他服务器进行时间同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章转载于:

1、https://blog.youkuaiyun.com/u010316188/article/details/79503376?utm_source=blogxgwz9

2、https://blog.youkuaiyun.com/weixin_35852328/article/details/79506453

1、手动设置

Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC)。

系统时间:指当前Linux Kernel中的时间。

硬件时间:主板上有电池供电的时间。

查看系统时间的命令: #date

设置系统时间的命令: #date –set(月/日/年 时:分:秒)

例:#date –set “10/11/10 10:15”

查看硬件时间的命令: # hwclock

设置硬件时间的命令: # hwclock –set –date = (月/日/年 时:分:秒)

上述提到的是手动设置时间到一个时间点,可能与当前网络的时间有误差。

2、与时间服务器上的时间同步

  1. 安装ntpdate工具

      yum -y install ntp ntpdate

     2、设置时间为阿里服务器的时间

      ntpdate ntp1.aliyun.com

     3、将系统时间写入硬件时间

      hwclock –systohc

3、把自己的一台服务器当成时间服务器,可以让别的服务器来同步

      搭建步骤,我这里是centos系统,不同系统可能会不一样
      3.1、修改vi /etc/ntp.conf 文件,可参考

[root@learn ~]# cat /etc/ntp.conf 
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
 
driftfile /var/lib/ntp/drift
 
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
 
# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1 
restrict -6 ::1
 
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.37.255 mask 255.255.255.0 nomodify notrap
##开启上面这一行,去掉注释,  这里192.168.37.255是你电脑广播地址,在ifconfig命令中能看到【inet addr:192.168.37.141  Bcast:192.168.37.255  Mask:255.255.255.0】
#Use public servers from the pool.ntp.org project.
#Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
#由于是把自己电脑机器当成ntpd服务器,不需要连接外网,所以注释下上面四行,  添加下面两行,
server 127.127.1.0 #local clock
fudge 127.127.1.0 startnum 10
#broadcast 192.168.1.255 autokey	# broadcast server
#broadcastclient			# broadcast client
#broadcast 224.0.1.1 autokey		# multicast server
#multicastclient 224.0.1.1		# multicast client
#manycastserver 239.255.254.254		# manycast server
#manycastclient 239.255.254.254 autokey # manycast client
 
# Enable public key cryptography.
#crypto
 
includefile /etc/ntp/crypto/pw
 
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys
 
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
 
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
 
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
 
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
--------------------- 


      3.2、修改完成开启ntpd服务器
              [root@learn ~]# sudo service ntpd start  --开启ntpd服务器
              [root@learn ~]# sudo chkconfig ntpd on --设置ntpd服务
      3.3、其他机器可通过ntpdate命令来同步时间
              /usr/sbin/ntpdate learn.hadoop1.com --这里的learn.hadoop1.com是我的机器主机名

### 配置Linux系统时间北京时间 为了确保Linux系统的时区被正确设置北京标准时间,可以按照以下方法操作: #### 使用`timedatectl`命令调整时区 现代版本的Linux发行版通常推荐使用`timedatectl`来管理时间和日期。通过这条指令能够方便地查询并更改系统的时区。 要查看当前的时间配置情况,执行: ```bash timedatectl status ``` 如果发现时区不正确,则可以通过下面的命令将其更改为亚洲/上海(即中国的东八区),这同样适用于北京地区[^1]: ```bash sudo timedatectl set-timezone Asia/Shanghai ``` #### 修改`/etc/localtime`链接指向正确的时区文件 对于某些较老的操作系统或者特定需求下,可能还需要手动更新`/etc/localtime`文件以反映新的时区设定。具体做法是从`/usr/share/zoneinfo/`目录下的相应位置创建软连接至该路径: 删除现有的localtime文件(如果有),然后建立新链接: ```bash sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ``` #### 设置硬件时钟(HW Clock) 考虑到Windows操作系统默认保存的是本地时间而非协调世界时(UTC), 如果双系统共存的话,在安装Linux期间应该避免选择将BIOS中的RTC(Reliable Time Clock)设为UTC模式。否则可能导致两个不同平台间显示不同的实际时刻差异。 完成上述步骤之后再次运行 `date` 命令验证现在的输出是否已经变为符合预期的+0800偏移量了[^2]: ```bash date -R ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值