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 Linux system Service systemctl

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

Share

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

This article mainly shows you "Linux system service systemctl how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Linux system service systemctl how to use" 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.

1. Run-level classification runlevel

> run level 0: system downtime status

> run level 1: single user working status, root permission, used for system maintenance, no remote login

> runlevel 2: multi-user status (no NFS)

> runlevel 3: full multiuser status (with NFS)

> runlevel 4: the system is not in use, reserved

Runlevel 5:X11 console

> runlevel 6: the system shuts down and restarts normally

2. Manage the service systemctl (root permission)

> systemctl [opt] xxx.service

Status to view the current service status

Start starts the service

Stop shuts down the service

Restart restart the service

> set enable to boot

> set disable to disable boot.

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

Mask logout service

Unmask cancels logout

3. 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

Some directories related to 4.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

5. 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. The file content is as follows

[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.

6. Let's give another example of doing 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/bash logdate=$ (date +% s) logdir= "/ root/log/" logname=fancy.$ {logdate} .log # echo $logname touch ${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=simple ExecStart=/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 all the contents of the article "how to use systemctl in Linux system Service". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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

Servers

Wechat

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

12
Report