A menudo, hay una necesidad de habilitar o deshabilitar servicios temporal o permanentemente en nuestro sistema Ubuntu. A veces, podemos requerir que ciertos servicios se inicien automáticamente al arrancar, por ejemplo, ssh o servidores web, y a veces podemos necesitar deshabilitar servicios que ya no necesitamos y que están acaparando la CPU y la RAM.
En este artículo, echamos un vistazo a cómo podemos habilitar y deshabilitar servicios en Ubuntu.
Para hacer esto, primero debemos entender que hay 3 sistemas principales de init para Ubuntu:
- Systemd
- Upstart
- SysV
Cada sistema init tiene una forma diferente de iniciar y detener los servicios. Echaremos un vistazo a cada uno de estos.
Cómo habilitar y deshabilitar servicios en Systemd init
Para iniciar un servicio en systemd ejecuta el comando como se muestra:
systemctl start service-name
Por ejemplo, para iniciar el servicio web de apache, ejecuta,
systemctl start apache2
Para verificar que el servicio se está ejecutando, ejecuta,
systemctl status apache2
Salida
apache2.service - LSB: Apache2 web server Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: active (running) since Thu 2018-03-15 17:09:05 UTC; 35s ago Docs: man:systemd-sysv-generator(8) CGroup: /system.slice/apache2.service ├─2499 /usr/sbin/apache2 -k start ├─2502 /usr/sbin/apache2 -k start └─2503 /usr/sbin/apache2 -k start Mar 15 17:09:04 ip-172-31-41-251 systemd[1]: Starting LSB: Apache2 web server... Mar 15 17:09:04 ip-172-31-41-251 apache2[2475]: * Starting Apache httpd web ser Mar 15 17:09:05 ip-172-31-41-251 apache2[2475]: * Mar 15 17:09:05 ip-172-31-41-251 systemd[1]: Started LSB: Apache2 web server.
Para detener el servicio en ejecución,
systenctl stop apache2
Para confirmar que el servicio no se está ejecutando, ejecuta,
systemctl status apache2
Salida
apache2.service - LSB: Apache2 web server Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─apache2-systemd.conf Active: inactive (dead) since Thu 2018-03-15 17:19:47 UTC; 12s ago Docs: man:systemd-sysv-generator(8) Process: 2822 ExecStop=/etc/init.d/apache2 stop (code=exited, status=0/SUCCESS Process: 2687 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCE Mar 15 17:10:11 ip-172-31-41-251 systemd[1]: Starting LSB: Apache2 web server... Mar 15 17:10:11 ip-172-31-41-251 apache2[2687]: * Starting Apache httpd web ser Mar 15 17:10:12 ip-172-31-41-251 apache2[2687]: * Mar 15 17:10:12 ip-172-31-41-251 systemd[1]: Started LSB: Apache2 web server. Mar 15 17:19:46 ip-172-31-41-251 systemd[1]: Stopping LSB: Apache2 web server... Mar 15 17:19:46 ip-172-31-41-251 apache2[2822]: * Stopping Apache httpd web ser Mar 15 17:19:47 ip-172-31-41-251 apache2[2822]: * Mar 15 17:19:47 ip-172-31-41-251 systemd[1]: Stopped LSB: Apache2 web server.
Para habilitar el servicio apache2 en el arranque,
systemctl enable apache2
Para deshabilitar el servicio apache2 al arrancar, ejecuta,
systemctl disable apache2
Para reiniciar el servicio,
systemctl restart apache2
Para comprobar si el servicio está actualmente configurado para iniciarse en el siguiente arranque,
systemctl is-enabled apache2
Salida
Executing /lib/systemd/systemd-sysv-install is-enabled apache2 enabled
Para verificar si el servicio está activo,
systemctl is-active apache2
Salida
active
Cómo eliminar completamente los servicios de Systemd
¿Qué pasa si instalas un paquete y luego decides que ya no lo necesitas? ¿Cómo se hace para eliminarlo por completo? Sigue las siguientes instrucciones.
Primero, detén el servicio,
systemctl stop service-name
A continuación, desactiva el servicio,
systemctl disable service-name
Eliminar el servicio en systemd:
rm /etc/systemd/system/service-name
rm /etc/systemd/system/service-name/[related symlinks]
Recarga systemd,
systemctl daemon-reload
Y por último,
systemctl reset-failed
Cómo habilitar y deshabilitar servicios en Upstart init
El sistema init de Upstart fue revelado justo antes de systemd Se usó en Ubuntu 9.10 a Ubuntu 14.10. Posteriormente se fue eliminando paulatinamente el cambio por systemd en Ubuntu 15.04 y versiones más recientes. En este ejemplo, veamos cómo podemos iniciar y detener, habilitar y deshabilitar servicios en Ubuntu 14.04.
Upstart utiliza los ficheros de configuración para controlar los servicios, que se encuentran en el directorio /etc/init. Estos archivos están compuestos de secciones de texto plano organizadas en stanzas (estrofas) y cada stanza describe un servicio y cómo funciona.
Para comprobar si un servicio se está ejecutando o no, ejecuta el siguiente comando,
initctl status service-name
O,
service service-name status
O,
status service-name
En este ejemplo, comprobaremos el estado de cups, un servidor de impresión Linux.
initctl status cups
O,
service cups status
O,
status cups
Salida
cups start/running, process 3029
Para detener el servicio ejecuta el siguiente comando,
initctl stop cups
O,
service cups stop
O,
stop cups
Salida
cups stop/waiting
Para habilitar un servicio en Upstart init
En el archivo /etc/init/*.conf, encontrarás la directiva “respawn” que arranca un servicio en caso de un fallo inesperado o si el sistema se reinicia. Normalmente está habilitado por defecto.
Por ejemplo, en el archivo /etc/init/cups.conf a continuación,
El primer argumento (3) es el número de intentos que intentará reiniciar y el segundo (12) es el intervalo de tiempo entre reintentos. Si no se reinicia automáticamente, se mantendrá en estado de parada.
Para deshabilitar un servicio en el inicio de init
Ejecute el siguiente comando,
echo manual >> /etc/init/service.override
Esto crea un archivo de anulación que deshabilita un servicio sin alterar en absoluto la definición del trabajo.
Para el servicio de tazas, el comando será,
echo manual >> /etc/init/cups.override
Al reiniciar el sistema, las copas estarán en un estado de parada. Si deseas volver a activar el servicio, debes eliminar el archivo /etc/init/cups.override.
Herramienta Sysv-rc-conf
Se trata de una consola basada en texto que ofrece una visión general de los diferentes niveles de servicio y de ejecución que están programados para iniciar.
Se puede instalar usando el siguiente comando,
apt-get install sysv-rc-conf
Para ejecutar la herramienta, ejecuta,
sysv-rc-conf
Herramienta Jobs-Admin
Esta es otra característica que te permite controlar servicios y procesos en un entorno GUI.
Puedes instalarlo ejecutando.
apt-get install jobs-admin
Cómo habilitar y deshabilitar servicios en SysV init
Para habilitar un servicio en la ejecución de SysV init,
update-rc.d enable service-name
Por ejemplo, si quieres habilitar el servidor web apache, deberás ejecutar,
update-rc.d enable apache2
Para desactivar un servicio, ejecuta,
update-rc.d disable service-name
Por ejemplo,
update-rc.d disable apache2
Casi todos los sistemas Linux se ejecutan en Systemd , desde Ubuntu, Debian, RHEL y CentOS. Por lo tanto, descubrirás que estarás usando más del comando systemctl para iniciar, detener, habilitar y deshabilitar servicios.