0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ユーザーのパスワードの有効期限を一覧表示する

Last updated at Posted at 2019-08-10

はじめに

最近はパスワードの定期変更は不要という流れになっていますが、未だにパスワードの定期的な変更を求められるケースも多々あります。

特にサーバーだと、基本的に複数のユーザーが存在するため、パスワードの定期的な変更を管理するのが面倒になります。
そこで、ログイン可能なユーザーのパスワードの有効期限を一覧表示するスクリプトを作ってみました。

スクリプト

ユーザー名の抽出

  • /etc/passwdの中身を取得した後、ログインシェル(/bin/bash)が指定されている行に絞り込みます。
  • ログインシェルとして/sbin/nologinが指定されている行は、ログインできないユーザーとなります。
  • 「:」より後ろを削除して、ユーザー名だけを抽出します。

パスワードの有効期限の取得

  • chageコマンドを使って、各ユーザーのパスワードの有効期限を取得します。
  • この時、chageコマンドの結果の2行目にパスワードの有効期限が書かれているため、2行目から日付部分だけを抽出します。
  • 地域情報(Locale)を変更した場合、sedコマンドの引数を少し修正する必要があるかもしれません。
pw_expiration.sh
#!/bin/bash# ログイン可能なユーザーを全て取得して、
# 各ユーザーのパスワード有効期限を一覧表示する。
users=$(cat /etc/passwd | grep ':/bin/bash$' | sed s/:.*//)for user in ${users[@]}; doexpiration=$(chage -l $user | sed -n 2p | sed s/.*://)
    echo $user,$expirationdone

実行結果

  • 以下のように、{ユーザー名, パスワード有効期限}の形で一覧表示されます。
  • 「なし」となっているユーザーは、パスワードの有効期限が設定されていません。
[root@akagi scripts]# ./pw_expiration.sh 
root, なし
nkojima, 2月 03, 2020
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?