웹사이트 검색

passwd 및 adduser를 사용하여 Linux VPS에서 암호를 관리하는 방법


소개

암호 및 인증은 모든 사용자가 Linux 환경에서 작업할 때 처리해야 하는 개념입니다. 이러한 항목은 다양한 구성 파일 및 도구에 걸쳐 있습니다.

이 가이드에서는 "/etc/passwd" 및 "/etc/shadow"와 같은 일부 기본 파일과 적절한 이름의 "passwd" 명령 및 \사용자 추가\.

우리는 이러한 주제를 논의하기 위해 Ubuntu 12.04 VPS를 사용할 것이지만 모든 최신 Linux 배포판은 비슷한 방식으로 작동해야 합니다.

/etc/passwd 파일이란 무엇입니까?

우리가 살펴볼 첫 번째 파일인 "/etc/passwd" 파일은 실제로 암호를 저장하지 않습니다.

한때 이 파일은 시스템에 있는 모든 사용자의 해시된 암호를 저장했습니다. 그러나 이 책임은 보안상의 이유로 별도의 파일로 옮겨졌습니다.

\/etc/passwd\ 파일에서 무엇인지 살펴보겠습니다:

less /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
. . .

가장 먼저 주목해야 할 점은 권한이 없는 사용자가 이 파일에 액세스할 수 있다는 것입니다.

시스템의 모든 사용자는 이 파일에 대한 읽기 권한을 가집니다. 이것이 암호 정보가 이 파일에서 이동된 이유입니다.

파일 형식을 살펴보겠습니다.

/etc/passwd 파일을 읽는 방법

파일의 각 행에는 시스템에 있는 단일 사용자의 로그인 정보가 들어 있습니다. 이러한 사용자 중 일부는 데몬 및 백그라운드 서비스에서 사용하기 위해 생성될 수 있습니다.

어떤 정보가 포함되어 있는지 보려면 한 줄을 살펴보십시오.

root:x:0:0:root:/root:/bin/bash

정보 필드는 콜론(:) 문자로 구분됩니다. 일반적인 Linux "/etc/passwd" 파일의 각 행에는 7개의 필드가 있습니다.

  1. root: Account username.
  2. <li><strong>x</strong>: Placeholder for password information.  The password is obtained from the "/etc/shadow" file.</li>
    
    <li><strong>0</strong>: User ID.  Each user has a unique ID that identifies them on the system.  The root user is always referenced by user ID 0.</li>
    
    <li><strong>0</strong>: Group ID.  Each group has a unique group ID.  Each user has a "primary" group that is used as the group by default.  Again, the root group's ID is always 0.</li>
    
    <li><strong>root</strong>: Comment field.  This field can be used to describe the user or user's function.  This can be anything from contact information for the user, to descriptions of the service the account was made for.</li>
    
    <li><strong>/root</strong>: Home directory.  For regular users, this would usually be "/home/<span class="highlight">username</span>".  For root, this is "/root".</li>
    
    <li><strong>/bin/bash</strong>: User shell. This field contains the shell that will be spawned or the command that will be run when the user logs in.</li>	
    

"adduser" 및 "useradd"와 같은 명령을 사용하여 사용자 계정을 추가하거나 더 많은 서비스를 설치하면 이 파일이 커집니다. 새 사용자 이름 정보가 이 파일의 맨 아래에 추가됩니다.

대부분의 경우 이 파일을 직접 편집할 필요가 없습니다. 이 파일을 조작하고 올바른 구문이 유지되도록 하는 도구가 있습니다.

/etc/shadow 파일이란 무엇입니까?

실제 암호 데이터는 "/etc/shadow"라는 파일에 저장됩니다.

이것은 실제로 일반 텍스트의 암호를 포함하지 않습니다. 대신 키 파생 함수를 사용하여 해시를 생성합니다. 이것이 파일에 저장되는 것입니다.

키 파생 함수는 기본적으로 동일한 입력이 주어지면 항상 특정 해시를 생성하는 알고리즘입니다. 인증 중에 제공된 암호에 대해 동일한 알고리즘이 실행되고 이 값이 이 파일의 값과 비교됩니다.

"/etc/passwd" 파일과 달리 이 파일은 권한이 없는 사용자가 읽을 수 없습니다.

루트 사용자는 읽기 및 쓰기 권한이 있고 인증에 필요한 사용자가 포함된 "shadow" 그룹은 읽기 권한이 있습니다.

/etc/shadow 파일을 읽는 방법

다음을 입력하여 "/etc/shadow" 파일을 엽니다.

