Capítulo 8.1 - Introducción a systemd
Objetivos de Aprendizaje
Al final de este capítulo, usted será capaz de:
- Comprender qué es systemd
- Utilizar systemctl para gestionar servicios
- Comprender las unidades de systemd
- Ver el estado de los servicios
- Iniciar, detener y reiniciar servicios
¿Qué es systemd?
systemd es el sistema de inicialización y gestión de servicios en la mayoría de las distribuciones Linux modernas.
Funciones:
- Gestión de servicios
- Arranque del sistema
- Gestión de logs (journald)
- Planificación (timers)
- Montaje de sistemas de archivos
Unidades de systemd
Tipos de Unidades
# Ver tipos
systemctl list-units --type=service
systemctl list-units --type=timer
systemctl list-units --type=mount
systemctl list-units --type=socket
Tipos comunes:
- .service: Servicios
- .timer: Tareas programadas
- .mount: Puntos de montaje
- .socket: Sockets de red
- .target: Grupos de unidades
systemctl - Comandos Básicos
Ver el Estado
# Todos los servicios
systemctl list-units --type=service
# Servicios activos
systemctl list-units --type=service --state=running
# Servicios inactivos
systemctl list-units --type=service --state=inactive
# Servicios fallidos
systemctl list-units --type=service --state=failed
# Estado de un servicio
systemctl status sshd
systemctl status apache2
Iniciar/Detener
# Iniciar
sudo systemctl start service_name
# Detener
sudo systemctl stop service_name
# Reiniciar
sudo systemctl restart service_name
# Recargar configuración
sudo systemctl reload service_name
# Recargar o reiniciar
sudo systemctl reload-or-restart service_name
Activar/Desactivar al Arranque
# Activar (inicio automático)
sudo systemctl enable service_name
# Desactivar
sudo systemctl disable service_name
# Activar e iniciar
sudo systemctl enable --now service_name
# Verificar si está activado
systemctl is-enabled service_name
Estados de los Servicios
Estados Posibles
Estados:
- active (running): En ejecución
- active (exited): Finalizado correctamente
- inactive: Detenido
- failed: Fallido
- activating: Iniciando
Ver Detalles
# Estado detallado
systemctl status service_name
# Información completa
systemctl show service_name
# Propiedad específica
systemctl show -p ActiveState service_name
systemctl show -p LoadState service_name
Comandos Útiles
Listar Servicios
# Todos los servicios
systemctl list-units --type=service --all
# Servicios activados
systemctl list-unit-files --type=service --state=enabled
# Servicios desactivados
systemctl list-unit-files --type=service --state=disabled
# Por estado
systemctl list-units --type=service --state=running
systemctl list-units --type=service --state=failed
Ocultar/Mostrar
# Ocultar servicio
sudo systemctl mask service_name
# Mostrar
sudo systemctl unmask service_name
Recargar systemd
# Recargar configuración (después de modificar archivos)
sudo systemctl daemon-reload
Ejemplos Prácticos
Gestión de SSH
# Ver estado
systemctl status sshd
# Iniciar
sudo systemctl start sshd
# Activar al arranque
sudo systemctl enable sshd
# Reiniciar
sudo systemctl restart sshd
Gestión de Apache/Nginx
# Apache
sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl status apache2
# Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
Servicios Fallidos
# Ver servicios fallidos
systemctl list-units --state=failed
# Reiniciar servicio fallido
sudo systemctl restart failed_service
# Ver logs
sudo journalctl -u failed_service
Resumen
En este capítulo, usted ha aprendido:
systemd: Sistema de inicialización y gestión de servicios
Unidades: .service, .timer, .mount, .socket, .target
systemctl: Comandos para gestionar servicios
Estados: active, inactive, failed, activating
Comandos: start, stop, restart, enable, disable, status
Práctica: Gestión de servicios comunes (SSH, Apache, etc.)
Próximos Pasos
Capítulo 8.2: Creación de Servicios Personalizados
Capítulo 8.3: Cron Avanzado y systemd Timers
Capítulo creado el: diciembre 2024