Service manipulation involves managing services on a system, including starting, stopping, enabling, and disabling them. Services are background processes that perform specific functions, such as web servers, databases, or network services. Let’s explore how we can manipulate services using various commands:
systemd Services
Systemd is the default service manager for most modern Linux distributions. It controls the startup process and manages system services.
- Starting and Stopping Services:
- To start a service named
httpd
, you would use:
service httpd status - To stop the same service:
service httpd status
- Enabling and Disabling Services:
- To enable the
sshd
service to start automatically at boot:
service sshd status - To disable the same service from starting automatically at boot:
service sshd status
- Restarting and Reloading Services:
- To restart the
nginx
service:
service nginx status - To reload configuration changes for the
apache2
service:
service apache2 status
SysVinit Services
SysVinit is an older init system used in some Linux distributions. While systemd has largely replaced it, some systems may still use SysVinit.
- Starting and Stopping Services:
- To start the
mysql
service:
service mysql status - To stop the
mysql
service:
service mysql status
- Enabling and Disabling Services:
- To enable the
ntp
service to start automatically at boot:
service ntp status - To disable the same service from starting automatically at boot:
service ntp status
- Checking Service Status:
- To check the status of the
cron
service:
service cron status
Practical Examples
- Managing Apache Web Server:
- To check the status of the Apache service:
service apache2 status
- Restarting SSH Service:
- To restart the SSH service:
service ssh restart
- Checking Status of Nginx Service:
- To check the status of the Nginx service:
service nginx status
- Disabling and Stopping MySQL Service:
- To stop the MySQL service:
service mysql stop
By mastering service manipulation commands, you can effectively manage the services running on your Linux system, ensuring smooth operation and efficient resource utilization.
Revision: Service Management
Service management in Red Hat Enterprise Linux (RHEL) revolves around using systemd, which is the default system and service manager. systemd manages system services, processes, and dependencies in a more efficient and parallel manner compared to the older SysV init system.
Key Concepts in systemd
- Unit: The basic object managed by systemd. Units can be services, sockets, devices, mount points, etc. Each unit has a corresponding configuration file.
- Service Unit: A unit configuration file with a .service extension, describing a system service.
- Target Unit: A unit that groups other units, allowing them to be managed collectively. These files have a .target extension.
- Dependency: systemd units can have dependencies on other units, meaning the start or stop of one unit can trigger the start or stop of another.
Common Commands for Service Management
1. Checking the Status of a Service
systemctl status <service_name>
Explanation: Displays the current status of the specified service, including whether it is active (running), inactive (stopped), or failed, along with the latest log entries.
Example:
systemctl status httpd
2. Starting a Service
systemctl start <service_name>
Explanation: Starts the specified service immediately.
Example:
systemctl start httpd
3. Stopping a Service
systemctl stop <service_name>
Explanation: Stops the specified service immediately.
Example:
systemctl stop httpd
4. Restarting a Service
systemctl restart <service_name>
Explanation: Stops and then starts the specified service. Useful for applying configuration changes.
Example:
systemctl restart httpd
5. Reloading a Service
systemctl reload <service_name>
Explanation: Reloads the configuration of the specified service without stopping it.
Example:
systemctl reload httpd
6. Enabling a Service
systemctl enable <service_name>
Explanation: Configures the specified service to start automatically at boot time by creating a symlink in the appropriate target directory.
Example:
systemctl enable httpd
7. Disabling a Service
systemctl disable <service_name>
Explanation: Prevents the specified service from starting automatically at boot by removing the symlink from the target directory.
Example:
systemctl disable httpd
8. Checking If a Service Is Enabled
systemctl is-enabled <service_name>
Explanation: Checks whether the specified service is configured to start automatically at boot.
Example:
systemctl is-enabled httpd
9. Viewing All Active Services
systemctl list-units –type=service –state=active
Explanation: Lists all active service units currently running on the system.
10. Viewing All Loaded Services
systemctl list-units –type=service
Explanation: Lists all loaded service units, whether they are active, inactive, or in any other state.
11. Masking a Service
systemctl mask <service_name>
Explanation: Completely disables a service by linking its unit file to /dev/null. This prevents the service from being started manually or automatically.
Example:
systemctl mask httpd
12. Unmasking a Service
systemctl unmask <service_name>
Explanation: Reverses the masking of a service, allowing it to be started manually or automatically again.
Example:
systemctl unmask httpd
Understanding Unit Files
Unit files describe how systemd should manage a service. They are located in:
- /etc/systemd/system/: Custom unit files created by the administrator.
- /usr/lib/systemd/system/: Default unit files provided by installed packages.
- /run/systemd/system/: Runtime unit files.
Example of a Service Unit File
[Unit]
Description=Apache HTTP Server
After=network.target
[Service]
Type=forking
ExecStart=/usr/sbin/httpd -k start
ExecReload=/usr/sbin/httpd -k graceful
ExecStop=/usr/sbin/httpd -k stop
PIDFile=/run/httpd/httpd.pid
[Install]
WantedBy=multi-user.target
- [Unit] section: Describes the service and its dependencies.
- [Service] section: Configures the service’s behavior, such as how to start, reload, and stop the service.
- [Install] section: Specifies how the service should be enabled (i.e., which target it should be linked to).
Practical Examples
Enable and Start a Service
systemctl enable httpd
systemctl start httpd
Explanation: Enables the httpd service to start at boot and then starts it immediately.
Check the Status of a Service
systemctl status httpd
Explanation: Displays the current status of the httpd service, showing whether it is running and providing recent log entries.
Restart and Reload a Service
systemctl restart httpd
systemctl reload httpd
Explanation: Restarts the httpd service, stopping and then starting it, and reloads its configuration without stopping it.
Disable and Stop a Service
systemctl disable httpd
systemctl stop httpd
Explanation: Prevents the httpd service from starting at boot and stops it immediately.
Mask and Unmask a Service
systemctl mask httpd
systemctl unmask httpd
Explanation: Completely disables the httpd service by masking it, and then allows it to be started again by unmasking it.
Viewing Logs with journalctl
systemd uses the journald daemon to collect and manage logs. The journalctl command is used to query and display logs.
View All Logs
journalctl
Explanation: Displays all log entries collected by journald.
View Logs for a Specific Service
journalctl -u <service_name>
Explanation: Displays log entries related to the specified service.
Example:
journalctl -u httpd
View Recent Logs
journalctl -r
Explanation: Displays log entries in reverse chronological order.
View Logs for the Current Boot
journalctl -b
Explanation: Displays log entries for the current boot session.
Follow Logs in Real-Time
journalctl -f
Explanation: Continuously displays new log entries as they are generated, similar to the tail -f command.
These commands and concepts are fundamental for managing services on a RHEL system. Understanding and practicing these commands will help ensure smooth operation and management of system services.