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 in Linux

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

Share

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

This article is about how to use systemctl in Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Systemd is the latest initialization system (init) of the Linux system. Its function is to improve the startup speed of the system, start as few processes as possible, and start as many processes concurrently as possible.

1. Classification of runlevels: runlevel runlevel 0: system downtime state runlevel 1: single user working status, root permissions, for system maintenance, disable remote login runlevel 2: multi-user status (no NFS) runlevel 3: full multiuser state (with NFS) runlevel 4: system is not in use Keep the running level 5:X11 console runlevel 6: the system shuts down and restarts normally 2. Management service systemctl (root permissions) systemctl [opt] xxx.servicestatus view the current service status start startup service stop shutdown service restart restart service enable setting boot disable setting boot reload does not follow the specific service name, reload the configuration file mask logout service unmask cancel logout III. Some commonly used commands systemctl to view currently started services systemctl list-units to see all services systemctl list-unit-files to see which services depend on systemctl list-dependencies xx.service to see what services depend on (reverse) systemctl list-dependencies-reverse xx.service IV. some directories related to system services (Centos environment Debian class environment may be slightly different) / usr/lib/systemd/system/ system installation software default startup script directory / etc/systemd/system/ users according to their needs to establish the startup script directory / etc/sysconfig/ service initialization options directory / var/lib/ service runtime generated data storage directory / etc/xxx/ each service configuration directory V. 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 daemonDocumentation=man:sshd (8) man:sshd_config (5) After=network.target sshd-keygen.serviceWants=sshd- keygen.service[ service] Type=notifyEnvironmentFile=/etc/sysconfig/sshdExecStart=/usr/sbin/sshd-D $OPTIONSExecReload=/bin/kill-HUP $MAINPIDKillMode=processRestart=on-failureRestartSec=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, there is no problem to store them in the system service directory / usr/lib/systemd/system/. 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, executing only a few commands, so there is no configuration file, and it will not be resident in memory. Thank you for reading it once. This is the end of the article on "how to use systemctl in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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