2009年2月14日土曜日

Linux の シャドウパスワードを移行する

Linux のユーザ管理、"/etc/passwd","/etc/shadow" ついでに "/etc/group""/etc/gshadow" とあります。
これを他のサーバに移行したい場合、そのまま持っていけばOKというお話。
 
 


shadow にはハッシュが書いているだけなので、ハッシュの求め方が同じシステムでも動作するのは当然ということで、折角なのでLinux同士で試してみた。
(※FreeBSDに移動するのもOKらしいね、ファイル名は違うけど)
FreeBSDに関して情報追記:
フィールドの並びも違うよ。あと、BSD はパスワードデータベースファイルを作るのでファイルをコピーした場合、pwd_mkdb(8)なりvipw(8)なりでデータベースも更新する必要があります
by umq さん
なるほどー

 
 


編集用コマンドが用意されてるので一応それ使いましょう


"/etc/passwd","/etc/shadow" を直接 vi で編集すると何かしら事故った時に困るので、専用の編集コマンドがあります。
 

vipw, vigr というのがそれ、使い方は vi と同じなんですけど、同時読み込みとか保存とか気を使ってくれるらしい。デリケートゾーンのお手入れ用?という感覚だろうか。
 

ちょっとその 'man vipw' を。
VIPW(8) BSD System Manager’s Manual VIPW(8)
 
NAME
vipw, vigr - edit the password or group files
 
SYNOPSIS
vipw [-V] [--version]
vigr [-V] [--version]
 
DESCRIPTION
Vipw edits the password file after setting the appropriate locks, and does any necessary processing
after the password file is unlocked. If the password file is already locked for editing by another
user, vipw will ask you to try again later. The default editor for vipw is vi(1).
Vigr edits the group file in the same manner as vipw.
 
ENVIRONMENT
If the following environment variable exists it will be utilized by vipw:
 
EDITOR The editor specified by the string EDITOR will be invoked instead of the default editor
vi(1).
--snip--

適切ロックでヨロシクやってくれる ということだろうか、よかね。
 
"-s" でシャドウ編集となってるのもあるけど、Linux (CentOS5.2で試した) のはオプション不要、 passwd を編集した後に shadow も編集するか聞かれる。
 
 

wassr突っ込み追記
sudoers を編集する visudo(8) もあるんですよ
こちらも umq さん


貼り付けてみよう


まあちょっとやってみよう。
# vipw


とすると、 "/etc/passwd" が開くので、よそから持ってきたユーザ、
sawano, sawano64 の情報をファイル末尾にくっつける、ラストは改行しちゃ(多分)ダメ。

--snip-
sawano:x:635:635::/home/sawano/:/bin/bash
sawano64:x:636:636::/home/sawano64/:/bin/bash

 

さて、"passwd" を編集し終わって保存すると、

You are using shadow passwords on this system.
Would you like to edit /etc/shadow now [y/n]?

と聞かれる、特に編集してない場合は聞かれないので、お試しのときは注意。
 

y を押すと "/etc/shadow" の編集に入るので移行元の shadow を貼り付ける。
sawano:$1${パスワードのハッシュ}.:13872:0:99999:7:::
sawano64:$1${パスワードのハッシュ}.:13872:0:99999:7:::

 

ホームディレクトリは作ってあげないといけないので、mkdir で作って skel を持ってきてあげよう。
と書いて思ったが useradd で作って shadow だけ編集でも十分だったりするがちょっと趣旨と外れるのでやめとこう。
 


ほな移行したユーザでログインしてみよか。
# ssh sawano@localhost
sawano@localhost's password: *******
Last login: ----
-bash-3.2$

 

OKOK。
 
 

ついでに "/etc/shadow" に書かれているものをチェック


shadow ってそのままペタっとしちゃって大丈夫なの?ってちょっと心配だったので、'man shadow' で そもそも shadow に何が書かれているのかを確認した。



--snip--
DESCRIPTION
shadow manipulates the contents of the shadow password file, /etc/shadow. The structure in the
#include file is:
 
struct spwd {
char *sp_namp; /* user login name */
char *sp_pwdp; /* encrypted password */
long int sp_lstchg; /* last password change */
long int sp_min; /* days until change allowed. */
long int sp_max; /* days before change required */
long int sp_warn; /* days warning for expiration */
long int sp_inact; /* days before account inactive */
long int sp_expire; /* date when account expires */
unsigned long int sp_flag; /* reserved for future use */
}
 
The meanings of each field are:
・ sp_namp - pointer to null-terminated user name
・ sp_pwdp - pointer to null-terminated password
・ sp_lstchg - days since Jan 1, 1970 password was last changed
・ sp_min - days before which password may not be changed
・ sp_max - days after which password must be changed
・ sp_warn - days before password is to expire that user is warned of pending password expiration
・ sp_inact - days after password expires that account is considered inactive and disabled
・ sp_expire - days since Jan 1, 1970 when account will be disabled
・ sp_flag - reserved for future use
--snip--


特に保持しなきゃいけない重要な情報はないね、完全にパスワード周りの管理用ファイル。
パスワードのハッシュ以外は特に気にしなくてよいことが確認できた。
 
 

tar でうまく固めて、UID・GID を引き継いだファイルを持ってくればホームディレクトリの再現も簡単かしら。
また今度色々試してみたい。