Ricerca nel sito web

Come gestire i servizi Systemd con Systemctl su Linux


Su questa pagina

  1. Prerequisiti
  2. Verifica Systemd
  3. Gestisci i servizi con Systemd
  4. Gestisci i file delle unità Systemd
  5. Gestisci i socket con Systemctl
  6. Comandi aggiuntivi Systemctl
  7. Conclusione

Systemd un processo standard per la gestione dei servizi di avvio nei sistemi operativi Linux. Viene utilizzato per controllare quali programmi vengono eseguiti all'avvio del sistema Linux. È un gestore di sistema ed è diventato il nuovo standard per i sistemi operativi Linux. Systemd ti consente di creare un servizio systemd personalizzato per eseguire e gestire qualsiasi processo. In questo tutorial spiegheremo come gestire i servizi con systemd su Linux.

Prerequisiti

  • Un server che esegue Linux con Systemd, qui userò il server Ubuntu 20.04.
  • Una password di root è configurata sul server.

Verifica Systemd

Per impostazione predefinita, Systemd è preinstallato in tutti i principali sistemi operativi. Puoi verificare la versione installata di Systemd con il seguente comando:

systemctl --version

Dovresti vedere il seguente output:

systemd 245 (245.4-4ubuntu3)
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=hybrid

Puoi trovare la posizione di Systemd con il seguente comando:

whereis systemd

Dovresti ottenere il seguente output:

systemd: /usr/bin/systemd /usr/lib/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz

Per trovare la posizione systemctl, esegui il seguente comando:

whereis systemctl

Dovresti ottenere il seguente output:

systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

Puoi anche verificare se Systemd è in esecuzione o meno con il seguente comando:

ps -eaf | grep systemd

Dovresti ottenere il seguente output:

root         269       1  0 09:57 ?        00:00:01 /lib/systemd/systemd-journald
root         294       1  0 09:57 ?        00:00:00 /lib/systemd/systemd-udevd
systemd+     297       1  0 09:57 ?        00:00:00 /lib/systemd/systemd-networkd
message+     319       1  0 09:57 ?        00:00:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root         329       1  0 09:57 ?        00:00:00 /lib/systemd/systemd-logind
systemd+     382       1  0 09:57 ?        00:00:00 /lib/systemd/systemd-resolved
root        1128       1  0 10:47 ?        00:00:00 /lib/systemd/systemd --user
root        1177    1152  0 10:48 pts/0    00:00:00 grep --color=auto systemd

Puoi anche analizzare il tempo impiegato da ciascun processo all'avvio del sistema con il seguente comando:

systemd-analyze blame

Dovresti ottenere il seguente output:

3.662s csm.service                         
1.899s apt-daily-upgrade.service           
1.798s dev-sda1.device                     
1.400s systemd-logind.service              
1.336s fstrim.service                      
1.207s systemd-networkd.service            
1.131s systemd-resolved.service            
 856ms man-db.service                      
 842ms systemd-journald.service            
 415ms e2scrub_reap.service                
 320ms keyboard-setup.service              
 295ms networkd-dispatcher.service         
 239ms systemd-udev-trigger.service 

Per analizzare la catena critica all'avvio con il seguente comando:

systemd-analyze critical-chain

Dovresti ottenere il seguente output:

The time when unit became active or started is printed after the "@" character.
The time the unit took to start is printed after the "+" character.

graphical.target @4.948s
??multi-user.target @4.946s
  ??csm.service @1.282s +3.662s
    ??basic.target @1.277s
      ??sockets.target @1.276s
        ??dbus.socket @1.274s
          ??sysinit.target @1.257s
            ??systemd-update-utmp.service @1.247s +8ms
              ??systemd-tmpfiles-setup.service @1.228s +17ms
                ??systemd-journal-flush.service @1.201s +24ms
                  ??systemd-journald.service @356ms +842ms
                    ??systemd-journald.socket @313ms
                      ??system.slice @225ms
                        ??-.slice @225ms

Gestisci i servizi con Systemd

Systemctl fornisce un modo più semplice per gestire i servizi systemd.

Per avviare il servizio Apache, esegui il seguente comando:

systemctl start apache2

Per riavviare il servizio Apache, eseguire il seguente comando:

systemctl restart apache2

Per arrestare il servizio Apache, eseguire il seguente comando:

systemctl stop apache2

Per ricaricare il servizio Apache, eseguire il seguente comando:

systemctl reload apache2

Per abilitare l'avvio del servizio Apache dopo il riavvio del sistema, eseguire il seguente comando:

