序章

パスワードと認証は、Linux環境で作業するときにすべてのユーザーが対処しなければならない概念です。 これらのトピックは、さまざまな構成ファイルとツールにまたがっています。

このガイドでは、「/ etc/passwd」や「/etc/ shadow」などの基本的なファイルと、適切な名前の「passwd」コマンドや「adduser」などの認証を構成するためのツールについて説明します。

これらのトピックについては、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」ファイルの読み方

ファイルの各行には、システム上の1人のユーザーのログイン情報が含まれています。 これらのユーザーの一部は、デーモンおよびバックグラウンドサービスで使用するために作成される場合があります。

1行を見て、そこに含まれる情報を確認してください。

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

情報のフィールドはコロン(:)文字で区切られます。 一般的なLinuxの「/etc/passwd」ファイルの各行には7つのフィールドがあります。

  1. root :アカウントのユーザー名。
  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」ファイルとは異なり、特権のないユーザーには読み取れないことに注意してください。

rootユーザーには読み取りおよび書き込み権限があり、認証に必要なユーザーを含む「シャドウ」グループには読み取り権限があります。

「/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」ファイルと同様に、各行はユーザーの情報を定義し、各フィールドはコロン(:)文字で区切られます。

注:上記の一部の行の2番目のフィールドのアスタリスク(*)値は、アカウントがログインできないことを意味します。 これは主にサービスに使用され、意図された動作です。

もう一度1行を見てみましょう。

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

これらは、「/ etc/shadow」ファイルで定義されているフィールドです。

  1. デーモン:アカウントのユーザー名。
  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:::

結論

これらのシンプルなツールを使用して、システムのログイン情報を変更できます。

変更を加えた後、ログインする能力をテストすることが重要です。 機能とセキュリティを維持するために、認証ファイルのアクセス許可を同じに保つことも不可欠です。

ジャスティン・エリングウッド