웹사이트 검색

Linux에서 Systemctl로 Systemd 서비스를 관리하는 방법


이 페이지에서

  1. 전제 조건
  2. 시스템 확인\n
  3. Systemd로 서비스 관리
  4. Systemd Unit 파일 관리
  5. Systemctl로 소켓 관리
  6. Systemctl 추가 명령
  7. 결론

Linux 운영 체제에서 시작 서비스를 관리하기 위한 표준 프로세스를 체계화했습니다. Linux 시스템이 부팅될 때 실행되는 프로그램을 제어하는 데 사용됩니다. 시스템 관리자이며 Linux 운영 체제의 새로운 표준이 되었습니다. Systemd를 사용하면 모든 프로세스를 실행하고 관리하는 사용자 지정 systemd 서비스를 만들 수 있습니다. 이 튜토리얼에서는 Linux에서 systemd로 서비스를 관리하는 방법을 설명합니다.

전제 조건

  • Systemd와 함께 Linux를 실행하는 서버, 여기서는 Ubuntu 20.04 서버를 사용하겠습니다.\n
  • 루트 암호는 서버에 구성됩니다.\n

시스템 확인

기본적으로 Systemd는 모든 주요 운영 체제에 사전 설치되어 제공됩니다. 다음 명령을 사용하여 설치된 Systemd 버전을 확인할 수 있습니다.

systemctl --version

다음 출력이 표시되어야 합니다.

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

다음 명령을 사용하여 Systemd의 위치를 찾을 수 있습니다.

whereis systemd

다음 출력이 표시되어야 합니다.

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

systemctl 위치를 찾으려면 다음 명령을 실행합니다.

whereis systemctl

다음 출력이 표시되어야 합니다.

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

다음 명령을 사용하여 Systemd가 실행 중인지 여부를 확인할 수도 있습니다.

ps -eaf | grep systemd

다음 출력이 표시되어야 합니다.

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

다음 명령을 사용하여 시스템 부팅 시 각 프로세스에 소요된 시간을 분석할 수도 있습니다.

systemd-analyze blame

다음과 같은 결과가 표시되어야 합니다.

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 

다음 명령을 사용하여 부팅 시 중요 체인을 분석하려면:

systemd-analyze critical-chain

다음과 같은 결과가 표시되어야 합니다.

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

Systemd로 서비스 관리

Systemctl은 systemd 서비스를 관리하는 더 쉬운 방법을 제공합니다.

Apache 서비스를 시작하려면 다음 명령을 실행합니다.

systemctl start apache2

Apache 서비스를 다시 시작하려면 다음 명령을 실행합니다.

systemctl restart apache2

Apache 서비스를 중지하려면 다음 명령을 실행합니다.

systemctl stop apache2

Apache 서비스를 다시 로드하려면 다음 명령을 실행합니다.

systemctl reload apache2

시스템 재부팅 후 Apache 서비스를 시작하려면 다음 명령을 실행하십시오.

systemctl enable apache2

시스템 시작 시 Apache 서비스를 제거하려면 다음 명령을 실행하십시오.

systemctl disable apache2

Apache 서비스를 종료하려면 다음 명령을 실행하십시오.

systemctl kill apache2

Apache 서비스를 마스킹하려면 다음 명령을 실행합니다.

systemctl mask apache2

Apache 프로세스의 마스크를 해제하려면 다음 명령을 실행하십시오.

systemctl unmask apache2

Apache 서비스가 활성화되었는지 여부를 확인하려면 다음 명령을 실행합니다.

systemctl is-enabled apache2

Apache 서비스의 상태를 확인하려면 다음 명령을 실행합니다.

systemctl status apache2

서비스가 실행 중인 경우 다음 출력이 표시되어야 합니다.

? 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...

systemd 파일의 이름을 바꾸거나 삭제한 후 변경 사항을 적용하려면 다시 로드해야 합니다. systemd 서비스 파일을 다시 로드하려면 다음 명령을 실행하십시오.

systemctl daemon-reload

시스템 단위 파일 관리

위의 명령은 단일 서비스를 관리하는 데 유용합니다. 그러나 시스템의 현재 상태를 찾는 데 사용할 수는 없습니다. 이 정보를 제공하는 데 유용한 다른 많은 명령이 있습니다.

사용 가능한 모든 단위 목록을 보려면 다음 명령을 실행하십시오.

systemctl list-unit-files

다음 출력에 모든 단위 파일 목록이 표시되어야 합니다.

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      

모든 활성 장치 목록을 보려면 다음 명령을 실행하십시오.

systemctl list-units

다음 출력이 표시되어야 합니다.

  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>

실패한 모든 장치 목록을 보려면 다음 명령을 실행하십시오.

systemctl --failed

활성화 및 비활성화된 서비스를 포함한 모든 서비스를 나열하려면 다음 명령을 실행합니다.

systemctl list-unit-files --type=service

다음 출력이 표시되어야 합니다.

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      

SSH 장치의 속성을 볼 수 있습니다. 다음 명령을 실행합니다.

systemctl show sshd.service

다음 출력이 표시되어야 합니다.

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

Systemctl로 소켓 관리

Systemctl 명령을 사용하여 소켓을 관리할 수도 있습니다.

사용 가능한 모든 시스템 소켓을 나열하려면 다음 명령을 실행하십시오.

systemctl list-unit-files --type=socket

다음 출력이 표시되어야 합니다.

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      

SSH 소켓을 시작하려면 다음 명령을 실행합니다.

systemctl start ssh.socket

SSH 소켓을 중지하려면 다음 명령을 실행합니다.

systemctl stop ssh.socket

SSH 소켓을 다시 시작하려면 다음 명령을 실행합니다.

systemctl restart ssh.socket

SSH 소켓의 상태를 확인하려면 다음 명령을 실행합니다.

systemctl status ssh.socket

부팅 시 SSH 소켓을 활성화 및 비활성화하려면 다음 명령을 실행합니다.

systemctl enable ssh.socket
systemctl disable ssh.socket

Systemctl 추가 명령

systemctl 명령을 사용하여 여러 작업을 수행할 수 있습니다. 시스템을 단일 사용자 모드로 전환하려면 다음 명령을 실행하십시오.

systemctl rescue

시스템을 중지하려면 다음 명령을 실행하십시오.

systemctl halt

시스템을 종료하려면 다음 명령을 실행하십시오.

systemctl poweroff

시스템을 다시 시작하려면 다음 명령을 실행하십시오.

systemctl reboot

결론

위 가이드에서는 Linux에서 systemd 서비스를 관리하고 제어하는 방법을 배웠습니다. 이것이 시스템 인스턴스와 상호 작용하고 제어하는 데 도움이 되기를 바랍니다. 궁금한 점이 있으면 언제든지 문의해 주세요.