Cómo habilitar o deshabilitar servicios en Ubuntu Systemd / Upstart

A menudo, es necesario habilitar o deshabilitar servicios de forma temporal o permanente en nuestro sistema Ubuntu. A veces, es posible que necesitemos que ciertos servicios se inicien automáticamente al arrancar, por ejemplo, ssh o servidores web y, a veces, es posible que necesitemos deshabilitar servicios que ya no necesitamos y estamos acaparando la CPU y la RAM.
En este tutorial, echamos un vistazo a cómo podemos habilitar y deshabilitar servicios en Ubuntu. Para hacer esto, primero debemos entender que hay 3 sistemas de inicio principales para Ubuntu
Cada sistema de inicio tiene una forma diferente de iniciar y detener servicios. Echaremos un vistazo a cada uno de estos.
Cómo habilitar y deshabilitar servicios en Systemd init
Para iniciar un servicio en systemd ejecute el comando como se muestra:
systemctl start service-name
Por ejemplo, para iniciar el servicio web apache, ejecute
systemctl start apache2
Para verificar que el servicio se está ejecutando, ejecute
systemctl status apache2
Producción
● 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 servicio en ejecución
systemctl stop apache2
Para confirmar que el servicio no se está ejecutando, ejecute
systemctl status apache2
Producción
● 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, ejecute
systemctl enable apache2
Para deshabilitar el servicio apache2 en el arranque, ejecute
systemctl disable apache2
Para reiniciar el servicio
systemctl restart apache2
Para comprobar si el servicio está configurado actualmente para iniciarse en el próximo arranque
systemctl is-enabled apache2
Producción
Executing /lib/systemd/systemd-sysv-install is-enabled apache2 enabled
Para comprobar si el servicio está activo
systemctl is-active apache2
Producción
active
Cómo eliminar completamente los servicios de Systemd
¿Qué sucede si instala un paquete y luego decide que ya no lo necesita? ¿Cómo se hace para eliminarlo por completo? Siga los comandos a continuación.
Primero, detenga el servicio
systemctl stop service-name
Luego deshabilita el servicio
systemctl disable service-name
Eliminando el servicio en systemd
rm /etc/systemd/system/service-name
rm /etc/systemd/system/service-name/[related symlinks]
Recargar systemd
systemctl daemon-reload
Finalmente corre,
systemctl reset-failed
Cómo habilitar y deshabilitar servicios en Upstart init
El sistema de inicio Upstart se dio a conocer justo antes de systemd. Se usó en Ubuntu 9.10 a Ubuntu 14.10. Más tarde se eliminó paulatinamente allanando el camino para systemd init 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 hace uso de archivos de configuración para controlar los servicios. Estos archivos se guardan en el directorio / etc / init. Estos archivos se componen de secciones de texto sin formato organizadas en estrofas y cada estrofa describe un servicio y cómo funciona.
Para comprobar si un servicio se está ejecutando o no ejecuta el comando a continuación
initctl status service-name
O
service service-name status
O
status service-name
En este ejemplo, verificaremos el estado de cups, un servidor de impresión Linux.
initctl status cups
O
service cups status
O
status cups
Producción
cups start/running, process 3029
Para detener el servicio ejecuta el comando a continuación
initctl stop cups
O
service cups stop
O
stop cups
Producción
cups stop/waiting
Para habilitar un servicio en Upstart init
En el archivo /etc/init/*.conf, encontrará el «reaparecer«Directiva que inicia un servicio en caso de que se bloquee inesperadamente o si el sistema se reinicia. Normalmente está habilitado de forma predeterminada.
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 argumento (12) es el intervalo de tiempo entre reintentos. Si no se reinicia automáticamente. se mantendrá en estado detenido.
Para deshabilitar un servicio en upstart init
ejecuta el comando a continuación
echo manual >> /etc/init/service.override
Esto crea un archivo de anulación que deshabilita un servicio sin alterar la definición del trabajo en absoluto.
Para el servicio de tazas, el comando será
echo manual >> /etc/init/cups.override
Tras un reinicio del sistema, las tazas estarán en un estado detenido. Si desea volver a habilitar el servicio, debe eliminar el archivo /etc/init/cups.override.
Herramienta sysv-rc-conf
Esta es una consola basada en texto que le brinda una descripción general de los diferentes niveles de servicio y ejecución que están programados para iniciarse. Se puede instalar usando el siguiente comando
apt-get install sysv-rc-conf
Para ejecutar la herramienta, ejecute
sysv-rc-conf
Herramienta de administración de trabajos
Esta es otra característica que le permite controlar servicios y procesos en un entorno GUI. Puede instalar esto ejecutando.
apt-get install jobs-admin
Cómo habilitar y deshabilitar servicios en SysV init
Para habilitar un servicio en SysV init, ejecute
update-rc.d enable service-name
Por ejemplo, si desea habilitar el servidor web apache, debe ejecutar
update-rc.d enable apache2
Para deshabilitar un servicio, ejecute
update-rc.d disable service-name
Por ejemplo
update-rc.d disable apache2
Casi todos los sistemas Linux se ejecutan en Systemd init desde Ubuntu, Debian, RHEL y CentOS. Por lo tanto, descubrirá que utilizará más del comando systemctl para iniciar, detener, habilitar y deshabilitar servicios. Le invitamos a probar los comandos que se muestran en este artículo. Gracias.
Leer también: