1. Overview
Sometimes, we need to create a new user account that expires after a while. In this tutorial, we’ll discuss different ways of creating a temporary user in Linux.
2. Using useradd
We can create a new user using the built-in useradd command. The useradd command supports the -e or –expiredate option to set an expiration date for the user.
Let’s create a user called Baeldung and set an expiration date for that user using the -e option:
$ sudo useradd Baeldung -e 2023-06-05
$
Notably, there’s no output by default and useradd creates a user with the specified expiration date. Also, we can notice that the expiration date is in the YYYY-MM-DD format.
3. Can We Create a Temporary User Account Using adduser?
The useradd command, which we saw in the previous section, is a low-level built-in utility that can only create a user. But after creating a new user we often want to set a password, create a home directory for the user, create and assign a new group, and do some other initializations. In such a case, a lot of popular Linux distributions recommend using the adduser command instead.
Let’s create a user called Baeldung with adduser:
$ sudo adduser Baeldung
Adding user `Baeldung' ...
Adding new group `Baeldung' (1001) ...
Adding new user `Baeldung' (1001) with group `Baeldung' ...
Creating home directory `/home/Baeldung' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for Baeldung
Enter the new value, or press ENTER for the default
Full Name []: Baeldung user
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
As we can see, this utility does some usual steps for us, but it doesn’t support the -e or –expiredate option. So, after the creation of the user, we must set an expiration date with another command. In the next section, we’ll learn how to set or change an existing user expiration date.
4. Changing Expiration Date for Existing User Using usermod
In the Linux command line, we can use usermod to change a lot of properties of an existing user. So, we can use this command to change the user expiration date. For this purpose, we must use the -e option similar to what we did with useradd:
$ sudo usermod Baeldung -e 2023-06-05
$
5. Creating Short-Time Users Using usermod –lock Option
Till now, we were talking about setting an expiration date directly with commands. But there’s a limitation in setting an expiration date in that way. The expiration date format only includes the date (and not the time). So, after creating a user, the user will be active at least for a day. Therefore, if we want to create a user who’s active for some hours, minutes, or seconds we can use && operator in Linux to create a user, wait a while, and then lock the user.
Let’s create a user named Baeldung (e.g. using useradd or adduser) and lock it after 20 minutes (1200 seconds):
$ sudo adduser Baeldung && sleep 1200 && sudo usermod --lock Baeldung
To lock the user, we used the –lock option of usermod. The –lock option can be used to lock the user after a while or after any specific condition happens.
It’s worth noting that these commands will be run in the foreground and that will cause the current terminal to be locked for 20 minutes (because of the sleep command). So it’s better to do some tricks, such as using the at command, to do it better:
$ sudo adduser Baeldung
$ echo "usermod --lock Baeldung" | at now + 20 minutes
6. Conclusion
In this article, we’ve discussed ways of creating an account with an expiration date. First, we used the useradd with the -e option. After that, we talked about the adduser command with usermod to set the expiration date for a user. Finally, we looked at a way to create users with an expiration date of less than one day.