CS/Linux

[Linux] 리눅스 초기화 (SysV init, systemd, rc.local)

kyoulho 2025. 8. 10. 13:45

1. 초기화 개요

리눅스 시스템이 부팅될 때는 다음과 같은 순서를 거칩니다.

BIOS/UEFI → 부트로더(GRUB) → Linux 커널 → init/systemd → 서비스 실행 → 로그인 프롬프트

부팅 단계

  1. BIOS/UEFI: 하드웨어 초기화, 부트 디바이스 선택.
  2. 부트로더 (GRUB): 커널과 initramfs 로드.
  3. Linux 커널: 장치 드라이버 초기화, 루트 파일 시스템 마운트.
  4. init 또는 systemd: 초기화 프로세스 실행, 서비스 시작.
  5. 사용자 로그인: CLI 또는 GUI 환경 제공.

2. 전통적인 SysV init

/etc/inittab

과거 SysV init 환경에서는 /etc/inittab이 부팅 시 실행할 런레벨을 결정했습니다.

id:3:initdefault:

위 예시는 런레벨 3(CLI 멀티유저 모드)으로 부팅한다는 의미입니다.

런레벨 번호

런레벨 의미
0 시스템 종료
1 단일 사용자 모드 (복구 모드)
2 멀티유저, 네트워크 없음
3 멀티유저, CLI 모드
4 예약(사용자 정의)
5 멀티유저, GUI 모드
6 재부팅

/etc/rc.d/rcX.d 구조

각 런레벨에 맞는 서비스 실행 스크립트를 저장하는 디렉토리입니다.

/etc/rc.d/
├── init.d/      # 서비스 제어 스크립트
├── rc0.d/       # 런레벨 0 서비스
├── rc3.d/       # 런레벨 3 서비스
├── rc5.d/       # 런레벨 5 서비스
└── rc.local     # 부팅 마지막에 실행
  • Sxx서비스명 → 실행(start)
  • Kxx서비스명 → 중지(kill)

3. systemd 초기화

타겟(Target) 개념

systemd에서는 런레벨 대신 타겟을 사용합니다.

런레벨 systemd 타겟
0 poweroff.target
1 rescue.target
3 multi-user.target
5 graphical.target
6 reboot.target

유닛(Unit) 종류

종류 설명
service 서비스
target 런레벨 그룹
mount 마운트 포인트
socket 소켓 활성화
timer 예약 실행

실행 순서 (systemd)

systemd → 기본 타겟 실행 → 의존 관계 서비스 실행 → After/Before 조건 처리 → 사용자 환경 진입

4. rc.local

개념

  • 부팅 마지막 단계에서 실행되는 사용자 스크립트
  • SysV init 환경에서는 기본 활성화
  • systemd 환경에서는 기본 비활성

활성화 (systemd)

sudo chmod +x /etc/rc.d/rc.local
sudo systemctl enable rc-local
sudo systemctl start rc-local

5. 런레벨 ↔ 타겟 매핑 표

런레벨 SysV 의미 systemd 타겟  설명
0 종료 poweroff.target 시스템 종료
1 단일 사용자 rescue.target 복구 모드
2 멀티유저(네트워크 없음) multi-user.target 네트워크 없이 CLI
3 멀티유저(CLI) multi-user.target 네트워크 + CLI
4 사용자 정의 (사용자 정의) 예약 용도
5 멀티유저(GUI) graphical.target 네트워크 + GUI
6 재부팅 reboot.target 시스템 재시작

6. 서비스 자동 실행 관리

SysV init

chkconfig --list
chkconfig 서비스명 on
chkconfig 서비스명 off
service 서비스명 start|stop|restart

systemd

systemctl enable 서비스명
systemctl disable 서비스명
systemctl start|stop|restart 서비스명

7. 소스 설치한 Apache 자동 실행 등록

# 유닛 파일 생성
sudo vi /etc/systemd/system/apache-local.service

---
[Unit]
Description=Apache HTTP Server (Local Build)
After=network.target

[Service]
ExecStart=/usr/local/apache2/bin/apachectl -k start -DFOREGROUND
ExecStop=/usr/local/apache2/bin/apachectl -k stop
Type=simple
PIDFile=/usr/local/apache2/logs/httpd.pid

[Install]
WantedBy=multi-user.target
---

# systemd 데몬 갱신
sudo systemctl daemon-reload
# 서비스 등록 & 즉시 실행
sudo systemctl enable -now apache-local.service
# 상태 확인
sudo systemctl status apach-local.service