页面“Linux时间同步的那些事儿”与“Debian服务器初始化”之间的差异

来自linux中国网wiki
(页面间的差异)
跳到导航 跳到搜索
 
 
第1行: 第1行:
== centos7(debian10)从外网同步时间和时区设置==
 
<pre>
 
  
# 设置亚洲时区 tzselect 有时不准备
+
[[Salt-ssh批量初始化机器]]
timedatectl set-timezone Asia/Shanghai
+
= os init=
# 启用NTP同步 #关闭是 false
+
== change sources ==
timedatectl set-ntp true # yes
+
===10源===
#关闭
 
timedatectl set-ntp false
 
 
 
 
 
set-ntp [BOOL]
 
          Takes a boolean argument. Controls whether network time synchronization is active and enabled (if available). If the argument is true, this enables and starts the first
 
          existed service listed in the environment variable $SYSTEMD_TIMEDATED_NTP_SERVICES of systemd-timedated.service. If the argument is false, then this disables and stops the
 
          all services listed in $SYSTEMD_TIMEDATED_NTP_SERVICES.
 
 
 
 
 
date -s "2019-05-23 01:01:01"
 
 
 
019
 
[root@prod-sns-php01 ~]#  timedatectl set-ntp true
 
Failed to set ntp: NTP not supported.
 
yum install -y ntp
 
 
 
 
 
 
 
 
 
 
 
 
 
#下面几个可以不要    安装
 
yum install chrony
 
# 启用
 
systemctl start chronyd
 
systemctl enable chronyd
 
 
 
timedatectl set-time 15:58:30
 
Failed to set time: Automatic time synchronization is enabled
 
root@debian-hyper:~# timedatectl set-ntp  no  #or
 
timedatectl set-ntp false
 
root@debian-hyper:~# timedatectl set-time 15:58:30  #修改时间
 
 
 
 
 
</pre>
 
 
 
==基础知识==
 
timedatectl命令对于RHEL / CentOS 7和基于Fedora 21+的分布式系统来说,是一个新工具,它作为systemd系统和服务管理器的一部分,代替旧的传统的用在基于Linux分布式系统的sysvinit守护进程的date命令、
 
在最新的 Ubuntu 版本中,timedatectl 替代了老旧的 ntpdate。默认情况下,timedatectl 在系统启动的时候会立刻同步时间,并在稍后网络连接激活后通过 socket 再次检查一次。
 
 
 
  如果已安装了 ntpdate / ntp,timedatectl 会退而让你使用之前的设置。这样确保了两个时间同步服务不会相互冲突,同时在你升级的时候还保留原本的行为和配置。但这也意味着从旧版本的发行版升级时ntp/ntpdate 仍会安装,因此会导致新的基于 systemd 的时间服务被禁用。
 
 
 
 
 
 
 
    UTC 整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)。
 
 
 
    GMT 格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。(UTC与GMT时间基本相同,本文中不做区分)
 
 
 
    CST 中国标准时间 (China Standard Time)
 
 
 
 
 