systemctl enable apache2

Per rimuovere il servizio Apache dall'avvio del sistema, eseguire il seguente comando:

systemctl disable apache2

Per terminare il servizio Apache, esegui il seguente comando:

systemctl kill apache2

Per mascherare il servizio Apache, esegui il seguente comando:

systemctl mask apache2

Per smascherare il processo Apache, esegui il seguente comando:

systemctl unmask apache2

Per verificare se il servizio Apache è abilitato o meno, esegui il seguente comando:

systemctl is-enabled apache2

Per verificare lo stato del servizio Apache, eseguire il seguente comando:

systemctl status apache2

Se il servizio è in esecuzione, dovresti ottenere il seguente output:

? apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-01-09 10:53:12 UTC; 26s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 1708 (apache2)
      Tasks: 55 (limit: 2353)
     Memory: 5.8M
     CGroup: /system.slice/apache2.service
             ??1708 /usr/sbin/apache2 -k start
             ??1709 /usr/sbin/apache2 -k start
             ??1710 /usr/sbin/apache2 -k start

Jan 09 10:53:12 ubunt4 systemd[1]: Starting The Apache HTTP Server...

Dopo aver rinominato o eliminato qualsiasi file systemd, sarà necessario ricaricarlo per applicare le modifiche. Per ricaricare il file di servizio systemd, eseguire il seguente comando:

systemctl daemon-reload

Gestisci i file delle unità di sistema

I comandi precedenti saranno utili per la gestione dei singoli servizi. Tuttavia, non può essere utilizzato per trovare lo stato corrente del sistema. Ci sono molti altri comandi utili per fornire queste informazioni.

Per visualizzare un elenco di tutte le unità disponibili, eseguire il seguente comando:

systemctl list-unit-files

Dovresti vedere un elenco di tutti i file di unità nel seguente output:

UNIT FILE                              STATE           VENDOR PRESET
proc-sys-fs-binfmt_misc.automount      static          enabled      
-.mount                                generated       enabled      
dev-hugepages.mount                    static          enabled      
dev-mqueue.mount                       static          enabled      
proc-sys-fs-binfmt_misc.mount          disabled        enabled      
sys-fs-fuse-connections.mount          static          enabled      
sys-kernel-config.mount                static          enabled      
sys-kernel-debug.mount                 static          enabled      
sys-kernel-tracing.mount               static          enabled      
acpid.path                             enabled         enabled      
ntp-systemd-netif.path                 enabled         enabled      
systemd-ask-password-console.path      static          enabled      
systemd-ask-password-wall.path         static          enabled      
session-2.scope                        transient       enabled      
acpid.service                          disabled        enabled      
apt-daily-upgrade.service              static          enabled      
apt-daily.service                      static          enabled      
                        enabled         enabled      
console-getty.service                  disabled        disabled     
console-setup.service                  enabled         enabled      
               static          enabled      
cron.service                           enabled         enabled      
cryptdisks-early.service               masked          enabled      
cryptdisks.service                     masked          enabled      
csm.service                            enabled         enabled      
dbus-org.freedesktop.hostname1.service static          enabled      
dbus-org.freedesktop.locale1.service   static          enabled      

Per visualizzare un elenco di tutte le unità attive, eseguire il seguente comando:

systemctl list-units

Dovresti vedere il seguente output:

  UNIT                                                                                        LOAD   ACTIVE SUB       DESCRIPTION             >
  proc-sys-fs-binfmt_misc.automount                                                           loaded active waiting   Arbitrary Executable Fil>
  sys-devices-pci0000:00-0000:00:03.0-virtio0-net-eth0.device                                 loaded active plugged   Virtio network device   >
  sys-devices-pci0000:00-0000:00:04.0-virtio1-net-eth1.device                                 loaded active plugged   Virtio network device   >
  sys-devices-pci0000:00-0000:00:05.0-virtio2-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged   QEMU_HARDDISK 1         >
  sys-devices-pci0000:00-0000:00:05.0-virtio2-host2-target2:0:0-2:0:0:0-block-sda.device      loaded active plugged   QEMU_HARDDISK           >
  sys-devices-platform-serial8250-tty-ttyS1.device                                            loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS10.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS11.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS12.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS13.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS14.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS15.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS16.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS17.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS18.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS19.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS2.device                                            loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS20.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS21.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS22.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS23.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS24.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS25.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS26.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS27.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS28.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS29.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS3.device                                            loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS30.device                                           loaded active plugged   /sys/devices/platform/se>
  sys-devices-platform-serial8250-tty-ttyS31.device                                           loaded active plugged   /sys/devices/platform/se>

