disqus4u Latest Questions

How can I create a temporary Linux User account?


Is it possible to create a temporary Linux User account that can expire after a specified period?

Related Questions

  1. We use the chage command to create a temporary Linux user. This command specifically sets the user password to expire based on the expiry date or the number of days you’ve set it to expire.

    Start by creating a user on Linux using the useradd command:

     sudo useradd temporaryuser

    In this example, I am calling my user, temporaryuser but you can name it whatever you want for your case.

    We can set the password for the user using the passwd command:

    passwd temporaryuser

    Change temporaryuser with the actual username of your choice. You will be prompted to type in your password.

    It’s important to note that while typing in the password, it won’t be visible on the terminal.

    Finally, we need to set the password expiration using the chage command:

    sudo chage -E YYYY-MM-DD temporaryuser

    The -E flag or parameter is what we use to set the expiration date for the user password. Make sure that the date format is represented as YYYY-MM-DD


    If you want the account to expire after a certain number of days starting from today, here is the command you can use:

     sudo chage -E $(date -d "+X days" "+%Y-%m-%d") temporaryuser

    From the above command, replace x with the number of days you want the account password to expire from today. Let’s assume you want the account to expire after 30 days from now, here is the command you can use:

     sudo chage -E $(date -d "+30 days" "+%Y-%m-%d") temporaryuser

    You will have to replace temporaryuser with the name of the actual user you want to expire.


    To verify if the account expiration period, run the following command:

    sudo chage -l temporaryuser

    Here is the sample output you will see on your terminal:

    Last password change : Jan 16, 2024
    Password expires : never
    Password inactive : never
    Account expires : Dec 22, 2024
    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

    It’s also important to note that once the user expires, it won’t be deleted but the password will stop working because of the expiry parameter we’ve set.

    If you want to read more about creating temporary users on Linux, I recommend this article for you to get started

Leave an answer

Leave an answer