Linux 服务控制与运行级别
程序员文章站
2022-05-23 10:58:41
如何控制服务的运行状态?如何切换不同的运行级别? 服务控制 ntsysv 仿图形交互界面,集中配置各种服务启动状态 :同时对指定运行级别中的服务进行管理,不加仅管理当前运行级别中的服务。 systemctl systemctl {选项} name.servive :启动 :停止 :重启,先停止,再启 ......
如何控制服务的运行状态?如何切换不同的运行级别?
服务控制
ntsysv
- 仿图形交互界面,集中配置各种服务启动状态
--level 35
:同时对指定运行级别中的服务进行管理,不加仅管理当前运行级别中的服务。
[root@localhost ~]# ntsysv --level 35
systemctl
- systemctl {选项} name.servive
start
:启动stop
:停止
[root@localhost ~]# systemctl start httpd.service [root@localhost ~]# systemctl stop httpd.service
restart
:重启,先停止,再启动reload
:重新加载配置文件,不会完全停止和启动服务
[root@localhost ~]# systemctl restart httpd.service [root@localhost ~]# systemctl reload httpd.service
status
:状态is-active
:是否启动成功is-failed
:是否启动失败
[root@localhost ~]# systemctl status httpd.service ● httpd.service - the apache http server loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) active: active (running) since tue 2019-08-27 17:31:39 cst; 40s ago docs: man:httpd(8) man:apachectl(8) process: 8833 execreload=/usr/sbin/httpd $options -k graceful (code=exited, status=0/success) main pid: 8821 (httpd) status: "total requests: 0; current requests/sec: 0; current traffic: 0 b/sec" cgroup: /system.slice/httpd.service ├─8821 /usr/sbin/httpd -dforeground ├─8835 /usr/sbin/httpd -dforeground ├─8836 /usr/sbin/httpd -dforeground ├─8837 /usr/sbin/httpd -dforeground ├─8838 /usr/sbin/httpd -dforeground └─8839 /usr/sbin/httpd -dforeground aug 27 17:31:39 localhost.localdomain systemd[1]: starting the apache http server... aug 27 17:31:39 localhost.localdomain httpd[8821]: ah00558: httpd: could not reliably determine the server's fully qualified domain name, using localhost.l...s message aug 27 17:31:39 localhost.localdomain systemd[1]: started the apache http server. aug 27 17:31:45 localhost.localdomain httpd[8833]: ah00558: httpd: could not reliably determine the server's fully qualified domain name, using localhost.l...s message aug 27 17:31:45 localhost.localdomain systemd[1]: reloaded the apache http server. hint: some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl is-active httpd.service active [root@localhost ~]# systemctl is-failed httpd.service active
enable
:开机自启disable
:关闭开机自启is-enabled
:是否开机自启
[root@localhost ~]# systemctl enable httpd.service created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. [root@localhost ~]# systemctl disable httpd.service removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service. [root@localhost ~]# systemctl is-enabled httpd.service disabled
list-dependencies
:查看依赖关系
[root@localhost ~]# systemctl list-dependencies httpd.service httpd.service ● ├─-.mount ● ├─system.slice ● └─basic.target ● ├─microcode.service ● ├─rhel-autorelabel-mark.service ● ├─rhel-autorelabel.service ● ├─rhel-configure.service ● ├─rhel-dmesg.service ● ├─rhel-loadmodules.service ● ├─selinux-policy-migrate-local-changes@targeted.service ● ├─paths.target ● ├─slices.target ● │ ├─-.slice ● │ └─system.slice ● ├─sockets.target ● │ ├─dbus.socket ● │ ├─dm-event.socket ● │ ├─systemd-initctl.socket ● │ ├─systemd-journald.socket ● │ ├─systemd-shutdownd.socket ● │ ├─systemd-udevd-control.socket ● │ └─systemd-udevd-kernel.socket ● ├─sysinit.target ● │ ├─dev-hugepages.mount ● │ ├─dev-mqueue.mount ● │ ├─kmod-static-nodes.service ● │ ├─lvm2-lvmetad.socket ● │ ├─lvm2-lvmpolld.socket ● │ ├─lvm2-monitor.service ● │ ├─plymouth-read-write.service ● │ ├─plymouth-start.service ● │ ├─proc-sys-fs-binfmt_misc.automount ● │ ├─sys-fs-fuse-connections.mount ● │ ├─sys-kernel-config.mount ● │ ├─sys-kernel-debug.mount ● │ ├─systemd-ask-password-console.path ● │ ├─systemd-binfmt.service ● │ ├─systemd-firstboot.service ● │ ├─systemd-hwdb-update.service ● │ ├─systemd-journal-catalog-update.service ● │ ├─systemd-journal-flush.service ● │ ├─systemd-journald.service ● │ ├─systemd-machine-id-commit.service ● │ ├─systemd-modules-load.service ● │ ├─systemd-random-seed.service ● │ ├─systemd-sysctl.service ● │ ├─systemd-tmpfiles-setup-dev.service ● │ ├─systemd-tmpfiles-setup.service ● │ ├─systemd-udev-trigger.service ● │ ├─systemd-udevd.service ● │ ├─systemd-update-done.service ● │ ├─systemd-update-utmp.service ● │ ├─systemd-vconsole-setup.service ● │ ├─cryptsetup.target ● │ ├─local-fs.target ● │ │ ├─-.mount ● │ │ ├─boot.mount ● │ │ ├─rhel-import-state.service ● │ │ ├─rhel-readonly.service ● │ │ └─systemd-remount-fs.service ● │ └─swap.target ● │ └─dev-mapper-centos\x2dswap.swap ● └─timers.target ● └─systemd-tmpfiles-clean.timer
mask
:屏蔽服务unmak
:取消屏蔽
- 主要使用于相互冲突的服务,屏蔽后,不会被其他的
systemd
单元启动,也无法被手动启动。例如iptables
和firewalld
就是互相冲突的。
[root@localhost ~]# systemctl mask firewalld.service created symlink from /etc/systemd/system/firewalld.service to /dev/null. [root@localhost ~]# systemctl unmask firewalld.service removed symlink /etc/systemd/system/firewalld.service.
运行级别
runlevel
- 显示系统的运行级别
[root@localhost ~]# runlevel n 5
切换多用户模式
两种方法:
[root@localhost ~]# init 3
[root@localhost ~]# systemctl isolate multi-user.target
切换图形化界面
两种方法:
[root@localhost ~]# init 5
[root@localhost ~]# systemctl isolate graphical.target
开机默认 target
1.查看默认启动target
[root@localhost ~]# systemctl get-default graphical.target
2.切换默认启动target
,两种方法:
[root@localhost ~]# systemctl set-default multi-user.target removed symlink /etc/systemd/system/default.target. created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
[root@localhost ~]# ln -sf /lib/systemd/system/multi-user.target /etc/systemd/system/default.target
3.查看默认启动target
[root@localhost ~]# systemctl get-default multi-user.target
推荐阅读
-
Linux编程 14 文件权限(用户列表passwd,用户控制shadow,useradd模板与useradd命令参数介绍)
-
radmin 远程控制软件使用图文教程(服务器端配置与控制台连接)
-
在PHP中运行Linux命令并启动SSH服务的例子
-
Linux服务器漏洞防护CVE级别 可SUDO提权到管理员
-
阿里云服务器怎么远程控制(linux开远程命令)
-
查看linux ssh服务信息及运行状态方法
-
在linux服务器下使用版本控制软件SVN的方法
-
阿里云服务器怎么远程控制(linux开远程命令)
-
Linux server配置安装Java与Tomcat服务器教程详解
-
Linux下安装Memcached服务器和客户端与PHP使用示例