Per visualizzare un elenco di tutte le unità guaste, eseguire il seguente comando:

systemctl --failed

Per elencare tutti i servizi inclusi i servizi abilitati e disabilitati, eseguire il seguente comando:

systemctl list-unit-files --type=service

Dovresti vedere il seguente output:

UNIT FILE                              STATE           VENDOR PRESET
acpid.service                          disabled        enabled      
apache-htcacheclean.service            disabled        enabled      
           disabled        enabled      
apache2.service                        enabled         enabled      
                       disabled        enabled      
apt-daily-upgrade.service              static          enabled      
apt-daily.service                      static          enabled      
                        enabled         enabled      
console-getty.service                  disabled        disabled     
console-setup.service                  enabled         enabled      
               static          enabled      
cron.service                           enabled         enabled      
cryptdisks-early.service               masked          enabled      
cryptdisks.service                     masked          enabled      
csm.service                            enabled         enabled      
dbus-org.freedesktop.hostname1.service static          enabled      
dbus-org.freedesktop.locale1.service   static          enabled      
dbus-org.freedesktop.login1.service    static          enabled      
dbus-org.freedesktop.resolve1.service  enabled         enabled      
dbus-org.freedesktop.timedate1.service static          enabled      
dbus-org.freedesktop.timesync1.service masked          enabled      
dbus.service                           static          enabled      
debug-shell.service                    disabled        disabled     
dmesg.service                          enabled         enabled      
                       static          enabled      
e2scrub_all.service                    static          enabled      
                  static          enabled      
e2scrub_reap.service                   enabled         enabled      
emergency.service                      static          enabled      
fstrim.service                         static          enabled      
getty-static.service                   static          enabled      
                         enabled         enabled      

Puoi vedere le proprietà dell'unità SSH, eseguire il seguente comando:

systemctl show sshd.service

Dovresti vedere il seguente output:

Id=sshd.service
Names=sshd.service
Requires=basic.target
Wants=system.slice
WantedBy=multi-user.target
Conflicts=shutdown.target
Before=shutdown.target multi-user.target
After=syslog.target network.target auditd.service systemd-journald.socket basic.target system.slice
Description=OpenSSH server daemon

Gestisci i socket con Systemctl

Puoi anche gestire i socket usando il comando Systemctl.

Per elencare tutti i socket di sistema disponibili, eseguire il seguente comando:

systemctl list-unit-files --type=socket

Dovresti vedere il seguente output:

UNIT FILE                        STATE    VENDOR PRESET
acpid.socket                     enabled  enabled      
dbus.socket                      static   enabled      
ssh.socket                       disabled enabled      
syslog.socket                    static   disabled     
systemd-fsckd.socket             static   enabled      
systemd-initctl.socket           static   enabled      
systemd-journald-audit.socket    static   enabled      
systemd-journald-dev-log.socket  static   enabled      
 static   enabled      
systemd-journald.socket          static   enabled      
         static   enabled      
systemd-networkd.socket          disabled enabled      
systemd-rfkill.socket            static   enabled      
systemd-udevd-control.socket     static   enabled      
systemd-udevd-kernel.socket      static   enabled      

Per avviare il socket SSH, eseguire il seguente comando:

systemctl start ssh.socket

Per arrestare il socket SSH, eseguire il seguente comando:

systemctl stop ssh.socket

Per riavviare il socket SSH, eseguire il seguente comando:

systemctl restart ssh.socket

Per verificare lo stato del socket SSH, eseguire il seguente comando:

systemctl status ssh.socket

Per abilitare e disabilitare il socket SSH all'avvio, eseguire il seguente comando:

systemctl enable ssh.socket
systemctl disable ssh.socket

Comandi aggiuntivi Systemctl

È possibile eseguire diverse attività utilizzando il comando systemctl. Se vuoi mettere il sistema in modalità utente singolo, esegui il seguente comando:

systemctl rescue

Per arrestare il sistema, eseguire il seguente comando:

systemctl halt

Per spegnere il sistema, eseguire il seguente comando:

systemctl poweroff

Per riavviare il sistema, eseguire il seguente comando:

systemctl reboot

Conclusione

Nella guida sopra, hai imparato come gestire e controllare il servizio systemd su Linux. Spero che questo ti aiuti a interagire e controllare la tua istanza systemd. Non esitate a chiedermi se avete domande.