Linux services are background processes (also known as daemons) that perform various tasks. This chapter explores how to manage, configure, and troubleshoot these services, as well as test their security and functionality.
- Services are programs that run in the background to perform system-level tasks.
- Examples: web servers (Apache, Nginx), databases (MySQL, PostgreSQL), and file-sharing services (Samba).
- Service Manager: Manages services (e.g.,
systemd,init). - Service Files: Define how a service starts, stops, and operates.
systemctl [command] [service_name]| Command | Description |
|---|---|
start |
Start a service |
stop |
Stop a service |
restart |
Restart a service |
enable |
Enable service at boot |
disable |
Disable service at boot |
status |
Show service status |
- Start a service:
sudo systemctl start apache2
- Check the status of a service:
sudo systemctl status apache2
- Enable a service to start at boot:
sudo systemctl enable apache2
The service command is a legacy tool for managing services.
service [service_name] [action]- Restart a service:
sudo service apache2 restart
- Lists all active services.
systemctl list-units --type=service- Monitor running processes associated with services.
- Check if a service is running:
ps aux | grep apache2 - Use
topto monitor resource usage:
top
---
## 5. Configuring Services
### Modifying Service Files:
Service configuration files are typically located in `/etc/systemd/system/` or `/usr/lib/systemd/system/`.
### Example: Editing a Service File
1. Open the service file:
```bash
sudo nano /etc/systemd/system/my-service.service
- Modify parameters (e.g., environment variables, command options).
- Reload the systemd daemon:
sudo systemctl daemon-reload
- Restart the service:
sudo systemctl restart my-service
- Use
netstatorssto verify which ports are open.
sudo netstat -tuln- Review configuration files for secure settings.
- Example: Securing SSH (
/etc/ssh/sshd_config):PermitRootLogin no
- Test for open ports and vulnerabilities:
nmap -sV localhost
- Use
journalctlto view logs for a specific service:journalctl -u apache2.service
- Check the status and logs of a failed service:
systemctl status my-service journalctl -u my-service
- Restart the systemd manager if changes are not taking effect:
sudo systemctl daemon-reexec
By the end of this chapter, you should be able to:
- Start, stop, and manage services using
systemctl. - Configure services through service files.
- Test and secure services using tools like
nmapandnetstat. - Troubleshoot and debug service-related issues effectively.
- Move to Chapter 13: Becoming Secure and Anonymous to explore privacy and security techniques in Linux.
- Start and enable a web server service (e.g., Apache or Nginx) on your system.
- Check which ports are open on your system using
netstat. - Modify the configuration of a service (e.g., change the SSH port) and restart it.
- Use
nmapto scan your system for open ports and running services. - Debug a failed service by checking its logs with
journalctl.