「Linux」- 服务管理、进程管理

daemontools daemontools-encore: Derived from the public-domain release of daemontools Eye: A Ruby implementation Finit: Fast, Extensible Init for Linux Systems God: A Ruby implementation immortal: A Go implementation PM2: A Process Manager for Node.js Initng launchd minit: A small, yet feature-complete Linux init Monit runit Supervisor: A Python implementation s6: Low-level process and service supervision systemd
参考文献
Process supervision – Wikipedia

章节列表
「Supervisor」- 创建首个配置文件 「Upstart」- 基于事件的初始化守护进程 「systemd」- System Software,Process Supervisor 「Supervisor」- A Process Control System 「systemd」- 常见错误汇总 「systemd」- 查看单元文件、分析依赖关系(systemd-analyze) 「LSB – Linux Standard Base」 「LSB Init Scripts」[……]

READ MORE

「Linux」- SysV-style init

System V 风格 init 程序,用于控制系统的启动和关闭。由 Miquel van Smoorenburg 编写的。
参考文献
Homepage:https://savannah.nongnu.org/projects/sysvinit LFS:http://www.linuxfromscratch.org/lfs/view/stable/chapter06/sysvinit.html Cookbook: https://savannah.gnu.org/cookbook/?group=administration In Depth Guide: https://savannah.nongnu.org/userguide/ FAQ: http://savannah.gnu.org/maintenance/FrontPage/[……]

READ MORE

「LSB – Linux Standard Base」

参考文献
LSB:http://refspecs.linuxfoundation.org/ LSB 3.1:http://refspecs.linuxfoundation.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/book1.html[……]

READ MORE

「LSB Init Scripts」

LSB Init Scripts,LSB初始化脚本,用于控制系统服务的启动、停止、重启、状态查看等等。
我也不知道我为什么不喜欢Systemd :(。因为要用到VirtualBox虚拟机,我也不想每次开机以后都去手动启动虚拟机,所以学习一下LSB Init Scripts,开机的时候可以自己启动。
以内容,如果没有特殊说明,“LIS”一词默认指的都是LSB初始化脚本。
有关详细内容查看LBS3.1的第20章
脚本的结构
LSB注释头 引入函数库 动作(Actions)
LSB注释头
在/etc/init.d下,有一个skeleton文件,该文件的内容是LIS的基本框架。下面是/etc/init.d/skeleton文件的内容:

#!/bin/sh
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != “$INIT_D_SCRIPT_SOURCED” ] ; then
set “$0” “$@”; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides: skeleton
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d. This example start a
# single forking daemon capable of writing a pid
# file. To get other behavoirs, implemend
# do_start(), do_stop() or other functions to
# override the defaults in /lib/init/init-d-script.
### END INI[……]

READ MORE

「Upstart」- 基于事件的初始化守护进程

CentOS 6.x 使用 Upstart init
参考文献
Upstart (software) – Wikipedia[……]

READ MORE

「Supervisor」- A Process Control System

Supervisor
进程管理工具,类似与 init 或 systemd 进程,但是并不是用来替代它们的。
问题解决方案
1)避免编写复杂的 rc.d 脚本来管理服务,简化服务进程的管理工作; 2)重新启动崩溃的进程; 3)能够精准获取子进程状态,而不在依靠 Pidfile(不准确); 4)简化进程管理、提供网页界面; 4)进程可以分区并设置权重,以批量管理;
我们会在 CenOS 6.x 中使用 Supervisor 管理进程;对于 CentOS 7.X 我们使用 systemd 管理。除了配置简单,我们并没有觉得 Superviosr 比 systemd 功能强大且可靠。
实际应用场景
在 CentOS 6.5 中,管理 MyCat 进程:在开机时自动启动;在崩溃时重新启动;
参考文献
Supervisor 4.0/Introduction[……]

READ MORE

「Supervisor」- 安装

依赖环境
Supervisor is intended to work on Python 3 version 3.4 or later and on Python 2 version 2.7.
注意事项
如果从发行版的仓库中安装,那么安装的 Supervisor 版本会落后于官方版本。比如 CentOS 6.5 仓库中只包含 Supervisor 2.1 版本。
如果从发行版安装 Supervisor 是为了自带的服务管理脚本,那大可不必: GitHub/Supervisor/initscripts How to automatically start supervisord on Linux (Ubuntu)
Supervisor 2.1 on CentOS 6.5
Supervisor/Installing a Distribution Package

yum install supervisor-2.1
service supervisord start
chkconfig supervisord on

参考文献
Supervisor 4.0/Introduction[……]

READ MORE

「Supervisor」- 常见问题处理

操作流程概述
实验环境:Ubuntu 20.04 LTS
第一步、创建配置文件,并保存到 /etc/supervisor/conf.d/ 目录,不建议直接修改 supervisord.conf 文件;
第二步、加载配置文件(Supervisor not loading new configuration files):

supervisorctl reread
supervisorctl update

第三步、启动服务:

supervisorctl start all

第三步、停止服务:

supervisorctl stop “your service name”

# 注意事项:
# 1)某些程序会派正子进程,需要配置 stopasgroup=true 完全退出(包括子进程)# https://stackoverflow.com/questions/9090683/supervisord-stopping-child-processes

获取帮助:

# supervisorctl help

default commands (type help <topic>):
=====================================
add exit open reload restart start tail
avail fg pid remove shutdown status update
clear maintail quit reread signal stop version[……]

READ MORE

「Supervisor」- 创建首个配置文件

问题描述
本笔记将记录:配置 Supervisor 并管理 memcached 服务的方法。
解决方案
我们使用 CentOS 自带的 Supervisor 2.1 版本,并假设 Supervisor 服务已启动。
第一步、创建配置文件
使用命令 echo_supervisord_conf 创建配置文件:

echo_supervisord_conf > /etc/supervisord.conf

第二步、创建服务配置
创建配置并写入 /etc/supervisord.conf 中:

cat >> /etc/supervisord.conf <<EOF

[program:memcached]
command=/usr/local/memcached/bin/memcached -p 22122 -U 0 -r -u root -m 2040 -c 1024 -t 4
EOF

第三步、重新加载配置
发送信号通知 Supervisor 重新加载配置:

kill -HUP ‘<supervisor pid>’
killall -HUP supervisord # 在 CentOS 中,需要安装 psmisc 软件包

第四步、检查服务状态

# supervisorctl status all

# nc -z -v 127.0.0.1 22122
Connection to 127.0.0.1 22122 port [tcp/*] succeeded!

常见问题汇总
unix:///tmp/supervisor.sock no such file
Bug 1000190 – Supervisor unix HTTP server socket doesn’t get created
问题描述:

# supervisorctl
unix:///tmp/supervisor.sock no such file

问题原因: The [unix_http_server] section is part of supervisor 3: http://supervisord.org/upgrading.html#upgrading-supervisor-2-to-3
解决办法: You must configure the http_port option in the [supervisord] section of the config file. The default config file:

[superviso[……]

READ MORE

「systemd」- System Software,Process Supervisor

[……]

READ MORE

「systemd」- 概念、术语

System Instance and User Instance
在阅读「systemd-system.conf」手册时,有这样一段话:

When run as a system instance, systemd interprets the configuration file system.conf and the files in system.conf.d directories; when run as a user instance, systemd interprets the configuration file user.conf and the files in user.conf.d directories.

问题就是:什么是 system instance?什么是 user instance?
「系统服务」是由「系统级 systemd 实例」管理。
「用户服务」是由「用户级 systemd 实例」管理。
「系统级 systemd 实例」只能有一个,它总是运行的,进程ID 为 1 。只有系统管理员才能管理它的服务。
「用户级systemd实例」是以命令systemd –user形式运行,你可以看一下系统中systemd进程数及它们的运行方式,这个在桌面环境下比较明显。它们的进程ID不为 1 。每个用户只有一个实例。
参考文献
systemd/Directives/systemd-system.conf What is the difference between systemd’s user and system services?[……]

READ MORE

「systemd」- 常见问题处理

访问systemd的首页
在Serivce单元文件中使用管道

#!/bin/sh

# 在指令ExecStart的参数中,命令后的内容会被视为参数,所以对于管道应该使用如下形式:

ExecStart=/bin/sh -c ‘/apppath/appname > /filepath/filename 2>&1’

# 下面是一种错误的用法:

ExecStart=/apppath/appname > /filepath/filename 2>&1

# 则systemd会将/apppath/appname视为参数,而“>”、“/filepath/filename”、“2>&1”则会被视为命令参数。

“运行级别”
在 systemd 中,已经没有了以往“运行级别”的概念了,而是“目标(Target)”。下面是“运行级别”和“目标”的对应关系:

Run level 0 is matched by poweroff.target (and runlevel0.target is a symbolic link to poweroff.target).
Run level 1 is matched by rescue.target (and runlevel1.target is a symbolic link to rescue.target).
Run level 3 is emulated by multi-user.target (and runlevel3.target is a symbolic link to multi-user.target).
Run level 5 is emulated by graphical.target (and runlevel5.target is a symbolic link to graphical.target).
Run level 6 is emulated by reboot.target (and runlevel6.target is a symbolic link to reboot.target).
Emergency is matched by emergency.target.

查看默认“目标”:systemctl get-default 设置默认“目标”:systemctl set-default multi-user.target 变更“运行级别”:systemctl isolate multi-user.target
How to Change Runlevels (targets) in SystemD[……]

READ MORE

「systemd」 – 创建和修改单元文件

问题描述
作为一个系统管理员,要遵循系统管理规范,而不能说”能用就行“。本文将介绍如何编写systemd单元文件。
注意事项
这不是一个详细的教程。这篇文章是在我学习如何编写systemd单元文件时的一个摘要笔记。文章的内容只是一个概括,涵盖了创建单元文件可能用到的知识点和相关的参考手册,并不涉及太多的细节,细节上的东西只能去读文档了。
单元文件的存储位置
单元文件的保存位置:Table 10.2, “Systemd Unit Files Locations”
/usr/lib/systemd/system/ Systemd unit files distributed with installed RPM packages.
/run/systemd/system/ Systemd unit files created at run time. This directory takes precedence over the directory with installed service unit files.
/etc/systemd/system/ Systemd unit files created by systemctl enable as well as unit files added for extending a service. This directory takes precedence over the directory with runtime unit files. the /etc/systemd/system/ directory is reserved for unit files created or customized by the system administrator.
单元文件的命名方式
形如:unit_name.type_extension
unit_name:单元名。 type_extension:单元类型。可用的类型参考:Table 10.1, “Available systemd Unit Types”
单元文件结构
单元文件由 [Unit]、[unit type]、[Install] 三部分组成,每个部分都包含多种不同的指令。
[Unit]
解释:包含描述、依赖等指令,是一些更通用的指令,这些指令与单元类型无关。
参考 systemd.unit/[Unit] Section Options 文档,获取 [Unit] 部分的指令说明。
[<unit type>]
参数unit type不是固定的,可以是Service、Timer等等,它代表单元文件的类型。比如,如果服[……]

READ MORE

「使用systemd在开机时自动挂在硬盘」

Recently, I discovered you can mount partitions using systemd.mount by writinga mount unit file. In this blog post, we”ll talk about systemd.mount & how you can use it to mount partitions.
Systemd is gradually becoming the de facto init system & service manager replacing the old sysV init scripts & upstart. Recently, I discovered you can mount partitions using systemd.mount by writing your own .mount systemd unit file.
After RTFM’ing, I realized, under the hood, systemd just runs mount command to mount the specified partition with the specified mount options listed in the mount unit file. Basically, you need to specify the following options in your unit file:

What= a partition name, path or UUID to mount
Where= an absolute path of a directory i.e. path to a mount point. If the mount point is non-existent, it will be created
Type= file system type. In most cases mount_command auto-detects the file

system

Options= Mount options to use when mounting

In the end, you can convert your typical fstab entry such as this:

UUID=86fef3b2-bdc9-47fa-bbb1-4e528a89d222 /mnt/backups ext4 defaults 0 0

to:

[Mou[……]

READ MORE

「systemd」- 配置文件

修改system.conf后,如何生效?
修改/etc/systemd/system.conf文件之后,不要执行systemctl daemon-reload命令。因为该命令是用于重新加载单元文件的,而不是systemd自身的配置。
应该执行systemctl daemon-reexec命令,它将重新执行systemd命令,并加载配置文件。
详细内容可以参考「systemctl/daemon-reexec」手册。
参考文献
reboot or “systemctl daemon-reload” for changes to /etc/systemd/system.conf?[……]

READ MORE

「systemd」- 查看单元文件、分析依赖关系(systemd-analyze)

解决方案
查看所有单元文件:

systemctl -l # –full

分析依赖关系:

systemctl list-dependencies # 虽然能够显示依赖关系,但是不够细微

systemd-analyze plot > startup_order.svg
systemd-analyze dot | dot -Tsvg > systemd.svg

常见问题处理
在磁盘挂载后,再启动服务:

[Unit]

After=local-fs.target

参考文献
yocto – Is there any way to list systemd services in linux “in-the-order-of” they were loaded? – Stack Overflow Running systemd unit directly after local-fs.target and before basic.target – Stack Overflow[……]

READ MORE

「systemd」- 从 systemctl status 中提取个别字段

问题描述
在 Shell Script 中,我们需要判断服务的运行状态,以作出相应的逻辑处理。
该笔记将记录:如何获取 systemd 管理的某个服务的运行状态。
解决方法
我们以 nginx 服务为例,判断服务的运行状态

# systemctl show nginx.service –property ActiveState
ActiveState=inactive

# systemctl start nginx.service

# systemctl show nginx.service –property ActiveState
ActiveState=active

如果希望在 Shell Script 中使用:

eval $(systemctl show nginx.service –property ActiveState)
if [ “$ActiveState” = “active” ]; then
echo “Nginx is running.”
else
echo “Nginx is stopped.”
fi

参考文献
python – how to get individual values from the output of systemctl status – Stack Overflow shell – Run a string as a command within a Bash script – Stack Overflow[……]

READ MORE

「systemd」- 常见错误汇总

# Unit XXX is transient or generated
Failed to enable unit: Unit /run/systemd/generator.late/jenkins.service is transient or generated
#
Jun 19 08:37:38 dns systemd[1]: named-sdb.service: main process exited, code=killed, status=11/SEGV[……]

READ MORE

「Linux」- “无聊”的命令

cowsay
xcowsay
sl
boxes
img2txt
w3m-img
# I couldn’t imagine a better waste of my time.
在终端里…看…视…频…
How to watch films/images without X? https://www.youtube.com/watch?v=EvCkBDuLgxY http://www.annasagrera.com/on-ascii-youtube-and-letting-go (1)Watch YouTube videos in terminal mplayer -vo caca video.mkv[……]

READ MORE

「CentOS」- 禁用「You have new mail in /var/spool/mail/root」提示

问题描述
在CentOS中,进行终端(命令行)操作时,经常会收到「You have new mail in /var/spool/mail/root」消息。该功能有时会影响操作,比如在命令输入的过程中,上面的消息会混进输入内容里,影响对输入内容的阅读及确认。
本文将介绍如何禁用「You have new mail in /var/spool/mail/root」消息。
解决办法
默认情况下,在Bourne shell中,每十分钟检查一次邮件。要在其他的某个时间间隔检查,可以将变量MAILCHECK设置为新的时间间隔(以秒为单位)。
例如,下面的命令告诉Shell每隔55秒检查一次:MAILCHECK=55
一个有用的技巧是将MAILCHECK设置为0值,此时Shell将在打印提示符时进行检查。换句话说,它会在每个命令后检查。在繁忙的系统上这可能会很慢。
如果你不想使用该功能,可以unset MAILCHECK环境变量。
参考文献
Disable “You have new mail” terminal message 21.8.2 For Bourne Shell Users[……]

READ MORE

「Linux / Shell」- 当退出“man”手册后,在屏幕上保留查看的内容

问题描述
当我们执行终端全屏的命令后,在退出时,终端的内容会被清空。比如man find,按“q”退出时,回到我们执行命令之前的状态,而不会在终端上保留执行man find命令产生的输出;
有时这并不方便。因为我们也许需要参照手册的提示内容,来键入后面的命令。因此需要保留命令在终端上的产生的输出;
本文的主要内容就是讨论如何在终端里保留全屏命令产生的输出;
原因分析
为什么屏幕的内容会被“清空”?
实际上并不是”清空“,这种行为来自某些(硬件)终端和大多数(软件)终端仿真器中提供的“备用屏幕(Alternate Screen)”功能。这会发生什么事情是一些“终端感知程序”切换到“备用屏幕”来完成他们的工作,并在他们被退出(或暂停等等)时切换回“正常屏幕”。这有效地清除了这些程序的最终输出,所以看起来像是被清空了;
因此入手点有两个:

禁止程序使用“备用屏幕”。这通过设置特定程序实现;
关闭“备用屏幕”功能。这通过设置终端程序实现;

下面将按照这两方面进行介绍;
通过设置特定程序实现
文章「Exorcising the Evil Alternate Screen」讨论了“备用屏幕”的问题,并提出了几个解决办法;
某些程序通过配置可以禁用“备用屏幕”:

命令 less(1)可以使用-X 选项,或者通过它支持的 LESS 环境变量;
命令 vim(1)可以通过取消 t_ti 和 t_te 变量;
关于 GNOME Terminal 参考「Stop gnome-terminal screen clear」一文;
终端复用程序 screen 可以使用altscreen off配置选项。而 tmux 可以在配置文件中添加set -g alternate-screen off配置行;

有关的详细内容和配置细节请参考对应的手册;
解决方案
通过设置终端程序实现
本部分旨在提用一个在 XTerm 中相对通用的方法,而不是去配置具体的程序来实现“禁用备用屏幕”;
方法一、配置 XTerm 控制序列
本部分参考「Is there any way to exit “less” without clearing the screen?」、「How can I still see the ‘man’ text after I quit man?」文章;
方法二、修改 .Xresource 文件
在$HOME/.Xresources 文件中加入:

XTerm*titleInhibit: true

然后,执行:xrdb $HOME/.Xres[……]

READ MORE

「Zsh」- Z shell (Zsh)

安装 Zsh 程序

# Debian GNU/Linux 10 (buster)
apt-get install zsh

使用 Oh My Zsh 框架(主题工具)
ohmyzsh/ohmyzsh: 🙃 A delightful community-driven (with 1700+ contributors) framework for managing your zsh configuration.
Oh My Zsh,让我们的终端五彩斑斓(然而,我们依旧使用 Bash,只是想体验一下)。通过 OMZ 可以扩展 zsh 功能,增强 zsh 体验。

安装 Oh My Zsh 框架
安装过程如下(这里记录手动安装,更多安装方法参考 ohmyzsh/README.md at master · ohmyzsh/ohmyzsh 文档):

# Debian GNU/Linux 10 (buster)
git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh
cp -v ~/.zshrc ~/.zshrc.orig
cp -v ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
# chsh -s $(which zsh) # 我们只是体验 Zsh 因此不会修改默认 Shell 程序
zsh # 之后,会提示相应的初始化步骤

手动升级:omz update 禁用升级提示:echo ‘DISABLE_UPDATE_PROMPT=true’ >> ~/.zshrc 开启自动升级:echo ‘DISABLE_AUTO_UPDATE=true’ >> ~/.zshrc
安装 powerlevel10k 主题
romkatv/powerlevel10k: A Zsh theme
powerlevel10k,让我们的终端(zsh)更加的五彩斑斓(HuaLiHuShao):

我们已经安装 Oh My Zsh 框架,因此采用与之相关的安装方法(其他安装方法参考 powerlevel10k/README.md at master · romkatv/powerlevel10k 文档)

# 第一步、安装字体
mkdir -pv /usr/local/share/fonts/powerlevel10k/
wget -P /usr/local/share/fonts/powerlevel10k/ https://github.com/romkatv/powerlevel10k-media/raw/master/M[……]

READ MORE

在umount时,出现「device is busy」的解决办法

参考文献
umount: device is busy. Why?[……]

READ MORE

「Shell」- 关于「printenv(1)」「env(1)」「set」之间的差异

本文主要介绍这几个命令在环境变量打印方面的差异;
printenv(1)
命令 printenv 用于打印所有或者部分环境变量;
但是,只能打印已经导出的 Shell 环境变量;
env(1)
命令 env 将仅显示已导出的环境变量列表,并且不会显示所有 bash 变量;
set
命令 set 是 Shell 中的一个内建命令,它能够显示当前 Shell 中的变量,已经用户自定义的变量,不管该变量有没有 export。set 命令允许你更改 Shell 选项的值并设置位置参数,或者显示 Shell 变量的名称和值。如果未提供任何选项或参数,则会设置显示所有 Shell 变量和函数的名称和值(按照当前语言环境排序),并且输出的格式可以重新用作设置或重置当前设置变量的输入;
最后总结
在我的 Debian 中,命令 env(1)和 printenv(1)的输出相同,唯一不同的地方是”_“环境变量(执行diff <(env) <(printenv)命令查看);
总的来讲 printenv(1)和 env(1)在环境变量打印方面是类似的。但是在其他方面,比如功能上,env(1)主要用于设置环境变量并运行指定的命令命令,而 printenv(1)是为了打印环境变量;
而 set 是一个 Shell 的内建命令,与 Shell 有关,用于设置 Shell 的属性;[……]

READ MORE

「Linux」- 进行时间校准

为什么每次开机之后,时间都是准确的,不会因为断电而时间不准确了?因为电脑上是有电池的,即使是台式机,主板上也有一颗纽扣电池,该电池是给CMOS模块供电的,而时间和配置相关的信息都保存在CMOS中。但如果电池没电了,或者时间不准确了,想校准时间怎么办?这篇文章会回答这个问题。
使用nptdate命令将系统时间与网络时间同步
ntpdate 如果有网络的话,推荐的做法是使用NTP Server来同步时间。
首先,查找可用的NTP Server 可以在ntp的「Server Pool Project」中查找国内的NTP Server列表,如下(08/10/2017):

server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 3.asia.pool.ntp.org

然后,使用ntpupdate来更新时间

ntpdate 0.asia.pool.ntp.org

关于NTP Server的更多细节,查看另外一篇文章“NTP Server”
手动设置系统时间
date 命令date(1)用于打印和设置系统时间。支持以指定的日期格式来打印。但这里介绍的主要内容为使用date(1)命令来设置时间。 date中的-s选项可以用来设置时间,比如:date -s “2015/03/30 12:34:16″。 关于date命令的更多使用细节,参见date(1)手册。
hwclock 命令 date 只能设置系统时间,而修改硬件时间需要使用 hwclock 命令。 hwclock -w 可以将当前的系统时间写入CMOS中。 关于 hwclock 命令命令的更多细节,查看 hwclock(8) 手册。
参考文献
LFS:http://www.linuxfromscratch.org/blfs/view/stable/basicnet/ntp.html vbird:http://linux.vbird.org/linux_server/0440ntp.php NTP Homepage:http://linux.vbird.org/linux_server/0440ntp.php wiki:https://en.wikipedia.org/wiki/Network_Time_Protocol[……]

READ MORE

「NTP」- 常见错误列表

#1 no server suitable for synchronization found

# ntpdate -v 103.213.249.202
5 Apr 23:25:30 ntpdate[32496]: ntpdate 4.2.8p10@1.3728-o (1)
5 Apr 23:25:38 ntpdate[32496]: no server suitable for synchronization found

原因:这个问题的原因可能会有很多种,说以下我遇到的情况:103.213.249.202是我自己的NTP服务,我在服务器中配置了防火墙,导致NTP服务在请求上游的服务器时,数据包被防火墙过滤掉了。解决办法就是添加防火墙规则。
解决:iptables -A INPUT -i eth0 -p udp -m udp –dport 123 –sport 123 -j ACCEPT
#2 the NTP socket is in use, exiting

# ntpdate -v 0.centos.pool.ntp.org
5 Apr 23:35:14 ntpdate[7475]: ntpdate 4.2.6p5@1.2349-o Wed Jul 12 12:22:59 UTC 2017 (1)
5 Apr 23:35:14 ntpdate[7475]: the NTP socket is in use, exiting

原因:本地已经运行了NTP服务。NTP服务运行使使用了123端口,而ntpdate发送查询数据包时默认的源端口也是123。
系统:Ubuntu 14.04.5 LTS
解决:有两种解决方法: 1)停止本地服务即可。执行service ntp stop命令来停止服务,或者使用其他方法来停止。 2)使用ntpdate的-u选项,在发送数据包时使用非特权端口(>1024)。[……]

READ MORE

「Linux」- 时区设置

本笔记将记录常见 Linux 发行的时区查看与设置方法。
CentOS 6.x(通用方法)
Setup Timezone and NTP on CentOS 6 How to check which timezone in Linux?
查看当前时区,如下输出表示当前市区为 Asia/Shanghai:

# date ‘+%Z %z’
CST +0800

设置时区:

ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime[……]

READ MORE

「Linux」- 同步系统时间(基于 systemd 管理)

问题描述
很早之前,系统时间同步是通过 NTP 服务并配合 Cron(定时任务来完成的)。
现在,在较新的 Linux 发行版中,如果操作系统通过 systemd 进行服务管理,那多半也使用 systemd 提供的工具进行系统时间管理。
解决方案
systemd-timesyncd
systemd-timesyncd – ArchWiki
实现 SNTP 客户端,能够进行系统时间的管理 不提供 NTP Server 功能,
查看当前时钟设置(查看)
通常使用 timedatectl 设置操作系统时间。
使用 timedatectl 查看时间同步状态:

# timedatectl
Local time: Mon 2020-05-25 10:58:29 EDT
Universal time: Mon 2020-05-25 14:58:29 UTC
RTC time: Mon 2020-05-25 14:58:28
Time zone: America/New_York (EDT, -0400)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
Sun 2020-03-08 01:59:59 EST
Sun 2020-03-08 03:00:00 EDT
Next DST change: DST ends (the clock jumps one hour backwards) at
Sun 2020-11-01 01:59:59 EDT
Sun 2020-11-01 01:00:00 EST

选择并设置时区(时区)
时区信息由 NANA 维护,但是时区采用地区命名,即使用在时区中的某个地区代表这个时区。这带来的问题的就是我们不知道使用哪个地区名,比如到底是 Asia/Beijing 还是 Asia/Shanghai 代表我们的时区。使用 tzselect 命令,根据提示进行选择,最后可以确定要使用的时区。该命令用于确定时区,但是不会修改时区。
使用 timedatectl set-timezone 设置:

timedatectl set-timezone Asiz/Shanghai

直接修改时间信息(时间)
使用 timedatectl set-time 设置,时[……]

READ MORE

「Linux」- 系统时间同步,杂记

# Failed to set time: Automatic time synchronization is enabled
Linux Set Date and Time From a Command Prompt
系统打开了时间自动同步,关闭即可:timedatectl set-ntp 0[……]

READ MORE

「Linux」- 使用 systemd-timesyncd 同步时间

systemd-timesyncd
内建于 systemd 的、用于时间同步的、轻量级的组件。
注意事项,该组件只能用于时间同步,而不能像 ntpd 一样作为时间服务器。
实验环境概述
操作系统:Debian 10 软件版本:systemd 241 (241)
该操作不适用与 CentOS 7.x 系列: 根据 Systemd TimeSyncd 与 CentOS 7.2 Minimal Time synchronization timedated and/or ntpd/chrony 的描述,RedHat 在编译 systemd 时,移除 systemd-timesyncd 特征。对于 CentOS 7.x 系列,建议使用 chrony 服务。
第一步、卸载其他时间服务
根据 systemd-timesyncd.service 定义,当存在 ntpd、chronyd 等等服务时,无法启动 systemd-timesyncd 服务。可以使用 systemctl cat systemd-timesyncd.service 命令查看 [Unit] 部分来了解。
因此我们需要卸载其他时间服务,卸载方法这里不再展开,这里只列举几种需要卸载的时间服务:

# Debian 10
apt-get purge chrony

第二步、启用 systemd-timesyncd 服务

systemctl start systemd-timesyncd
systemctl enable systemd-timesyncd

# 查看服务状态
systemctl status systemd-timesyncd

如果不卸载其他时间服务程序,命令 systemctl status systemd-timesyncd 将返回 condition failed 错误信息。
第三步、查看时间同步状态
使用 timedatectl 查看时间同步状态:

# timedatectl
Local time: Mon 2020-05-25 16:19:29 CST
Universal time: Mon 2020-05-25 08:19:29 UTC
RTC time: Mon 2020-05-25 08:19:29
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in l[……]

READ MORE