systemd 란?
systemd(system daemon)은 전통적으로 Unix 시스템이 부팅후에 가장 먼저 생성된 후에 다른 프로세스를 실행하는 init 역할을 대체하는 데몬입니다. Red Hat 에 근무하는 Lennart Poettering 이 주도적으로 개발을 시작했고 지금은 RHEL/CentOS 와 Ubuntu 나 Arch 등 대부분의 리눅스 시스템에 공식적으로 채택되었습니다.
다음과 같이 매우 복잡한 아키텍처를 갖고 있지만 일반 리눅스 사용자 입장에서는 최상단의 systemd utillities 인 systemctl, journalctl 등 유틸리티 사용법을 위주로 익히면 되며 이 문서에서는 systemctl 사용 방법에 대해서 설명합니다.
출처: https://en.wikipedia.org/wiki/Systemd#/media/File:Systemd_components.svg
Target 관리
- target 은 기존 SysV init 의 run level 과 같은 개념
- systemd 의 기본 타겟 명은 default.target 이며 다중 사용자 모드인 mutl-user.target 의 별칭
기본 타겟 확인
$ systemctl get-default multi-user.target
타겟 목록 출력
$ systemctl list-units --type target UNIT LOAD ACTIVE SUB DESCRIPTION basic.target loaded active active Basic System cryptsetup.target loaded active active Encrypted Volumes getty.target loaded active active Login Prompts local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems multi-user.target loaded active active Multi-User System network-online.target loaded active active Network is Online network.target loaded active active Network paths.target loaded active active Paths remote-fs.target loaded active active Remote File Systems slices.target loaded active active Slices sockets.target loaded active active Sockets sound.target loaded active active Sound Card swap.target loaded active active Swap sysinit.target loaded active active System Initialization timers.target loaded active active Timers LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type
활성화되지 않은 타겟(rescue, emergency 등) 목록도 출력하려면 --all 옵션 추가
$ systemctl list-units --type target --all
기본 타겟 변경
$ systemctl set-default name.target
설치시 기본 타겟은 multi-user.target 이며 부팅시 X-Windows 로 로그인하려면 graphical.target 으로 설정
$ sudo systemctl set-default graphical.target
재부팅하지 않고 현재 타겟 변경
재부팅하지 않고 현재 리눅스의 런 레벨(run level)을 변경하는 명령어인 telinit 처럼 systemctl isolate 명령어를 사용하면 현재 target 을 변경할 수 있음.
$ sudo systemctl isolate graphical.target
$ sudo systemctl isolate multi-user.target
single user mode
시스템 복구등의 이유로 single user mode 로 진입할 필요가 있을 경우 rescue 명령어 사용
$ sudo systemctl rescue
systemctl emergency
파일 시스템이 깨졌거나 등의 이유로 single mode 로 진입할 수 없을 때 emergency 로 들어가면 부팅시 최소의 기능으로만 부팅(root 파일 시스템은 read only 로 마운트하고 다른 파일 시스템은 마운트를 안 하는 등) 하므로 응급 복구 가능
$ sudo systemctl emergency
기존 명령어와 비교
service
service | systemctl | 설명 |
---|---|---|
|
| 서비스 시작 |
|
| 서비스 중지 |
|
| 서비스 재시작 |
|
| 서비스가 구동중이었을 경우에만 재시작 |
|
| 설정 재구동 |
|
| 서비스 구동 여부 확인 |
|
| 모든 서비스의 상태 표시 |
chkconfig
service | systemctl | 설명 |
---|---|---|
|
| 서비스 활성화(부팅시 자동 구동) |
|
| 서비스 비활성화 |
|
| 서비스의 활성화 여부 표시 |
|
| 모든 서비스의 현재 활성화 여부 표시 |
|
| 지정한 target 이후에 시작하는 서비스를 표시 |
|
| 지정한 target 이전에 시작하는 서비스를 표시 |
서비스 관리
서비스 상태 확인
systemctl status 서비스명
Ex
systemctl status php-fpm.service
서비스 구동
systemctl start mariadb.service
서비스 자동 시작
systemctl start mariadb.service
서비스 목록 보기
list-units 명령어 사용
$ sudo systemctl list-units
설치된 모든 unit 파일을 보려면 list-unit-files 사용
$ sudo systemctl list-unit-files
서비스 마스킹
동일한 용도로 사용하는 서비스가 동시에 설치되어 있을 경우 제대로 동작하지 않고 충돌할 수 있음. 예로 ntpd 와 chronyd 는 동일한 용도이므로 2개가 동시에 설치되어 있을 경우 문제가 되므로 이럴때는 service masking 를 사용하면 됨.
마스킹하면 서로 충돌하는 서비스를 실수로 시작하지 않게 됨.
예로 다음 명령어를 실행하면 실수로 ntpd 를 구동해도 ntpd 가 시작되지 않음.
$ sudo systemctl mask ntpd Created symlink from /etc/systemd/system/ntpd.service to /dev/null
실수로 ntpd 를 실행하는 경우 아래와 같은 메시지가 출력됨.
$ sudo systemctl start ntpd Failed to start ntpd.service: Unit is masked.
마스킹된 서비스를 해제하려면 systemctl unmask 명령어를 실행
$ sudo systemctl unmask ntpd Removed symlink /etc/systemd/system/ntpd.service
조건에 따라 서비스 보기
구동에 실패한 서비스
$ sudo systemctl list-units --state=failed
모든 active 목록
$ sudo systemctl list-units --state=active
상태가 inactive 인 목록
$ sudo systemctl list-units --all --state=inactive
서비스중에 상태가 running 인 목록
$ sudo systemctl list-units --type=service --state=running
특정 서비스가 active 상태인지 조회
서비스가 현재 active 상태인지 조회할 경우 is-active 구문 사용
$ sudo systemctl is-active nginx failed
서비스가 부팅때 구동되도록 설정 여부
서비스가 현재 active 상태인지 조회할 경우 is-eabled 구문 사용
$ sudo systemctl is-enabled nginx disabled
journalctl
Linux journalctl 사용법 을 참고하세요.