3 min read

우분투 패스워드 없는 계정 만들기

리눅스에서 계정 생성시 패스워드를 입력하게 된다. 그런데 남에게 계정을 만들어서 접속 정보를 넘겨줘야 한다면 썩 내키는 방식은 아니다. 첫 로그인을 할 때 패스워드를 직접 입력하도록 해 보자. 당연히 암호 없는 계정이므로 첫 로그인은 공개키 인증을 사용해야 한다.

우선 일반적인 방법으로 사용자 계정을 생성한다. 이 때 입력하는 암호는 임시로 쓸 암호로, 나중에 지울거다.

# adduser nopassuser
Adding user `nopassuser' ...
Adding new group `nopassuser' (1001) ...
Adding new user `nopassuser' (1001) with group `nopassuser' ...
Creating home directory `/home/nopassuser' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for nopassuser
Enter the new value, or press ENTER for the default
        Full Name []: No Password
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n] y

이제 해당 유저로 로그인 해서 키 쌍을 생성하고, 공개키 방식으로 로그인 할 수 있도록  authorized_keys 파일도 만들어 준다. 왜인지 모르겠지만 윈도우에서 openssh 포맷 인식이 잘 안 되어, pem 포맷을 사용하였다.

# su -l nopassuser
nopassuser@host$ ssh-keygen -t rsa -m pem
Generating public/private rsa key pair.
Enter file in which to save the key (/home/nopassuser/.ssh/id_rsa):
Created directory '/home/nopassuser/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/nopassuser/.ssh/id_rsa
Your public key has been saved in /home/nopassuser/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:~~~~ nopassuser@host
The key's randomart image is:
+---[RSA 3072]----+
|.................|
+----[SHA256]-----+
nopassuser@host$ cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys

이제 개인키 파일(~/.ssh/id_rsa)을 적당히 안전한 장소에 복사해 둔다. 패스워드 날리고 나면 그 유저는 잠긴 상태가 되고, 비밀번호를 재설정 해야 다시 사용할 수 있다. 뭐 루트 권한이 있는데 못 볼 것도 없긴 하다.

이제 로그아웃 하고, 해당 유저의 패스워드를 날리고, 패스워드 만료 설정을 해 준다.

nopassuser@host$ logout
# passwd --delete --expire nopassuser
passwd: password expiry information changed.

이제 해당 사용자는 패스워드가 없으므로(--delete) 패스워드 인증으로는 로그인 불가하며, 공개키 인증으로만 로그인할 수 있다. 또한 패스워드가 만료되었으므로(--expire) 다음 번 로그인 할 때 반드시 암호를 변경해야 한다. 이제 개인키 파일을 계정 사용자에게 전달해 주면 된다.

이렇게 설정된 계정에 로그인 하면 어떤 일이 벌어질까? 직접 해 보자.

$ ssh -i id_rsa nopassuser@host
You are required to change your password immediately (administrator enforced)
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-62-generic x86_64)

 * Documentation:  <https://help.ubuntu.com>
 * Management:     <https://landscape.canonical.com>
 * Support:        <https://ubuntu.com/advantage>

8 updates can be installed immediately.
0 of these updates are security updates.
To see these additional updates run: apt list --upgradable

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

WARNING: Your password has expired.
You must change your password now and login again!
New password:
Retype new password:
passwd: password updated successfully
Connection to host closed.

패스워드가 만료되었으니 강제로 패스워드 설정이 진행되며, 기존 패스워드가 없으므로 새 패스워드만 물어본다. 강제로 접속이 끊어지지만 다시 접속하면 된다.