Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 adopted 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 system administration. 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.
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, replacing "username" with your own username and "remote.server.com" with the hostname or IP address of the remote server
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 correct password, you will gain access to the remote server and be presented with the command terminal prompt for that particular server.
Step 2: Check the Status of a Service
Once you establish a connection with the remote server, it's essential to check the status of any service using the systemctl status command. To know the status of any service, use this command
sudo systemctl status apache2
The command sudo systemctl status apache2 will provide detailed information about 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, replace "service_name" with the actual name of the service. For example, to initiate the Apache web server on a remote server, use this command
sudo systemctl start apache2
After executing the command, you'll be prompted for your password
[sudo] password for username:
If the password is correct, the service will start successfully. No output typically means the command executed without errors.
Step 4: Stop a Service
If a service is running and you need to stop it, you can use the systemctl stop command. The syntax is as follows
sudo systemctl stop apache2
Output may look like this
[sudo] password for user:
After entering the password, the service will stop. Like the start command, no additional output usually indicates successful execution.
Step 5: Restart a Service
If you've made changes to a service configuration and need to restart it for those changes to take effect, use the systemctl restart command
sudo systemctl restart apache2
The terminal will request the user's password when using the sudo command
[sudo] password for user:
This command stops and then starts the service, ensuring that any configuration changes are applied.
Step 6: Enable a Service
If you want a service to start automatically when the system boots up, use the systemctl enable command
sudo systemctl 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.
If the service is already enabled, you may see a message indicating synchronization with existing configurations. The systemctl enable command ensures that the service will start automatically during system boot.
Step 7: Disable a Service
To prevent a service from starting automatically at boot time, use the systemctl disable command
sudo systemctl disable apache2
This command removes the symbolic links that enable automatic startup, effectively disabling the service from starting at boot.
Step 8: Check Service Logs
If a service is not working properly or you're experiencing issues with it, use the journalctl command to check its logs
sudo journalctl -u apache2
Output for the 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.
You can also use additional flags like -f to follow logs in real-time or --since to view logs from a specific time period.
Common Systemctl Commands Summary
| Command | Purpose | Example |
|---|---|---|
| systemctl status | Check service status | sudo systemctl status apache2 |
| systemctl start | Start a service | sudo systemctl start apache2 |
| systemctl stop | Stop a service | sudo systemctl stop apache2 |
| systemctl restart | Restart a service | sudo systemctl restart apache2 |
| systemctl enable | Enable automatic startup | sudo systemctl enable apache2 |
| systemctl disable | Disable automatic startup | sudo systemctl disable apache2 |
| journalctl -u | View service logs | sudo journalctl -u apache2 |
Step 9: 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
We have learned how to manage systemd services on a remote Linux server using essential commands like systemctl status, systemctl start, systemctl stop, systemctl restart, systemctl enable, and systemctl disable. These commands allow you to inspect service status, control service states, and manage automatic startup behavior. Mastering systemd service management on remote Linux servers is an essential skill for effective system administration and ensuring reliable service operation.
