Set expire date for users

hello
chage command is a useful command for set expire date (suspend user) :

-E switch will update the �Account expires� value as shown below:
chage -E "2009-05-31" username

how can i write this shell script which can find present date and plus it with the value that user set,
like today is 2014-05-31, user enter 60 when script ask hi the expire period,
script must add 60 days to 2014-05-31 and store it in date.txt file

Hello Nimafire,

You can try with following command.

While user creation you can use following command.

useradd -e `date -d "60 days" +"%Y-%m-%d"` someuser

To change user's expiration date who already exist.

chage -E `date -d "60 days" +"%Y-%m-%d"` someuser

Thanks,
R. Singh

1 Like

very god,
ok now how can i list all users from /home and add them to $USERNAMES with loop ?
this script has to list all users from /home with ls /home
chage -l $USERNAMES | grep "Account expires" | cut -d: -f2 2> LIST.txt

---------- Post updated at 11:05 AM ---------- Previous update was at 10:21 AM ----------

checkout this code, the output is not what i want,
echo username + expire date to new dile

echo "`ls /home/`"$'\r' > /root/Desktop/userslist
for i in `cat /root/Desktop/userslist`
do
explist=`chage -l $i | grep "Account expires" | cut -d: -f2`
echo "$i IS EXPIRE IN $explist" > /root/Desktop/LIST
done

---------- Post updated at 12:49 PM ---------- Previous update was at 11:05 AM ----------

chage

send an unknown user error after 3 times, checout these errors
chage: unknown user user4
chage: unknown user nima5

you can run my script in your machine and check it will stop after for or while command run 3 times,

Try this corrected version, which is based on your own attempts:

ls /home > /root/Desktop/userslist

#empty LIST file
>/root/Desktop/LIST

for i in `cat /root/Desktop/userslist`
 do
 explist=`chage -l $i | grep "Account expires" | cut -d: -f2`
 echo "$i IS EXPIRE IN $explist" >> /root/Desktop/LIST
done

If it works, please use the while loop version, which is more appropriate for this task. It does the same as the for loop, but in a "cleaner" way:

ls /home > /root/Desktop/userslist

#empty LIST file
>/root/Desktop/LIST

while read user
 do
 explist=`chage -l $user | grep "Account expires" | cut -d: -f2`
 echo "$user IS EXPIRE IN $explist" >> /root/Desktop/LIST
done < /root/Desktop/userslist
1 Like

is it possible to find first time that user login to system ?
checkout this:
user has been create in 10 september but first login is 20 september

Try the last command... if available on your system.

how can i uss this to find first login time of "user2" for example ?

How about reading the man page? Try last user2 .