Dev./Basic knowledge

[Tip] 인스턴스 권한 자동설정 (feat. docker)

Ivan'show 2023. 8. 31.
728x90
반응형

클라우드 플랫폼을 통해서 인스턴스를 생성하면 인스턴스에 root 계정과, init script 로 만든 유저가 하나 생성된다.

# init script sample

#!/bin/bash

# set variables
NEW_USER_ID="lion"
PASSWORD="abcd"

# user creations
echo "Creating new user"
useradd -s /bin/bash -d /home/$NEW_USER_ID -m $NEW_USER_ID

# password change
echo "Set password"
echo "$NEW_USER_ID:$PASSWORD" | chpasswd

# user auth
echo "Update authorization"
echo "$NEW_USER_ID ALL=(ALL:ALL) NOPASSWD:ALL" >> /etc/sudoers.d/$NEW_USER_ID

# check user auth
echo "Check user auth"
cat /etc/sudoers.d/$NEW_USER_ID

권한 부여 명령어

# echo 를 이용해서 sudoer.d 경로에 참고 정보 추가하여 권한 설정
echo "${user_name} ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers.d/${user_name}
# usermod 커맨드를 이용해서 권한 추가
sudo usermod -aG sudo ${user_name}

그러나 실제로 ssh 를 이용해서 접속해보면 권한부여가 실제로는 잘 적용되지 않은 경우나 docker 그룹과 같은 다른 그룹에서 sudo 권한을 사용하지 못할 때가 있다.

~$ docker images
Got permission denied while trying to connect to the Docker
...
connect: permission denied

이럴 때는 usermod 명령어를 이용해서 사용자권한을 등록해주고 해당 서비스를 재시작하면 해결된다.

# auth command
sudo usermod -aG docker lion
# restart service
sudo service docker restart

usermod: 사용자 계정관리 명령어

# usermod command detail

$ sudo usermod --help
Usage: usermod [options] LOGIN

Options:
  -b, --badnames                allow bad names
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS
  -a, --append                  append the user to the supplemental GROUPS
                                mentioned by the -G option without removing
                                the user from other groups
  -h, --help                    display this help message and exit
  -l, --login NEW_LOGIN         new value of the login name
  -L, --lock                    lock the user account
  -m, --move-home               move contents of the home directory to the
                                new location (use only with -d)
  -o, --non-unique              allow using duplicate (non-unique) UID
  -p, --password PASSWORD       use encrypted password for the new password
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -s, --shell SHELL             new login shell for the user account
  -u, --uid UID                 new UID for the user account
  -U, --unlock                  unlock the user account
  -v, --add-subuids FIRST-LAST  add range of subordinate uids
  -V, --del-subuids FIRST-LAST  remove range of subordinate uids
  -w, --add-subgids FIRST-LAST  add range of subordinate gids
  -W, --del-subgids FIRST-LAST  remove range of subordinate gids
  -Z, --selinux-user SEUSER     new SELinux user mapping for the user account

scp 로 파일 보내기

scp %{file_name} ${file_name2} ${user_name}@${server_address}:${PATH}
728x90
반응형

'Dev. > Basic knowledge' 카테고리의 다른 글

Dev: How a CDN improves scaling  (0) 2023.11.03
Dev: 네트워킹 in Cloud  (0) 2023.11.02
Dev: Self-hosted, PaaS, SaaS and DBaaS  (0) 2023.11.01
[Tip] DB server: Postgres:13 자동설정  (0) 2023.09.01
[CS] Boolean, Logic Gate, Binary  (0) 2023.06.07

댓글