chage -l root | grep 'Last password change' | awk -F: '{print $2}'
时间: 2024-06-08 22:09:49 浏览: 137
这个命令的作用是查看 root 用户最后一次修改密码的时间。具体解释如下:
- chage -l root:查看 root 用户的账户信息,其中包括密码修改日期等。
- grep 'Last password change':在查看的信息中筛选出最后一次密码修改的时间。
- awk -F: '{print $2}':以冒号为分隔符,取出第二个字段,也就是最后一次密码修改的时间。
综合起来,这个命令的输出结果为 root 用户最后一次修改密码的时间。
相关问题
#!/bin/bash users=$(cut -d: -f1 /etc/passwd) for user in $users do if [[ $(getent passwd $user | cut -d: -f3) -ge 1000 ]] then if [[ $(chage -l $user | grep "Password expires" | grep "密码过期时间" | cut -d: -f2 | tr -d ' ') == "never" || $(chage -l $user | grep "Password expires" | grep "密码过期时间" | cut -d: -f2 | tr -d ' ') == "从不" ]] then echo "$user: Password never expires" else expire_date=$(date -d "$(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ')" +%s) current_date=$(date +%s) if [[ $expire_date -lt $current_date ]] then echo "$(hostname -I | awk '{print $1}'), $user: Password has expired" else echo "$user: Password will expire on $(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ')" fi fi else account_status=$(passwd -S $user | awk '{print $2}') if [[ "$account_status" == "L" ]]; then echo "$(hostname -I | awk '{print $1}'), User $user: account locked!" fi fi done这段代码去掉筛选非系统用户
可以将第二个if语句中的条件改为:
```
if [[ $(getent passwd $user | cut -d: -f3) -ge 1000 && $(getent passwd $user | cut -d: -f7) != "/sbin/nologin" && $(getent passwd $user | cut -d: -f7) != "/usr/sbin/nologin" ]]
```
这样就可以排除掉系统用户了。
#!/bin/bash users=$(cut -d: -f1 /etc/passwd) for user in $users do if [[ $(getent passwd $user | cut -d: -f3) -ge 1000 ]] then if [[ $(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ') == "never" ]] then echo "$user: Password never expires" else expire_date=$(date -d "$(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ')" +%s) current_date=$(date +%s) if [[ $expire_date -lt $current_date ]] then echo "$(hostname -I | awk '{print $1}'),$user: Password has expired" else echo "$user: Password will expire on $(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ')" fi fi else echo "" account_status=$(passwd -S $user | awk '{print $2}') if [[ "$account_status" == "L" ]]; then echo "$(hostname -I | awk '{print $1}'),User $user: account locked!" fi done语法问题
在第27行缺少一个 `fi` 关键字来结束第20行的 `if` 语句。同时,第24行的 `echo` 命令中,应该把逗号放在双引号内,否则会被视为两个参数。正确的脚本如下:
```
#!/bin/bash
users=$(cut -d: -f1 /etc/passwd)
for user in $users
do
if [[ $(getent passwd $user | cut -d: -f3) -ge 1000 ]]
then
if [[ $(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ') == "never" ]]
then
echo "$user: Password never expires"
else
expire_date=$(date -d "$(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ')" +%s)
current_date=$(date +%s)
if [[ $expire_date -lt $current_date ]]
then
echo "$(hostname -I | awk '{print $1}'), $user: Password has expired"
else
echo "$user: Password will expire on $(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ')"
fi
fi
else
account_status=$(passwd -S $user | awk '{print $2}')
if [[ "$account_status" == "L" ]]; then
echo "$(hostname -I | awk '{print $1}'), User $user: account locked!"
fi
fi
done
```
阅读全文