Lab 8.1 - Gestión de Servicios systemd
Objetivos
Al final de este laboratorio, usted será capaz de:
- Utilizar systemctl para gestionar los servicios
- Comprender los estados de los servicios
- Iniciar, detener, reiniciar servicios
- Activar/desactivar servicios al arranque
- Ver los logs de los servicios
Prerrequisitos
- Ubuntu 24.04 instalado
- Acceso sudo
- Terminal abierto
Ejercicio 1: Comandos Básicos
Paso 1: Ver el Estado de un Servicio
# Estado de un servicio
systemctl status sshd
# o
systemctl status ssh
# Estado corto
systemctl is-active sshd
systemctl is-enabled sshd
systemctl is-failed sshd
Paso 2: Listar los Servicios
# 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
Ejercicio 2: Control de Servicios
Paso 1: Iniciar/Detener
# Iniciar un servicio
sudo systemctl start sshd
# Detener un servicio
sudo systemctl stop sshd
# Reiniciar
sudo systemctl restart sshd
# Recargar configuración (si es compatible)
sudo systemctl reload sshd
Paso 2: Activar al Arranque
# Activar (inicio automático)
sudo systemctl enable sshd
# Desactivar
sudo systemctl disable sshd
# Activar e iniciar
sudo systemctl enable --now sshd
Paso 3: Enmascarar un Servicio
# Enmascarar (impide inicio manual también)
sudo systemctl mask sshd
# Desenmascarar
sudo systemctl unmask sshd
Ejercicio 3: Logs de los Servicios
Paso 1: journalctl
# Logs de un servicio
sudo journalctl -u sshd
# Últimas líneas
sudo journalctl -u sshd -n 50
# Seguir en tiempo real
sudo journalctl -u sshd -f
# Logs desde hoy
sudo journalctl -u sshd --since today
# Logs de un periodo
sudo journalctl -u sshd --since "2024-12-01" --until "2024-12-31"
Paso 2: Niveles de Log
# Por prioridad
sudo journalctl -u sshd -p err
sudo journalctl -u sshd -p warning
# Combinaciones
sudo journalctl -u sshd -p err --since "1 hour ago"
Ejercicio 4: Servicios del Sistema
Paso 1: Servicios Esenciales
# Ver los servicios del sistema
systemctl list-units --type=service | grep systemd
# Estado de systemd
systemctl status
# Servicios críticos
systemctl list-units --type=service --state=failed
Paso 2: Analizar las Dependencias
# Dependencias de un servicio
systemctl list-dependencies sshd
# Servicios que dependen de sshd
systemctl list-dependencies --reverse sshd
Ejercicio 5: Práctica con nginx
Paso 1: Instalación y Configuración
# Instalar nginx
sudo apt update
sudo apt install -y nginx
# Verificar el estado
systemctl status nginx
# Iniciar
sudo systemctl start nginx
# Activar al arranque
sudo systemctl enable nginx
Paso 2: Gestión
# Reiniciar
sudo systemctl restart nginx
# Recargar configuración
sudo systemctl reload nginx
# Ver los logs
sudo journalctl -u nginx -f
Paso 3: Prueba
# Probar el servicio
curl http://localhost
# Ver los procesos
ps aux | grep nginx
Ejercicio 6: Script de Gestión
Paso 1: Script Básico
# Script para gestionar un servicio
cat > manage-service.sh << 'EOF'
#!/bin/bash
SERVICE=$1
ACTION=$2
if [ -z "$SERVICE" ] || [ -z "$ACTION" ]; then
echo "Usage: $0 service {start|stop|restart|status|enable|disable}"
exit 1
fi
case $ACTION in
start)
sudo systemctl start "$SERVICE"
;;
stop)
sudo systemctl stop "$SERVICE"
;;
restart)
sudo systemctl restart "$SERVICE"
;;
status)
systemctl status "$SERVICE"
;;
enable)
sudo systemctl enable "$SERVICE"
;;
disable)
sudo systemctl disable "$SERVICE"
;;
*)
echo "Acción desconocida: $ACTION"
exit 1
;;
esac
EOF
chmod +x manage-service.sh
# Usar
./manage-service.sh nginx status
./manage-service.sh nginx restart
Verificación
Checklist
Verifique que usted puede:
- Ver el estado de los servicios (systemctl status)
- Listar los servicios (list-units)
- Iniciar/detener servicios
- Activar/desactivar al arranque
- Ver los logs (journalctl)
- Analizar las dependencias
- Gestionar servicios comunes (nginx, sshd)
Preguntas de Reflexión
-
¿Cuál es la diferencia entre start y enable?
- start: Inicia ahora
- enable: Activa al arranque del sistema
-
¿Cuándo usar reload vs restart?
- reload: Recarga la configuración sin interrupción
- restart: Reinicia completamente (interrupción)
-
¿Por qué usar journalctl en lugar de /var/log?
- journalctl: Logs centralizados de systemd, más moderno
- /var/log: Logs tradicionales, siempre disponibles
Próximos Pasos
- Lab 8.2: Servicios Personalizados
- Lab 8.3: Cron Avanzado y systemd Timers
- Capítulo 8.1: Gestión de Servicios systemd
Lab creado el: diciembre 2024 - Probado en Ubuntu 24.04