Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use systemctl to manage Services in Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you the management of how to use systemctl in Linux. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Services: processes that reside in system memory and can provide some system and network functions; now the latest Linux systems all use systemctl for service management.

First, the classification of run-level runlevel

Runlevel 0: system downtime statu

Runlevel 1: single user working status, root permissions, for system maintenance, no remote login

Runlevel 2: multi-user status (no NFS)

Runlevel 3: full multiuser state (with NFS)

Runlevel 4: system not in use, reserved

Run-level 5:X11 console

Runlevel 6: system shuts down and restarts normally

2. Manage service systemctl (root permission)

Systemctl [opt] xxx.service

Status to view current service status

Start Startup Service

Stop shuts down the service

Restart restart service

Enable setting boot up

Disable setting does not start up.

Reload the configuration file without the specific service name after the reload

Mask logout service

Unmask cancel logout

three。 Some common commands systemctl

View the currently started service systemctl list-units

View all service systemctl list-unit-files

See if the service is dependent on systemctl list-dependencies xx.service

See what the service depends on (reverse) systemctl list-dependencies-reverse xx.service

IV. Some directories related to system services (Centos environment, Debian class environment may be slightly different)

Default startup script directory for software installed on / usr/lib/systemd/system/ system

/ etc/systemd/system/ users set up the startup script directory according to their own needs

/ etc/sysconfig/ service initialization options directory

Data storage directory generated by / var/lib/ service runtime

/ etc/xxx/ each service configuration directory

five。 Combined with an example, two ssh services are enabled on one machine.

Our most commonly used ssh service, the system default ssh service port 22, I now want to open another ssh service, port 8888

1. The system service startup script / usr/lib/systemd/system/sshd.service, copy it to / etc/systemd/system/, and rename it to sshd2.service, with the following file content:

[Unit] Description=OpenSSH server daemon Documentation=man:sshd (8) man:sshd_config (5) After=network.target sshd-keygen.service Wants=sshd-keygen.service [Service] Type=notify EnvironmentFile=/etc/sysconfig/sshd ExecStart=/usr/sbin/sshd-D $OPTIONS ExecReload=/bin/kill-HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target

Because a new service is to be restarted, modify the ExecStart line, read the new configuration file sshd2_config, and change it to

ExecStart=/usr/sbin/sshd-D $OPTIONS-f / etc/ssh/sshd2_config

two。 Under / etc/ssh/, copy the sshd_config to sshd2_config and modify the port line

Port 8888

3. Run the command systemctl reload to reload the configuration

4. Run the command systemctl status sshd2.service to check the status

5 run the command systemctl start sshd2.service to start the service

6. Run the command systemctl enable sshd2.service to set boot up

7. Log in to ssh fancy@ip-p8888 on another machine and you can log in

Note 1, the firewall needs to open port 8888

Note 2. It is officially recommended that users store their newly created service scripts in the / etc/systemd/system/ directory. In fact, it is no problem to store them in the system service directory / usr/lib/systemd/system/. It depends on the individual's choice.

Let's give another example to do our own service.

1. Create a shell script fancy_test.sh under / root/bin/ and modify its permissions, chmod uplix fancy_test.sh, as follows

#! / bin/bashlogdate=$ (date +% s) logdir= "/ root/log/" logname=fancy.$ {logdate} .log # echo $lognametouch ${logdir} ${logname}

This means that when you run the service, create a log file in the / root/log/ directory

two。 Create the startup script fancy_test.service under / etc/systemd/system/ and enter the following

[Unit] Description=fancy_test server daemon [Service] Type=simpleExecStart=/root/bin/fancy_test.sh [Install] WantedBy=multi-user.target

3. Run the command systemctl reload

4. Run the command systemctl start fancy_test.service

5. At this point you will see that a log file has been created in the / root/log/ directory

Note that this is the simplest service, which only executes a few commands, so there is no configuration file, and it will not be resident in memory.

The above is how to use systemctl to manage services in Linux shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report