[https://my.oschina.net/airship/blog/1544886 timedatectl时钟同步]
 
 
 
[https://blog.csdn.net/l1212xiao/article/details/80328918 CentOS 7时间和日期和时间同步]
 
 
 
==centos7  chrony 自建的时间同步 server==
 
 
<pre>
 
<pre>
Chrony是一个开源的自由软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确。它由两个程序组成:chronyd和chronyc。
 
          chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。
 
          Chrony 是网络时间协议的另一种实现,与网络时间协议后台程序(ntpd)不同,它可以更快地且准确的同步系统时钟。
 
 
 
centos7  建议是用 chrony
 
 
yum -y install chrony ntp  -y
 
 
systemctl start chronyd
 
systemctl status chronyd
 
systemctl enable  chronyd
 
 
  
timedatectl set-timezone Asia/Shanghai
+
deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
 
+
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
timedatectl set-ntp true
+
deb http://mirrors.aliyun.com/debian-security buster/updates main
+
deb-src http://mirrors.aliyun.com/debian-security buster/updates main
crontab  -l  #如果实在要用
+
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
* */2 * * * ntpdate time.windows.com
+
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
 
+
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
yum install chrony -y
+
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
 
 
#改配置
 
cat /etc/chrony.conf
 
 
 
#没用的全删除了
 
# Use public servers from the pool.ntp.org project.
 
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
 
server  news.neu.edu.cn iburst
 
#server 0.centos.pool.ntp.org iburst
 
 
 
 
 
systemctl  start chronyd.service && systemctl  enable chronyd.service
 
 
 
 
 
测试
 
 
 
故意超时
 
date -s "2020-11-11 11:11:11"
 
 
 
哪个命令fix对了时间的
 
重启这个就行了 其实是不是不用动也行 过了阵就会自己同步
 
  systemctl restart chronyd.service
 
 
 
timedatectl set-ntp yes
 
  
 
</pre>
 
</pre>
===chrony 使用 ===
+
===9 源===
 
<pre>
 
<pre>
使用chronyc
 
你也可以通过运行chronyc命令来修改设置,命令如下:
 
  
accheck - 检查NTP访问是否对特定主机可用
+
echo 'deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib
 
+
deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib
activity - 该命令会显示有多少NTP源在线/离线
+
deb http://mirrors.aliyun.com/debian-security stretch/updates main
 
+
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main
 
+
deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
 
+
deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
add server - 手动添加一台新的NTP服务器。
+
deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
 
+
deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib ' >sources.list
clients - 在客户端报告已访问到服务器
+
</pre>
 
 
delete - 手动移除NTP服务器或对等服务器
 
 
 
settime - 手动设置守护进程时间
 
 
 
tracking - 显示系统时间信息
 
  
 +
== ssh config==
 +
<pre>
 +
echo "ssh-rsa AAAAB3NzaC you_prk_key root@ops
 +
"  >> /root/.ssh/authorized_keys
  
 +
sed -i "s/#PubkeyAuthentication yes/PubkeyAuthentication yes/g" /etc/ssh/sshd_config
  
#有用 ?
+
sed -i "s/^PasswordAuthentication yes/PasswordAuthentication no/g" /etc/ssh/sshd_config
将系统时间写入硬件时间
 
  
  sudo hwclock --systohc
+
systemctl restart sshd
 +
#service sshd restart
  
 
</pre>
 
</pre>
 
+
==常用软件==
==老方法 centos7==
 
 
<pre>
 
<pre>
#proxmox 上的 vm centos7 老是时间不对 ,以前用的ntpd老是不成功呢
+
useradd -d /data/evan  -s /bin/bash -m  evan
 +
数m表示如果该目录不存在,则创建该目录
  
yum install ntp  -y
 
timedatectl set-timezone Asia/Shanghai
 
timedatectl set-ntp true
 
ntpq -p
 
  
 +
apt install net-tools  rsync wget  firewalld  vim  build-essential dnsutils screen  curl sudo lsb-release  iotop software-properties-common  -y  #dig dnsutils
  
#设置系统时间为中国时区并启用NTP同步
+
#全面的开发工具
yum install ntp -y#//安装ntp服务
+
sudo apt  install git golang build-essential gcc g++ gdb libboost-dev make automake autogen autoconf cscope global cmake cmake-gui astyle clang-format clang llvm lldb libsqlite3-dev sqlite3 bison flex ruby-dev linux-headers-`uname -r`
systemctl enable ntpd #//开机启动服务
 
systemctl start ntpd #//启动服务
 
timedatectl set-timezone Asia/Shanghai #//更改时区
 
timedatectl set-ntp yes # 用的是true ?//启用ntp同步
 
ntpq -p //同步时间
 
  
  
</pre>
+
#ps
 +
apt install procps
  
  
 +
安装Fail2Ban
 +
</pre>
  
<pre>
+
[https://www.debian.cn/archives/2880 Debian 安装 fail2ban 方式SSH爆破攻击]
CentOS配置时间同步NTP
 
为什么要使用ntpd而不是ntpdate?
 
原因很简单,ntpd是步进式的逐渐调整时间,而ntpdate是断点更新,比如现在服务器时间是9.18分,而标准时间是9.28分,ntpd会在一段时间内逐渐的把时间校准到与标准时间相同,而ntpdate会立刻把时间调整到9.28分,如果你往数据库内写入内容或在其他对时间有严格要求的生产环境下,产生的后果会是很严重的。(注:当本地时间与标准时间相差30分钟以上是ntpd会停止工作)
 
 
 
NTP通信协议原理
 
  
/usr/sbin/ntpd: 主要提供 NTP 服务的程序啰!配置文件为 /etc/ntp.conf
 
/usr/sbin/ntpdate: 用于客户端的时间校正,如果你没有要启用 NTP 而仅想要使用 NTP Client 功能的话,那么只会用到这个指令而已啦!
 
  
#这个运行不了 ali的是直接跑在  /etc/ntpd.conf配置的 
+
[[Debian配置iptables]]
*/10 * * * * /usr/sbin/ntpd cn.pool.ntp.org > /dev/null  2>&1
 
  
*/10 * * * * /usr/sbin/ntpdate cn.pool.ntp.org > /dev/null  2>&1
+
=时间同步=
</pre>
 
 
 
=debian=
 
 
<pre>
 
<pre>
 
UTC时区切换到CST 时区
 
UTC时区切换到CST 时区
  
 
#用这个啦  
 
#用这个啦  
# 设置亚洲时区
+
# 设置亚洲时区 tzselect 有时不准
 
timedatectl set-timezone Asia/Shanghai
 
timedatectl set-timezone Asia/Shanghai
 
# 启用NTP同步 #关闭是 false  
 
# 启用NTP同步 #关闭是 false  
第195行: 第80行:
 
  cat /etc/profile |grep TZ   
 
  cat /etc/profile |grep TZ   
 
source /etc/profile
 
source /etc/profile
date -R
+
 
 +
date -R #时区查看
 
  date  
 
  date  
 
Sat Aug 19 17:03:17 CST 2017
 
Sat Aug 19 17:03:17 CST 2017
 
debian 手工修改时间 
 
 
#将系统日期设定成1996年6月10日的命令
 
date -s 06/22/96
 
 
sudo  date  -s 10:20:00
 
sudo hwclock -w  #当前时间和日期写入BIOS,避免重启后失效 2020用了这种 有效果
 
 
 
</pre>
 
</pre>
  
==Linux 修改系统时间成功未生效==
+
=security=
<pre>
+
==firewalld==
使用 date -s “2019-02-02 02:02:02” 命令修改系统时间,提示修改成功但是系统时间未发生变化;
 
 
 
 
 
使用timedatectl命令查看系统时间
 
 
 
timedatectl
 
              Local time: Wed 2020-02-26 22:28:22 PST
 
          Universal time: Thu 2020-02-27 06:28:22 UTC
 
                RTC time: Thu 2020-02-27 06:28:22
 
                Time zone: America/Los_Angeles (PST, -0800)
 
System clock synchronized: yes
 
              NTP service: active
 
          RTC in local TZ: no
 
 
 
关闭ntp同步 timedatectl set-ntp false
 
 
 
 
 
 
 
</pre>
 
 
 
=see also=
 
 
 
 
 
[https://www.cnblogs.com/zhi-leaf/p/6282301.html  Linux下使用timedatectl命令时间时区操作详解]
 
 
 
[https://www.fujieace.com/linux/man/ntpdate-hwclock.html Linux时间同步命令:ntpdate(系统时间)、hwclock(硬件时间)]
 
 
 
[https://www.iteye.com/blog/justcoding-1962519 Linux查看与修改时区、时间的命令 ]
 
 
 
 
 
 
 
[[Centos7 NTP网络时间协议]]
 
 
 
[https://www.cnblogs.com/tangxiaosheng/p/4986375.html CentOS 7 时间, 日期设置 (含时间同步)]
 
 
 
http://cn.linux.vbird.org/linux_server/0440ntp.php
 
 
 
[https://blog.itnmg.net/2015/03/03/centos-7-time-date/ CentOS 7 时间, 日期设置]
 
 
 
[https://blog.csdn.net/scorpio3k/article/details/49645219 CentOS7 中使用NTP进行时间同步]
 
 
 
[http://blog.51cto.com/dadonggg/1947882 使用NTP进行CentOS7时间同步]
 
  
[http://luijnijei.blog.163.com/blog/static/350245942010913912192/ linux时间同步,ntpd、ntpdate ]
+
详情可见 [[Centos7 firewalld防火墙基础]]
  
[http://151wqooo.blog.51cto.com/2610898/1336282 服务器时间同步]
+
[https://computingforgeeks.com/how-to-install-and-configure-firewalld-on-debian/ How To Install and Configure Firewalld on Debian 10]
  
 +
[https://ywnz.com/linuxaq/5495.html 在Debian 10(Buster)上安装和配置Firewalld]
  
[http://xstarcd.github.io/wiki/sysadmin/ntpd.html ntpd时钟同步服务]
+
=参考=
  
==chrony==
+
[https://blog.51cto.com/wzlinux/2043586 Ubuntu 新装服务器部署流程]
  
[https://soyoger.blog.csdn.net/article/details/77868337 使用chrony代替ntp同步时间]
+
[https://www.howtoing.com/install-java-in-debian-and-ubuntu 如何在Debian和Ubuntu系统中安装Java 9]
  
[https://blog.csdn.net/Linuxprobe18/article/details/80460068 详解:Linux Chrony 设置服务器集群同步时间]
+
[http://www.ruanyifeng.com/blog/2014/03/server_setup.html Linux服务器的初步配置流程]
  
[https://blog.csdn.net/openbox2008/article/details/80250027 centos7.4 用chrony代替ntpd时间同步服务器]
+
[http://spenserj.com/blog/2013/07/15/securing-a-linux-server/ Securing a Linux Server]
  
[https://blog.csdn.net/JIANG123456T/article/details/77966627 Centos 7.3_1611使用chrony做时间同步]
+
[http://blog.51cto.com/feihan21/1060365 Linux服务器初始化配置脚本]
  
[https://blog.csdn.net/weixin_43875013/article/details/86485798 linux中时间同步服务(chronyd服务)以及系统时间设定]
+
[https://blog.imdst.com/linux-fu-wu-qi-chu-shi-hua-an-quan-jia-gu/ Linux服务器初始化调优及安全加固]
  
[https://blog.csdn.net/hnhuangyiyang/article/details/52711631 CentOS7.2 chrony替代ntp搭建时间服务器]
+
[https://linux.cn/article-5067-1.html 如何使用 fail2ban 防御 SSH 服务器的暴力破解攻击]
  
[https://blog.csdn.net/weixin_34290631/article/details/90571832 CentOS 7.x中正确设置时间与时钟服务器同步]
+
[https://blog.csdn.net/developerinit/article/details/73065229?utm_source=blogxgwz7 Debian的一些常用命令]
  
[https://blog.csdn.net/hnhuangyiyang/article/details/52711631  CentOS7.2 chrony替代ntp搭建时间服务器]
 
  
[[category:ops]]
+
[https://www.cnblogs.com/yoyotl/p/8151409.html Debian 8 设置时区和时间配置]
 +
[[category:ops]]  [[category:debian]]

2020年2月27日 (四) 08:24的版本

Salt-ssh批量初始化机器

os init

change sources

10源


deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian-security buster/updates main
deb-src http://mirrors.aliyun.com/debian-security buster/updates main
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib

9 源


echo 'deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb http://mirrors.aliyun.com/debian-security stretch/updates main
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main
deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib ' >sources.list

ssh config

echo "ssh-rsa AAAAB3NzaC you_prk_key root@ops
"  >> /root/.ssh/authorized_keys

sed -i "s/#PubkeyAuthentication yes/PubkeyAuthentication yes/g" /etc/ssh/sshd_config

sed -i "s/^PasswordAuthentication yes/PasswordAuthentication no/g" /etc/ssh/sshd_config

systemctl restart sshd
#service  sshd restart

常用软件

useradd -d /data/evan  -s /bin/bash -m  evan
数m表示如果该目录不存在,则创建该目录


apt install net-tools  rsync wget  firewalld  vim  build-essential dnsutils screen  curl sudo lsb-release  iotop software-properties-common  -y  #dig dnsutils 

#全面的开发工具
sudo apt  install git golang build-essential gcc g++ gdb libboost-dev make automake autogen autoconf cscope global cmake cmake-gui astyle clang-format clang llvm lldb libsqlite3-dev sqlite3 bison flex ruby-dev linux-headers-`uname -r`


#ps 
apt install procps


安装Fail2Ban
 

Debian 安装 fail2ban 方式SSH爆破攻击


Debian配置iptables

时间同步

UTC时区切换到CST 时区

#用这个啦 
# 设置亚洲时区  tzselect 有时不准
timedatectl set-timezone Asia/Shanghai
# 启用NTP同步 #关闭是 false 
timedatectl set-ntp yes


 echo "export TZ='Asia/Shanghai'"  >> /etc/profile  
 cat /etc/profile |grep TZ  
source /etc/profile

date -R  #时区查看 
 date 
Sat Aug 19 17:03:17 CST 2017

security

firewalld

详情可见 Centos7 firewalld防火墙基础

How To Install and Configure Firewalld on Debian 10

在Debian 10(Buster)上安装和配置Firewalld

参考

Ubuntu 新装服务器部署流程

如何在Debian和Ubuntu系统中安装Java 9

Linux服务器的初步配置流程

Securing a Linux Server

Linux服务器初始化配置脚本

Linux服务器初始化调优及安全加固

如何使用 fail2ban 防御 SSH 服务器的暴力破解攻击

Debian的一些常用命令


Debian 8 设置时区和时间配置