Ubuntu 유저 생성하기

Created
August 26, 2021
Created by
D
DaEun Kim
Tags
Linux
Property

  1. 계정 생성 (-m 옵션은 홈 디렉토리를 만들어준다.)
$ useradd <유저이름> -m 
$ useradd -h
Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]

Options:
      --badnames                do not check for bad names
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
      --btrfs-subvolume-home    use BTRFS subvolume for home directory
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
...
  1. 계정을 사용하려는 사람이 SSH 접속이 가능하도록 공개키를 ~/.ssh/authorized_keys 에 추가한다.
$ mkdir /home/<유저이름>/.ssh
$ touch /home/<유저이름>/.ssh/authorized_keys
  1. 계정에 로그인하면 bash 를 shell prompt 로 사용하도록 변경한다.
$ sudo chsh -s /bin/bash <유저이름>
$ chsh -
Usage: chsh [options] [LOGIN]

Options:
  -h, --help                    display this help message and exit
  -R, --root CHROOT_DIR         directory to chroot into
  -s, --shell SHELL             new login shell for the user account
  1. 사용자를 sudo 그룹에 추가한다.
sudosusu - 의 차이는 여기 를 참고
$ usermod -a -G sudo <유저이름>
  1. 사용자가 비밀번호를 설정할 수 있도록 잠깐 /etc/sudoers 파일을 변경한다.
/etc/sudoersroot를 포함한 사용자들이 어떤 그룹에서 무엇을 할 수 있는지 정의해놓은 곳이다. vi로 직접 수정하지 말고 $ sudo visudo명령어를 통해 열리는 파일 편집기로 수정하길 권장한다.
💡
원래는 useradd 명령어로 사용자를 생성할 때 -p 옵션을 줘서 비밀번호를 설정할 수 있지만 -p 옵션은 사실 상 무용지물이다. 보안 상의 목적으로 암호화된 비밀번호를 -p 옵션에 넘겨줘야 하는데 암호화된 비밀번호를 만들기가 까다롭다. (useradd 로 비밀번호 만들지 말라는 암묵적인 뜻) 그래서 사용자 생성 후에 passwd 명령어로 비번을 설정한다.

아래 내용을 /etc/sudoers 에 추가하면 <유저이름> 의 계정이 비밀번호 입력없이 비밀번호를 변경할 수 있다.

<유저이름> ALL=(ALL) NOPASSWD: /usr/bin/passwd
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# 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
# See sudoers(5) for more information on "#include" directives:


#includedir /etc/sudoers.d
<유저이름> ALL=(ALL) NOPASSWD: /usr/bin/passwd
  1. 사용자에게 SSH 로그인 후 아래 명령어를 통해 새 비밀번호를 입력하도록 안내한다.
$ sudo passwd <유저이름>
  1. 다시 $ sudo visudo 를 실행하여 추가했던 내용을 삭제한다.

[참고자료]

https://www.freecodecamp.org/news/linux-how-to-add-users-and-create-users-with-useradd/

https://askubuntu.com/questions/147241/execute-sudo-without-password

https://www.digitalocean.com/community/tutorials/how-to-edit-the-sudoers-file