시스템 운영 업무를 하다보면, 기존 세팅되어있는 서버의 관리뿐만 아니라 신규 서버를 생성하여 기본 사용자 계정, 권한 등을 세팅하는 방법은 기본적으로 알고 있어야 하는 상식이다.
그럼에도 이와 관련한 작업 요청이 왔을 때, 머리속에 완벽히 정리가 되어있지 않아 구글링을 하는 스스로를 발견하고 일련의 모든 것을 정리해보았다.
username:password = sysadm:Ywopsx1! 로 세팅을 하고, sysadm 사용자가 sudo 명령어를 사용할 수 있도록 설정해보자.
사용자 계정 생성 및 패스워드 설정
[ec2-user@ip-XXX-XX-XXX-XXX home]$ sudo useradd sysadm
[ec2-user@ip-XXX-XX-XXX-XXX home]$ sudo passwd sysadm
sudo passwd {username} 입력 후, New password: 에 Ywopsx1! 을 입력하여 설정하면 된다.
아래는 초기 세팅이 아니라 Changing password 라는 문구가 나오며, 패스워드 수정 또한 할 수 있다.
사용자를 wheel 그룹에 추가
이 단계는 사용자에게 sudo 권한을 부여하기 위한 설정이다.
wheel 그룹? 시스템 관리 권한을 가진 사용자 그룹이며, wheel 그룹에 속한 사용자들은 sudo 명령을 사용하여 슈퍼유저 (root) 권한으로 명령을 실행 가능하다.
먼저 해당 설정을 완료하면 확인할 수 있는 결과부터 보자.
[ec2-user@ip-XXX-XX-XXX-XXX home]$ groups sysadm
[ec2-user@ip-XXX-XX-XXX-XXX home]$ id sysadm
각 cmd 에서 sysadm 사용자가 sysadm, wheel 그룹에 속해있음을 확인가능하다.
1. 그룹 생성 (필요한 경우)
sysadm, wheel 그룹이 존재하는지 확인하고, 존재하지 않으면 (초기세팅의 경우) 생성
[ec2-user@ip-XXX-XX-XXX-XXX home]$ sudo groupadd sysadm
[ec2-user@ip-XXX-XX-XXX-XXX home]$ sudo groupadd wheel
2. 사용자를 그룹에 추가
sysadm, wheel 그룹 둘 다에 sysadm 사용자 추가
[ec2-user@ip-XXX-XX-XXX-XXX home]$ sudo usermod -aG sysadm,wheel sysadm
* sysadm 사용자를 생성하기 전이라면, 사용자 생성과 동시에 그룹까지 설정하는 명령어
[ec2-user@ip-XXX-XX-XXX-XXX home]$ sudo useradd -m -G sysadm,wheel sysadm
3. 설정 확인
sysadm 이 속한 그룹 목록 출력 (앞서 확인한 내용)
[ec2-user@ip-XXX-XX-XXX-XXX home]$ groups sysadm
'/etc/sudoers' 파일 수정
wheel 그룹 설정까지 완료한 사용자도, sudo 명령어로 루트 권한 명령을 실행하기 위해서는 /etc/sudoers 파일을 참조하여 권한을 검사한다. 따라서, wheel 그룹 추가 후 sudoers 설정까지 확인 및 수정은 필수적이다.
[ec2-user@ip-XXX-XX-XXX-XXX home]$ sudo visudo
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL # 해당 라인 주석처리 되어있거나 없으면, 해제 및 추가
# wheel 그룹의 모든 사용자의 sudo 명령 실행 허용
vi /etc/sudoers 가 아니라, sudo visudo 명령어를 사용한다.
해당 명령어를 사용하면 문법 오류를 방지하며, 안전하게 편집이 가능하다.
설정 완료 확인
* sudoers 파일은 root 권한이 아니면 확인할 수 없다. (Permission denied 확인)
세팅한 sysadm 사용자 및 패스워드로 sysadm 먼저 접속, wheel 그룹에 추가된 sysadm 의 패스워드로 root 권한 접근 가능
기본적인 지식은 헷갈리지 않도록 확실한 개념을 인지하고, 잊혀질 때 쯤 상기할 수 있도록 기록하는 습관을 들이자.
이번 포스팅에서 파생하여, 생성한 사용자 계정으로 로컬에서 SSH 접속을 가능하게 하기 위한 설정과 관련하여 다음 포스팅을 하겠다.