[转帖]Linux systemd 常用命令

系统管理 systemctl

显示系统状态:
$ systemctl status
立即激活单元:
# systemctl start [单元]
立即停止单元:
# systemctl stop [单元]
重启单元:
# systemctl restart [单元]
开机自动激活单元:
# systemctl enable [单元]
取消开机自动激活单元:
# systemctl disable [单元]
命令单元重新读取配置:
# systemctl reload [单元]
输出单元运行状态:
$ systemctl status [单元]
检查单元是否配置为自动启动:
$ systemctl is-enabled [单元]
显示单元的手册页:
# systemctl help [单元]
重新载入 systemd,扫描新的或有变动的单元:
# systemctl daemon-reload
输出激活的单元:
$ systemctl list-units
查看已激活的服务:
$ systemctl list-units -t service
输出运行失败的单元:
$ systemctl --failed
禁用一个单元:
# systemctl mask [单元]
取消禁用一个单元:
# systemctl unmask [单元]
查看所有已安装服务:
$ systemctl list-unit-files
检查某个服务的所有配置细节
# systemctl show [服务]
获取某个服务的依赖性列表
# systemctl list-dependencies [服务]
查看环境变量
$ systemctl show-environment
重启系统
# systemctl reboot
关闭系统
# systemctl poweroff
进入待机状态
# systemctl suspend
进入休眠状态
# systemctl hibernate
进入混合休眠模式
# systemctl hybrid-sleep
进入救援状态
# systemctl rescue

  

查看启动耗时 systemd-analyze

查看启动耗时
$ systemd-analyze
查看每个服务的启动耗时
$ systemd-analyze blame
显示瀑布状的启动过程流
$ systemd-analyze critical-chain
显示指定服务的启动流
$ systemd-analyze critical-chain [服务]

查看当前主机信息 hostnamectl

显示当前主机的信息
$ hostnamectl
设置主机名。
# hostnamectl set-hostname [hostname]

本地化设置 localectl

查看本地化设置
$ localectl
设置本地化参数。
# localectl set-locale LANG=en_US.UTF-8
# localectl set-keymap en_US

时区设置 timedatectl

查看当前时区设置
$ timedatectl
列出所有可用时区
$ timedatectl list-timezones
设置当前时区
# timedatectl set-timezone Asia/Shanghai
# timedatectl set-time YYYY-MM-DD
# timedatectl set-time HH:MM:SS
设置硬件时间为本地时间
# timedatectl set-local-rtc true

查看用户登录信息 loginctl

列出当前所有会话
$ loginctl list-sessions
列出当前登录用户
$ loginctl list-users
查看指定用户信息
$ loginctl show-user [username]

日志分析 journalctl

查看最近生成的5条日志
# journalctl -n 5
查看系统报错
# journalctl -p err
查看某个时间段生成的日志
# journalctl --since 时间("2017-08-01 00:00:00") --until ("2017-08-02 02:30:00")