For decades, periodic password changes have been a staple of system security. The idea is that if someone gets your password, they can gain access for a limited period of time. It's usually easiest to have the system prompt them rather than relying on users to remember to change their passwords. However, regular user accounts may be set with passwords that never expire and therefore never prompt users to change them.
The Bash script in this article lists all those regular user accounts on your system whose password is set to never expire. By regular users, I mean accounts that usually have interactive shell access and a /home
directory.
[ Learn more about Forcing Linux system password changes with the chage command. ]
For this example, you can use the following /etc/passwd
file with the regular users Alice, Bob, and Charlie:
[root@f3f6383512d0 /]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
tss:x:59:59:Account used for TPM access:/dev/null:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-resolve:x:193:193:systemd Resolver:/:/sbin/nologin
alice:x:1000:1000::/home/alice:/bin/bash
bob:x:1001:1001::/home/bob:/bin/bash
charlie:x:1002:1002::/home/charlie:/bin/bash
As you can see, these regular users have a home directory in /home
. Before kicking off the Bash script, check the three user accounts using chage
(1):
[root@f3f6383512d0 /]# for USER in alice bob charlie; do echo $USER; chage -l $USER; done
alice
Last password change : Aug 04, 2021
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
bob
Last password change : Aug 04, 2021
Password expires : Nov 02, 2021
Password inactive : never
Account expires : never
Minimum number of days between password change : 1
Maximum number of days between password change : 90
Number of days of warning before password expires : 7
charlie
Last password change : Aug 04, 2021
Password expires : never
Password inactive : never
Account expires : never
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
Only Bob's password is going to expire at some point in time. The passwords of Alice and Charlie never will. But how do you display all user accounts whose passwords will never expire without having to check each individual account? As mentioned earlier, all of your regular user accounts have a home directory in /home
. You can use this fact to filter them from /etc/passwd
. Let's take a look at the Bash script:
#!/bin/bash
for USER in $(grep home /etc/passwd | cut -d':' -f1)
do
if [ "$(chage -l $USER | grep 'Password expires' | cut -d':' -f2)" == ' never' ]
then
echo $USER
fi
done
The file /etc/passwd
consists of several columns separated by :
, with the first column of each line containing the username. For each line containing the string home, the script extracts the username and stores it in the variable USER. This finds all regular user accounts. Next, it runs the command chage
on these user accounts, and if the attribute Password expires is set to never, the username is output to STDOUT.
That wasn't too hard, was it? And in case you need to do this on an interactive shell, you don't need to write a script at all but could use a Bash one-liner instead:
[root@f3f6383512d0 /]# for USER in $(grep home /etc/passwd | cut -d':' -f1);do if [ "$(chage -l $USER | grep 'Password expires' | cut -d':' -f2)" == ' never' ]; then echo $USER;fi;done
alice
charlie
[root@f3f6383512d0 /]#
And that's the beauty of Bash.
Wrapping up
Passwords that don't expire may be considered a security flaw and violate your organization's security policies. It's straightforward to audit for password settings using a Bash script like the one in this article, which outputs a list of regular user accounts whose passwords will never expire. This Bash script utilizes some of the most common GNU tools, such as chage
(1), cut
(1), echo
(1), and grep
(1).
The Bash script uses typical constructs like a for
loop and the if-then
condition to get the job done. It's a powerful tool that every sysadmin should know.
About the author
Jörg has been a Sysadmin for over ten years now. His fields of operation include Virtualization (VMware), Linux System Administration and Automation (RHEL), Firewalling (Forcepoint), and Loadbalancing (F5). He is a member of the Red Hat Accelerators Community and author of his personal blog at https://www.my-it-brain.de.
More like this
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech
Products
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud services
- See all products
Tools
- Training and certification
- My account
- Customer support
- Developer resources
- Find a partner
- Red Hat Ecosystem Catalog
- Red Hat value calculator
- Documentation
Try, buy, & sell
Communicate
About Red Hat
We’re the world’s leading provider of enterprise open source solutions—including Linux, cloud, container, and Kubernetes. We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.
Select a language
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit