Computer >> 컴퓨터 >  >> 프로그래밍 >> Bash 프로그래밍

리눅스에서 그룹에 사용자를 추가하는 방법: useradd·groupmems 실전 예제

이 튜토리얼에서는 리눅스에서 그룹에 사용자를 추가하는 방법을 살펴봅니다. Bash 셸 명령어는 특정 목적을 달성할 수 있는 강력한 도구이며, 그룹은 정보를 공유하고 보호하는 효과적인 수단입니다. 리눅스의 파일 권한은 사용자 권한, 그룹 권한, 전체(other) 권한 단위로 소유권을 설정할 수 있게 해주며, root 권한을 체계적으로 관리하는 데에도 유용합니다.

그룹(wheel)과 함께 사용자 추가하기

새로운 관리자가 합류했고, 파일 서버에서 관리자 권한을 부여해야 하는 상황을 가정해 보겠습니다. useradd 명령어는 사용자를 추가하기 위한 간단하고 안정적인 도구로, 사용자를 생성하면서 동시에 특정 그룹에 할당할 수 있습니다.

NAME
       useradd - create a new user or update default new user information

SYNOPSIS
       useradd [options] LOGIN

위 내용은 useradd 매뉴얼 페이지의 기본 정보입니다. 이제 새 사용자인 Admin.Bob과 Admin.Jane을 추가하는 것부터 시작해 보겠습니다.

useradd의 주요 옵션

-c, --comment COMMENT
-G, --groups GROUP1[,GROUP2,...[,GROUPN]]]

-c(코멘트)와 -G(그룹 지정)가 바로 가장 많이 사용되는 두 가지 옵션입니다. 먼저 Bob을 추가해 보겠습니다.

[admin@wsxdn.com ec2-user]# useradd -c "USA/CO/Denver Office" Admin.Bob

이어서 Jane을 추가합니다.

[admin@wsxdn.com ec2-user]# useradd -c "USA/CO/Denver Office" Admin.Jane -G wheel

Bob은 관리자이므로 관리 권한이 필요하다는 것을 알고 있습니다. 하지만 때로는 이를 미처 파악하지 못하거나 단순히 잊어버릴 수도 있습니다. 이런 경우에는 나중에 그룹을 따로 추가해 주어야 합니다.

Jane은 이미 wheel 그룹에 포함되어 있습니다. wheel 그룹은 대부분의 리눅스 시스템에 기본적으로 내장된 그룹으로, 권한 상승(sudo)을 허용하는 기본 그룹입니다.

[admin@wsxdn.com ec2-user]$ sudo su

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for Admin.Jane: 
Sorry, try again.

잊어버린 단계 이야기가 나온 김에, 비밀번호 설정을 빠뜨렸네요. useradd 명령어로 비밀번호를 직접 설정하는 것은 권장하지 않습니다. 대신 반드시 passwd 명령어로 이어서 설정해 주세요. passwd는 히스토리에 남지 않는 안전한 터미널 프롬프트를 통해 입력받으며, 필요하다면 read -sp 옵션을 사용해 인라인 방식으로 비밀번호를 처리할 수도 있습니다.

[admin@wsxdn.com ec2-user]# passwd Admin.Jane
Changing password for user Admin.Jane.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[admin@wsxdn.com ec2-user]# su Admin.Jane
[admin@wsxdn.com ec2-user]$ sudo su

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for Admin.Jane: 
[admin@wsxdn.com ec2-user]#

성공입니다. 사용자 한 명이 wheel 그룹과 함께 추가되었고, 별도의 그룹 관련 명령어는 전혀 필요하지 않았습니다. 만약 처음부터 Jane을 여러 그룹에 추가해야 하는 상황이라면 다음과 같이 처리할 수 있습니다.

[admin@wsxdn.com ec2-user]# useradd Admin.Jane -G wheel,ec2-users
[admin@wsxdn.com ec2-user]# passwd Admin.Jane
[admin@wsxdn.com ec2-user]# su Admin.Jane
[admin@wsxdn.com ec2-user]$ groups
Admin.Jane wheel ec2-users
[admin@wsxdn.com ec2-user]$

확인 결과, Admin.Jane은 자신의 개인 사용자 그룹, sudo 접근을 위한 wheel 그룹, 그리고 ec2-users 그룹에 모두 속해 있습니다.

그렇다면 Bob은 어떻게 될까요? Bob은 wheel 그룹 접근 권한 없이 생성되었기 때문에, 다른 명령어를 사용해야 합니다.

기존 그룹에 사용자 추가하기

Bob을 관리 권한 없이 관리자 계정으로만 추가한 상태입니다. 이제 groupmems 명령어로 이 문제를 해결해 보겠습니다.

groupmems의 주요 옵션

SYNOPSIS
       groupmems -a user_name | -d user_name | [-g group_name] | -l | -p

OPTIONS
       The options which apply to the groupmems command are:

       -a, --add user_name
           Add an user to the group membership list.

           If the /etc/gshadow file exist, and the group has no entry in the /etc/gshadow file, a new entry will be created.

       -d, --delete user_name
           Delete a user from the group membership list.

           If the /etc/gshadow file exist, the user will be removed from the list of members and administrators of the group.

           If the /etc/gshadow file exist, and the group has no entry in the /etc/gshadow file, a new entry will be created.

       -g, --group group_name
           The superuser can specify which group membership list to modify.

       -h, --help
           Display help message and exit.

       -l, --list
           List the group membership list.

먼저 wheel 그룹의 현재 멤버 목록을 확인해 보겠습니다.

[admin@wsxdn.com ec2-user]# getent group wheel
wheel:x:10:ec2-user,samberry,Admin.Steve,Admin.Jane

목록에 Bob이 없는 것을 확인했습니다. 이제 Bob을 추가해 보겠습니다.

[admin@wsxdn.com ec2-user]# groupmems -a Admin.Bob -g wheel
[admin@wsxdn.com ec2-user]# groupmems -g wheel -l
ec2-user  samberry  Admin.Steve  Admin.Jane  Admin.Bob

완료되었습니다. Admin.Bob이 이제 wheel 그룹의 멤버가 되었으며, groupmems -l(--list) 옵션으로 정상 등록 여부를 확인했습니다.

지금까지 살펴본 것이 리눅스 그룹 관리의 기본입니다. 참고로 개인용 리눅스 환경에서는 wheel 대신 adm이 기본 관리자 그룹으로 설정되어 있는 경우가 많습니다.

admin@wsxdn.com:/home/samuelberry# groupmems -g adm -l
syslog  samuelberry

궁금하다면 /etc/sudoers 설정 파일을 열어 자신의 시스템이 어떤 그룹을 사용하는지 직접 확인해 보세요.

admin@wsxdn.com:/home/samuelberry# cat /etc/sudoers
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo	ALL=(ALL:ALL) ALL

[admin@wsxdn.com ec2-user]$ cat /etc/sudoers
## Allows people in group wheel to run all commands
%wheelALL=(ALL)ALL

## Same thing without a password
# %wheelALL=(ALL)NOPASSWD: ALL

눈치채셨겠지만, 이번 예제는 EC2 인스턴스에서 사용자를 추가하는 과정이었습니다. 다음 주에는 사용자/그룹 관리 시리즈의 연장선으로, EC2를 통해 리눅스 인스턴스에 사용자를 추가하는 방법을 자세히 다루겠습니다.