2014年7月1日火曜日

Linuxユーザ一覧取得|/etc/passwdファイルを分割

設定書にユーザ一覧を記載するときに便利
エクセルでこんな感じにしたいとき向け

nameUIDGIDcommenthomeshel
root00root/root/bin/bash
hoge00hoge/hoge/bin/bash

# cd /tmp
# mkdir userlist
# cd userlist
# touch userlist.sh
# chmod 755 userlist.sh
# vi userlist.sh

cut -d : -f1 /etc/passwd > name.txt
cut -d : -f3 /etc/passwd > uid.txt
cut -d : -f4 /etc/passwd > gid.txt
cut -d : -f5 /etc/passwd > comment.txt
cut -d : -f6 /etc/passwd > home.txt
cut -d : -f7 /etc/passwd > shel.txt

# ./userlist.sh

必要に応じて
# tar cvf userlist.tar .
でホストOSに移動するなりなんなりして
各.txtをファイルを開いてコピーしてエクセルに貼り付け



ちなみにグループ一覧は
cut -d : -f1 /etc/group > name.txt
cut -d : -f3 /etc/group > gid.txt
cut -d : -f4 /etc/group > users.txt
こんな感じで





【一括実行】以下コピペ後vi起動したらuserlist.shの内容コピペして:wqするとtar化までやってくれる

cd /tmp ; mkdir userlist ; cd userlist ; touch userlist.sh ; chmod 755 userlist.sh ; vi userlist.sh ; ./userlist.sh ; tar cvf userlist.tar .


以上