Skip to content

Latest commit

History

History
185 lines (141 loc) 路 4.26 KB

File metadata and controls

185 lines (141 loc) 路 4.26 KB

Chapter 12: Using and Abusing Services

Overview

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.


1. Understanding Services

What are Services?

  • 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).

Key Components:

  • Service Manager: Manages services (e.g., systemd, init).
  • Service Files: Define how a service starts, stops, and operates.

2. Managing Services with systemctl

Syntax:

systemctl [command] [service_name]

Common Commands:

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

Examples:

  • 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

3. Managing Services with service

The service command is a legacy tool for managing services.

Syntax:

service [service_name] [action]

Example:

  • Restart a service:
    sudo service apache2 restart

4. Monitoring Active Services

systemctl list-units

  • Lists all active services.

Example:

systemctl list-units --type=service

ps and top

  • Monitor running processes associated with services.

Examples:

  • Check if a service is running:
    ps aux | grep apache2
  • Use top to 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
  1. Modify parameters (e.g., environment variables, command options).
  2. Reload the systemd daemon:
    sudo systemctl daemon-reload
  3. Restart the service:
    sudo systemctl restart my-service

6. Testing Service Security

Checking Open Ports

  • Use netstat or ss to verify which ports are open.

Example:

sudo netstat -tuln

Verifying Service Configuration

  • Review configuration files for secure settings.
  • Example: Securing SSH (/etc/ssh/sshd_config):
    PermitRootLogin no

Using nmap to Scan Services

  • Test for open ports and vulnerabilities:
    nmap -sV localhost

7. Troubleshooting Services

Viewing Logs

  • Use journalctl to view logs for a specific service:
    journalctl -u apache2.service

Debugging Failed Services

  • Check the status and logs of a failed service:
    systemctl status my-service
    journalctl -u my-service

Restarting the Daemon

  • Restart the systemd manager if changes are not taking effect:
    sudo systemctl daemon-reexec

Summary

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 nmap and netstat.
  • Troubleshoot and debug service-related issues effectively.

Next Steps:


Exercises

  1. Start and enable a web server service (e.g., Apache or Nginx) on your system.
  2. Check which ports are open on your system using netstat.
  3. Modify the configuration of a service (e.g., change the SSH port) and restart it.
  4. Use nmap to scan your system for open ports and running services.
  5. Debug a failed service by checking its logs with journalctl.