sudo less /etc/shadow
root:$6$mJD3Rsj4$xUa7jru6EEGTXnhwTfTT26/j8M5XiQvUl6UH32cfAWT/6W9iSI5IuIw5OOw4khwrsOHPyMwfCLyayfYiVdhAq0:15952:0:99999:7:::
daemon:*:15455:0:99999:7:::
bin:*:15455:0:99999:7:::
sys:*:15455:0:99999:7:::
sync:*:15455:0:99999:7:::
games:*:15455:0:99999:7:::
man:*:15455:0:99999:7:::
. . .

"/etc/passwd" 파일과 마찬가지로 각 줄은 사용자 정보를 정의하고 각 필드는 콜론(:) 문자로 구분됩니다.

참고: 위의 일부 행에서 두 번째 필드의 별표(*) 값은 계정이 로그인할 수 없음을 의미합니다. 이것은 주로 서비스에 사용되며 의도된 동작입니다.

한 줄을 다시 살펴보겠습니다.

daemon:*:15455:0:99999:7:::

다음은 "/etc/shadow" 파일에 정의된 필드입니다.

  1. daemon: Account username.
  2. <li><strong>*</strong>: Salt and hashed password.  You can see what this looks like with the root entry above.
    
    	As noted above, the asterisk signifies that this account cannot be used to log in.</li>
    
    <li><strong>15455</strong>: Last password change.  This value is measured in days from the Unix "epoch", which is January 1, 1970.</li>
    
    <li><strong>0</strong>: Days until password change permitted.  0 in this field means there are no restrictions.</li>
    
    <li><strong>99999</strong>: Days until password change required.  99999 means that there is no limit to how long the current password is valid.</li>
    
    <li><strong>7</strong>: Days of warning prior to expiration.  If there is a password change requirement, this will warn the user to change their password this many days in advance.</li>
    
    <li><strong>[blank]</strong>The last three fields are used to denote days before the account is made inactive, days since the Epoch when the account expires.  The last field is unused.</li>	
    

비밀번호는 어떻게 변경합니까?

"passwd" 명령을 실행하여 사용자 암호를 수정할 수 있습니다.

기본적으로 이 명령은 현재 사용자 암호를 변경하며 특별한 권한이 필요하지 않습니다.

passwd

다른 사용자의 비밀번호를 변경하려면 관리 권한이 필요합니다. 다음 구문을 사용할 수 있습니다.

sudo passwd username

"sudo" 명령에 대한 암호를 입력하라는 메시지가 표시되면 사용하려는 새 암호를 입력하고 확인하라는 메시지가 표시됩니다.

"/etc/shadow" 파일에서 해시된 값을 비교하면 passwd 명령을 실행한 후 변경되는 것을 볼 수 있습니다.

새 사용자를 어떻게 생성합니까?

몇 가지 다른 명령을 사용하여 사용자를 생성할 수 있습니다.

가장 쉬운 방법은 "adduser" 명령을 사용하는 것입니다. 여기에서 다룰 것입니다. Ubuntu 시스템에서 이것은 적절한 사용자 생성을 처리하는 perl 스크립트에 연결됩니다.

다음과 같이 호출할 수 있습니다.

adduser demo
Adding user `demo' ...
Adding new group `demo' (1000) ...
Adding new user `demo' (1000) with group `demo' ...
Creating home directory `/home/demo' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for demo
Enter the new value, or press ENTER for the default
	Full Name []: test
	Room Number []: room
	Work Phone []: work phone
	Home Phone []: home phone
	Other []: other
Is the information correct? [Y/n]

"/etc/passwd" 파일 및 "/etc/shadow" 파일에 정보를 입력하는 데 도움이 되는 일련의 질문을 받게 됩니다.

다음을 입력하면 "/etc/passwd" 파일에 어떤 항목이 추가되었는지 확인할 수 있습니다.

tail -1 /etc/passwd
demo:x:1000:1000:test,room,work phoneme phone,other:/home/demo:/bin/bash

이것이 주석 필드를 잘 활용한다는 것을 알 수 있습니다. 다른 필드는 예상대로 채워집니다.

유사한 명령을 실행하여 "/etc/shadow" 파일에 대한 수정 사항을 확인할 수 있습니다.

sudo tail -1 /etc/shadow
demo:$6$XvPCmWr4$HXWmaGSeU5SrKwK2ouAjc68SxbJgUQkQ.Fco9eTOex8232S7weBfr/CMHQkullQRLyJtCAD6rw5TVOXk39NAo/:15952:0:99999:7:::

결론

이러한 간단한 도구를 사용하여 시스템의 로그인 정보를 변경할 수 있습니다.

변경 후에는 로그인 기능을 테스트하는 것이 중요합니다. 또한 기능과 보안을 유지하려면 인증 파일에 대한 권한을 동일하게 유지하는 것이 중요합니다.