GNU/Linux account expiration date

Please demonstrate 10 examples of setting an expiration date on a GNU/Linux user's account.

Setting an expiration date on a GNU/Linux user's account can be achieved by modifying the account's attributes using the usermod command or by directly editing the /etc/passwd file. Here are ten examples of how to set an expiration date for a user's account:

  1. Using usermod command to set an expiration date:
sudo usermod --expiredate 2023-06-30 username
  1. Setting an expiration date in seconds since the epoch (Unix timestamp) using usermod:
sudo usermod --expiredate $(date -d '2023-06-30' +%s) username
  1. Setting an expiration date using the chage command:
sudo chage --expiredate 2023-06-30 username
  1. Setting an expiration date in the YYYY-MM-DD format directly in the /etc/passwd file:
sudo nano /etc/passwd

Edit the user's entry and change the last field to the desired expiration date, e.g.:

username:x:1001:1001:User Name:/home/username:/bin/bash:2023-06-30
  1. Setting an expiration date using the passwd command (requires user interaction):
sudo passwd --expire username

The user will be prompted to change their password upon next login.

  1. Setting an expiration date in seconds since the epoch (Unix timestamp) directly in /etc/passwd:
sudo nano /etc/passwd

Edit the user's entry and change the last field to the desired expiration date in seconds since the epoch, e.g.:

username:x:1001:1001:User Name:/home/username:/bin/bash:1757667200
  1. Setting an expiration date using the useradd command (when creating a new user):
sudo useradd -e 2023-06-30 username
  1. Setting an expiration date using the chpasswd command:
echo "username:password" | sudo chpasswd --expire=2023-06-30
  1. Setting an expiration date using the pam_tally2 command:
sudo pam_tally2 --expire=username --inactive=30

This example sets an expiration date for 30 days of inactivity.

  1. Setting an expiration date using the userdel command (to delete the account on a specific date):
sudo userdel --expiredate 2023-06-30 username

Remember to replace username with the actual username you want to set the expiration date for, and adjust the desired expiration date according to your needs.

You should also read: