[Linux] Amazon Linux 2023 rc.local 활성화
Amazon Linux 2023에서 /etc/rc.d/rc.local 파일 생성, 실행 권한과 SELinux Context 설정으로 rc-local 서비스를 활성화하는 방법을 정리합니다.
요약
- Amazon Linux 2023에서는
/etc/rc.d/rc.local을 만들고 실행 권한을 주면rc-local서비스로 부팅 스크립트를 실행할 수 있습니다. - 기존
/etc/rc.local이 아니라/etc/rc.d/rc.local이 존재하고 실행 가능할 때 systemd-rc-local-generator가 rc-local.service를 부팅 과정에 추가합니다. - SELinux를 사용한다면
chcon -t initrc_exec_t로 Context를 추가해야 하며,systemctl status rc-local로 실행 결과를 확인합니다.
테스트 환경 Amazon Linux 2023
[Linux] Amazon Linux 2023 rc.local 활성화
Amazon Linux 2023 에서 rc.local 기능을 사용하기 위해서는 아래와 같은 방법을 이용하면 사용이 가능합니다.
(1) /etc/rc.d/rc.local 파일을 생성합니다.
man page 를 확인하면 아래와 같이 기존에는 /etc/rc.local 이였지만 지금은 /etc/rc.d/rc.local 으로 변경된 것을 확인 할 수 있습니다.
$ man systemd-rc-local-generator
DESCRIPTION
systemd-rc-local-generator is a generator that checks whether /etc/rc.d/rc.local exists
and is executable, and if it is, pulls the rc-local.service unit into the boot process.
...
$ sudo vi /etc/rc.d/rc.local
(2) /etc/rc.d/rc.local 에 스크립트 내용을 추가합니다.
아래는 rc.local 에 대한 예제입니다.
#!/bin/bash
printf "rc-local for test." | logger -t test-rc-locald
printf "rc-local for test." | logger -t test-rc-locald
printf "rc-local for test." | logger -t test-rc-locald
(3) /etc/rc.d/rc.local 에 실행 권한을 추가합니다.
$ sudo chmod u+x /etc/rc.d/rc.local
(4) SELinux 를 사용한다면 아래 명령어로 Context 를 추가합니다.
$ sudo chcon -t initrc_exec_t /etc/rc.d/rc.local
(5) rc.local daemon 을 기동하고 rc.local 이 정상적으로 수행이 되는지 확인합니다.
$ sudo systemctl start rc-local
(6) 실행 예제
$ systemctl status rc-local
● rc-local.service - /etc/rc.d/rc.local Compatibility
Loaded: loaded (/usr/lib/systemd/system/rc-local.service; static)
Active: active (exited) since Wed 2023-10-11 00:00:09 UTC; 3s ago
Docs: man:systemd-rc-local-generator(8)
Process: 2548 ExecStart=/etc/rc.d/rc.local start (code=exited, status=0/SUCCESS)
CPU: 8ms
Oct 11 00:00:09 chhanz.compute systemd[1]: Starting rc-local.service - /etc/rc.d/rc.local Compatibility...
Oct 11 00:00:09 chhanz.compute test-rc-locald[2550]: rc-local for test. <<<---!!
Oct 11 00:00:09 chhanz.compute test-rc-locald[2552]: rc-local for test. <<<---!!
Oct 11 00:00:09 chhanz.compute test-rc-locald[2554]: rc-local for test. <<<---!!
Oct 11 00:00:09 chhanz.compute systemd[1]: Started rc-local.service - /etc/rc.d/rc.local Compatibility.
참고 자료
자주 묻는 질문
Amazon Linux 2023에서 rc.local 파일은 어느 경로에 만들어야 하나요?
/etc/rc.d/rc.local에 만들어야 합니다. man systemd-rc-local-generator에 따르면 이 파일이 존재하고 실행 가능할 때 rc-local.service가 부팅 과정에 포함됩니다.
rc.local을 만들었는데 실행되지 않으면 무엇을 확인해야 하나요?
sudo chmod u+x /etc/rc.d/rc.local로 실행 권한을 줬는지 확인합니다. SELinux를 사용 중이라면 sudo chcon -t initrc_exec_t /etc/rc.d/rc.local로 Context도 추가해야 합니다.
rc.local이 정상 동작하는지 어떻게 확인하나요?
sudo systemctl start rc-local로 서비스를 기동한 뒤 systemctl status rc-local에서 Active 상태와 스크립트가 남긴 로그를 확인합니다.