How to Control Systemd Services on Remote Linux Server?


As Linux users and administrators, we are responsible for managing services on remote Linux servers, which could include web servers like Apache or Nginx, or database servers like MySQL or PostgreSQL. To effectively manage these services, we need to have a strong understanding of the underlying system and service manager for Linux - systemd.

Systemd is a robust and feature-rich system and service manager designed for Linux-based operating systems. It takes care of managing the initialization and termination of services, tracks system processes, and oversees system resources. It is widely acquired as the default system and service manager in many modern Linux distributions, such as Ubuntu, Fedora, CentOS, and Debian. Controlling systemd services on a remote Linux server is critical for you. This involves tasks such as starting, stopping, restarting, enabling, disabling, and monitoring system services. While there are various methods for managing systemd services, using commands like systemctl, journalctl, and others is the most efficient and reliable way.

This article offers a comprehensive tutorial on utilizing systemd commands to manage services on a remote Linux server. We will cover commands such as starting and stopping services, inspecting their status, enabling and disabling services during boot, and reviewing system logs. Each command will be accompanied by its output, with corresponding examples to enhance comprehension.

Step 1: Connect to the Remote Server

To connect to the remote server using SSH, open a terminal on your local machine and type the following command after that replacing "username" with your own username and "remote.server.com" with the hostname or IP address of the remote server use this command 

ssh username@remote.server.com

Output may look like this −

The authenticity of host 'remote.server.com (192.168.1.1)' can't be established.
RSA key fingerprint is 11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:00.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'remote.server.com,192.168.1.1' (RSA) to the list of known hosts.
username@remote.server.com's password:

Upon successful authentication with the right password, you will able to access the remote server and be displayed with the command terminal or prompt for that particular server.

Step 2: Check the Status of a Service

Once you will establish a connection with the remote server then it's required to check the status of any service using the systemctl status command in your system. To know the status of any service then simply you need to write this command in the Linux terminal or prompt 

sudo systemctl status apache2

The command "sudo systemctl status apache2" will give you brief details on the current status of the Apache service.

apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-03-15 14:30:00 UTC; 5min ago
   Main PID: 12345 (apache2)
      Tasks: 55 (limit: 2288)
     Memory: 67.0M
     CGroup: /system.slice/apache2.service
             ├─12345 /usr/sbin/apache2 -k start
             ├─12346 /usr/sbin/apache2 -k start
             └─12347 /usr/sbin/apache2 -k start
Mar 15 14:30:00 example.com systemd[1]: Started The Apache HTTP Server.

Step 3: Start a Service

To start a specific service then replace "service_name" with the actual name of the service. As an illustration, if you aim to initiate the Apache web server on a remote server, you can write this input command below −

sudo systemctl start apache2

After executing the command, the Apache service will be initiated on the remote server and the corresponding output will be displayed.

[sudo] password for <username>: 

Note: This is a prompt for your password. Type your password and press enter. If the password is correct, the output may look like this 

Job for apache2.service started successfully

Step 4: Stop a Service

If a service is running and you need to stop it, you can use the systemctl stop command to do so. The syntax for this stop command is as follows −

sudo systemctl stop apache2

Output may look like this −

[sudo] password for user: 
[user@hostname ~]$ sudo systemctl stop apache2
[user@hostname ~]$

This will stop the Apache service on the remote server.

Step 5: Restart a Service

If you've made changes to a service and need to restart it for those changes to take effect then you can use the systemctl restart command to do so. The syntax for this input command is as follows 

sudo systemctl restart apache2

It will allow you to enter the password for that user −

[sudo] password for user: 

The terminal requests the user's password when using the "sudo" command to run the "systemctl" command with elevated privileges as the root user. This is necessary to restart the Apache web server, which requires administrative access. The output will look like this −

Restarting Apache2 web server... 

This blank line indicates that the Apache web server is being restarted.


This line is an empty line that separates the output from the command prompt.

Note that the actual output may vary depending on the specific Linux distribution and version being used. Additionally, if there are any errors or issues with restarting the Apache web server, additional output may be displayed to indicate what went wrong.

Step 6: Enable a Service

If you want a service to start automatically when the system boots up, you can use the systemctl enable command to enable it. The syntax for this input command is as follows 

sudo systemctl enable apache2

The output of this command will depend on the current state of the Apache2 service. If the service is already enabled, you may see a message like this −

Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.Executing: /lib/systemd/systemd-sysv-install enable apache2

If the service is not yet enabled, the output may look like this −

Created symlink /etc/systemd/system/multi-user.target.wants/apache2.service → /lib/systemd/system/apache2.service.

Either way, the systemctl enable command ensures that the Apache2 service will start automatically when the system boots up.

Step 7: Check Service Logs

If a service is not working properly or you're experiencing issues with it, you can use the journalctl command to check its logs.

Here is an example of an input command −

sudo journalctl -u apache2

Output for journalctl command −

-- Logs begin at Mon 2023-03-13 00:00:00 UTC, end at Mon 2023-03-13 23:59:59 UTC. --
Mar 13 08:30:01 server systemd[1]: Starting The Apache HTTP Server...
Mar 13 08:30:01 server apachectl[12345]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
Mar 13 08:30:01 server systemd[1]: Started The Apache HTTP Server.
Mar 13 08:30:01 server systemd[1]: apache2.service: Main process exited, code=exited, status=0/SUCCESS
Mar 13 08:30:01 server systemd[1]: apache2.service: Succeeded.

Step 8: Exit the Remote Server

Once you've completed your tasks on the remote server, you can exit the SSH session by typing the following command −

exit

Here's an example of the terminal output 

me@local-machine:~$ ssh remote-server
Welcome to remote-server!
me@remote-server:~$ ls
file1.txt  file2.txt  file3.txt
me@remote-server:~$ exit
logout
Connection to remote-server closed.
me@local-machine:~$

This will close the SSH connection and return you to your local machine's command prompt.

Conclusion

In summary, we learned how to manage systemd services on a remote Linux server using commands like systemctl status, systemctl start, systemctl stop, systemctl enable, and systemctl disable and many more. These commands help you to inspect the status of services, start and stop them, and enable/disable automatic startup at boot time.

By following these steps, we can efficiently manage systemd services on a remote Linux server and ensure their smooth operation. Additionally, automating service startup and shutdown ensures their constant availability. Overall, mastering the ability to control systemd services on a remote Linux server is an essential skill for any Linux system user.

Updated on: 27-Jul-2023

260 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements