CentOS 7 上systemctl 的用法
我们对service和chkconfig两个命令都不陌生,systemctl 是管制服务的主要工具, 它整合了chkconfig 与 service功能于一体。
systemctl is-enabled iptables.service
systemctl is-enabled servicename.service #查询服务是否开机启动
systemctl enable *.service #开机运行服务
systemctl disable *.service #取消开机运行
systemctl start *.service #启动服务
systemctl stop *.service #停止服务
systemctl restart *.service #重启服务
systemctl reload *.service #重新加载服务配置文件
systemctl status *.service #查询服务运行状态
systemctl --failed #显示启动失败的服务
注:*代表某个服务的名字,如http的服务名为httpd
例如在CentOS 7 上安装http
[root@CentOS7 ~]# yum -y install httpd
启动服务(等同于service httpd start)
systemctl start httpd.service
停止服务(等同于service httpd stop)
systemctl stop httpd.service
重启服务(等同于service httpd restart)
systemctl restart httpd.service
查看服务是否运行(等同于service httpd status)
systemctl status httpd.service
开机自启动服务(等同于chkconfig httpd on)
systemctl enable httpd.service
开机时禁用服务(等同于chkconfig httpd on)
systemctl disable httpd.service
查看服务是否开机启动 (等同于chkconfig --list)
注意:
原服务都安装在/usr/lib/systemd/system/目录下,当我们使用systemctl添加一项服务开机启动的时候,linux帮我们在/etc/systemd/system/multi-user.target.wants/目录下面创建了对应服务的软连接,所以开机启动,当我们设置服务开机不启动,软连接会从/etc/systemd/system/multi-user.target.wants/下面移除对应的软连接:
(1)查看/usr/lib/systemd/system/ 系统原有的所有服务(有好多服务)
[root@VM_0_12_centos ~]# ls /usr/lib/systemd/system/
acpid.service
arp-ethers.service
atd.service
auditd.service
autovt@.service
basic.target
basic.target.wants
blk-availability.service
bluetooth.target
brandbot.path
brandbot.service
chrony-dnssrv@.service
chrony-dnssrv@.timer
..........
(2)查看mysqld服务并且到/etc/systemd/system/multi-user.target.wants/目录下查看:
[root@VM_0_12_centos ~]# ls /usr/lib/systemd/system |grep mysqld #原服务目录下存在mysqld服务
mysqld.service
[root@VM_0_12_centos ~]# ls /etc/systemd/system/multi-user.target.wants/ |grep
mysql #开机启动目录下没有mysqld服务
[root@VM_0_12_centos ~]# systemctl is-enabled mysqld.service #mysqld开机不启动
disabled
(3)设置mysql服务开机启动并查看目录
[root@VM_0_12_centos ~]# systemctl enable mysqld.service #设置mysql服务开机启动
Created symlink from /etc/systemd/system/mysql.service to /usr/lib/systemd/syste
m/mysqld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service
to /usr/lib/systemd/system/mysqld.service.
[root@VM_0_12_centos ~]# ll /etc/systemd/system/multi-user.target.wants/ |grep
mysql #查看开机启动目录下mysqld.service的软连接
lrwxrwxrwx 1 root root 38 Apr 6 15:37 mysqld.service -> /usr/lib/systemd/syste
m/mysqld.service
[root@VM_0_12_centos ~]# ll /usr/lib/systemd/system |grep mysqld #原服务目录下查看文件
-rw-r--r-- 1 root root 1073 Dec 9 16:11 mysqld.service
一个正常的服务器最少需要的服务:
httpd(apache http)
iptables.service
mysql.service
nginx.service
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了