![sudoers 파일의 중복 줄이기](https://linux55.com/image/223302/sudoers%20%ED%8C%8C%EC%9D%BC%EC%9D%98%20%EC%A4%91%EB%B3%B5%20%EC%A4%84%EC%9D%B4%EA%B8%B0.png)
사용자가 비밀번호 없이 sudo 명령을 실행할 수 있게 해주는 sudoers 파일이 있습니다(주로 특정 서비스를 관리하기 위해). 전체 구성의 중복을 대폭 줄이고 싶습니다.
간단한 버전은 다음과 같습니다.
Cmd_Alias NGINX = \
/bin/systemctl start nginx, \
/bin/systemctl stop nginx, \
/bin/systemctl restart nginx, \
/bin/systemctl reload nginx, \
/bin/systemctl status nginx
# glob php version, to save this breaking on version changes
Cmd_Alias PHP = \
/bin/systemctl start php[0-9].[0-9]-fpm, \
/bin/systemctl stop php[0-9].[0-9]-fpm, \
/bin/systemctl restart php[0-9].[0-9]-fpm, \
/bin/systemctl reload php[0-9].[0-9]-fpm, \
/bin/systemctl status php[0-9].[0-9]-fpm
...
username ALL = NOPASSWD: NGINX
username ALL = NOPASSWD: PHP
...
실제 버전에는 지정된 서비스가 여러 개 있으며 모두 동일한 형식을 따릅니다. 따라서 이상적으로 더 나은 접근 방식은 다음과 같습니다.
Cmd_Alias SERVICES = \
/bin/systemctl {start,stop,restart,reload,status} {nginx,php[0-9].[0-9]-fpm,foo,bar,alice,bob}
username ALL = NOPASSWD: SERVICES
하지만 버팀대 확장은 허용되지 않는 것 같습니다. 이스케이프 여부에 관계없이 쉼표에 대한 구문 오류가 발생합니다.
그래서 확인해보니 man sudoers
와일드카드/기본 형태의 와일드카드가 가능하다는 것을 언급했습니다(위에서 언급한 것처럼 PHP 버전의 와일드카드에서 완벽하게 작동합니다).
Wildcards
sudo allows shell-style wildcards (aka meta or glob characters) to be used in host names, path names, and command line arguments in
the sudoers file. Wildcard matching is done via the glob(3) and fnmatch(3) functions as specified by IEEE Std 1003.1 (“POSIX.1”).
* Matches any set of zero or more characters (including white space).
? Matches any single character (including white space).
[...] Matches any character in the specified range.
[!...] Matches any character not in the specified range.
\x For any character ‘x’, evaluates to ‘x’. This is used to escape special characters such as: ‘*’, ‘?’, ‘[’, and ‘]’.
하지만 중복을 줄이는 다른 방법이 있을까요? 어쩌면 변수일까요? 내가 모르는 Alias 방식?
사용자 입력을 기대하고 원하는 결과를 얻는 것이 더 좋습니다. 즉, sudo systemctl status whatever
사용자 입력을 스크립트로 구문 분석하는 것보다 좋습니다.