systemctl
是 Systemd 系统和服务管理器的一部分,用于管理系统上的服务、守护进程、挂载点、自动挂载等。下面是一些常见的 systemctl
命令及其用途:
常见命令
-
查看状态
-
systemctl list-unit-files
:- 列出所有已知的单元文件(服务、设备、挂载点等)。
-
systemctl status <service>
:- 显示指定服务的状态信息。
- 例如:
systemctl status nginx
-
systemctl is-active <service>
:- 显示指定服务是否处于活动状态。
- 例如:
systemctl is-active nginx
-
systemctl cat <service>
:- 显示指定服务的配置文件内容。
- 例如:
systemctl cat nginx
-
-
启动/停止/重启服务
-
systemctl start <service>
:- 启动指定服务。
- 例如:
systemctl start nginx
-
systemctl stop <service>
:- 停止指定服务。
- 例如:
systemctl stop nginx
-
systemctl restart <service>
:- 重启指定服务。
- 例如:
systemctl restart nginx
-
systemctl reload <service>
:- 重载指定服务的配置而不重启服务。
- 例如:
systemctl reload nginx
-
systemctl reload-or-restart <service>
:- 尝试重载服务的配置,如果重载失败,则重启服务。
- 例如:
systemctl reload-or-restart nginx
-
systemctl try-restart <service>
:- 尝试立即重启服务,如果服务当前未运行,则启动服务。
- 例如:
systemctl try-restart nginx
-
-
设置服务开机自启
-
systemctl enable <service>
:- 设置服务开机自启。
- 例如:
systemctl enable nginx
-
systemctl disable <service>
:- 取消服务开机自启。
- 例如:
systemctl disable nginx
-
systemctl is-enabled <service>
:- 检查服务是否设置为开机自启。
- 例如:
systemctl is-enabled nginx
-
-
控制机器
-
systemctl poweroff
:- 关机。
- 例如:
systemctl poweroff
-
systemctl reboot
:- 重启机器。
- 例如:
systemctl reboot
-
systemctl isolate multi-user.target
:- 切换到多用户模式。
- 例如:
systemctl isolate multi-user.target
-
-
查询和管理挂载点
-
systemctl list-mounts
:- 列出所有挂载点。
- 例如:
systemctl list-mounts
-
systemctl mount <mount-point>
:- 挂载指定挂载点。
- 例如:
systemctl mount /mnt/some_mount_point
-
systemctl unmount <mount-point>
:- 卸载指定挂载点。
- 例如:
systemctl unmount /mnt/some_mount_point
-
-
管理自动挂载
-
systemctl enable-automount <automount-point>
:- 启用自动挂载。
- 例如:
systemctl enable-automount /mnt/some_automount_point
-
systemctl disable-automount <automount-point>
:- 禁用自动挂载。
- 例如:
systemctl disable-automount /mnt/some_automount_point
-
-
管理定时任务
-
systemctl enable --now <timer-unit>.timer
:- 启用并立即运行定时任务。
- 例如:
systemctl enable --now cleanup.timer
-
systemctl disable <timer-unit>.timer
:- 禁用定时任务。
- 例如:
systemctl disable cleanup.timer
-
-
日志管理
-
journalctl -u <service>
:- 查看指定服务的日志。
- 例如:
journalctl -u nginx
-
journalctl --since "2023-01-01"
:- 查看从某个日期开始的所有日志。
- 例如:
journalctl --since "2023-01-01"
-
示例
以下是一些具体的示例命令:
-
启动服务:
systemctl start nginx
-
停止服务:
systemctl stop nginx
-
重启服务:
systemctl restart nginx
-
检查服务状态:
systemctl status nginx
-
设置服务开机自启:
systemctl enable nginx
-
取消服务开机自启:
systemctl disable nginx
总结
systemctl
是 Linux 系统中非常重要的工具之一,用于管理和控制各种服务、挂载点、自动挂载以及定时任务。