存档2020年3月4日

Ubuntu修改系统时间

1 Ubuntu的时间分类
    1.Ubuntu时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟。
    2.查看系统时间:date -R
    3.查看硬件时间: sudo hwclock --show



2 修改Ubuntu系统时间
2.1 修改时间
    1. tzselect
    2. 依次选择4->9->1->1 #Asia->China-> Beijing->yes


2.2 复制文件到/etc目录下
sudo cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

2.3 查看修改结果
date -R

3 修改Ubuntu硬件时间
sudo date -s MM/DD/YY //修改日期
sudo date -s hh:mm:ss //修改时间
sudo hwclock --systohc //修改生效

apt-get install -y tzdata
dpkg-reconfigure tzdata

新版timedatectl
timedatectl set-time ’16:10:40 2015-11-20′
timedatectl set-time 15:58:30
timedatectl set-time 20151120
将你的硬件时钟设置为协调世界时(UTC):
timedatectl set-local-rtc 0
要开始自动时间同步到远程NTP服务器,在终端键入以下命令。
timedatectl set-ntp true
要禁用NTP时间同步,在终端键入以下命令。
timedatectl set-ntp false
查看状态
timedatectl status

timedatectl 和 timesyncd 用来获取时间的 nameserver 可以通过 /etc/systemd/timesyncd.conf 来指定,另外在 /etc/systemd/timesyncd.conf.d/ 下还有灵活的附加配置文件

老的同步
apt install ntp ntpdate -y
选择一台服务作为 ntp 时间服务器,修改/etc/ntp.conf文件,删除:
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
改为 pool
server ntp3.aliyun.com iburst
其它服务 pool
server 178.100.101.6 prefer
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
去掉#打开注释,并将其ip修改服务器所在网段,
restrict 178.100.0.0 mask 255.255.0.0 nomodify notrap

在集群中的其他服务器上,也要修改此文件,删除掉:
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
加入:
server 192.168.210.101 prefer

集群中的所有服务器上执行如下命令:
service ntpd restart /etc/init.d/ntp restart
systemctl disable chronyd.service
chkconfig –level 345 ntpd on

集群中192.168.210.101以外的服务器上执行:
ntpq -p

ntpdate 178.100.101.6
sudo hwclock -w
客户端同时时出现:no server suitable for synchronization found 错误提示
在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误

如何知道何时ntp server完成了和自身同步的过程呢?
  在ntp server上使用命令:
  #ntpq -p
查看所有服务
service –status-all