Score:3

How to create a user using command line with setting to create password upon first login?

za flag

I am currently trying to set up a computer lab for an educational environment. I am hoping to use a combination of python and bash scripts to implement the users on all of the computers, and I was hoping to have this set so that the students would create their passwords during their first login. I would have assumed this was possible as it is the default setting when creating users through the GUI in settings. I am having issues though.

The command sudo adduser <user> desires a password be created immediately. This can be bypassed by pressing enter multiple times, but that seems like it would be an issue for my script.

The command sudo useradd <user> gets me closer, as I can afterwards use the command sudo passwd -d <user> to set it as a no password account. I can then use the command sudo passwd -e <user> to set the password to be created upon login. However, it appears the useradd command does not create a user directory on the home directory. In addition, the test showing the user and name of the computer is gone when I enter terminal, which concerns me.

in flag
For the home directory: `useradd -m newuser`
Nova72Scotia avatar
za flag
Wonderful! would you happen to know if the username and name of the computer not appearing will be more than a minor nuisance?
in flag
That’s configured as part of the profile, which is created when a home directory is made. If you don’t see it when signing in with account that has a home directory, you will need to define a `.bashrc` file and place it in `/home/{newuser}`
Nova72Scotia avatar
za flag
Ok the ```.bashrc``` file seems to be generating in the directories, still no header for the terminal though.
hr flag
... that's likely because the default login shell specified in the /etc/default/useradd file is `/bin/sh`, rather than `/bin/bash`. Read `man useradd` for options for how to specify the login shell.
Nova72Scotia avatar
za flag
Wonderful! Replacing `SHELL=/bin/sh` with `SHELL=/bin/bash` in `/etc/default/useradd` fixed that issue. I can now remove the password and force the user to create a new one upon login. Thanks so much to all of you!
Score:1
za flag

If you want them to set the password when they first login, then you're not necessarily worried about "security" until they set their password, so create the users with a generic/default password.

then use the chage command on each account. With a script it should be a simple loop.

-d and -m are used with useradd to define and create the user directory. See man useradd for more help.

This command should be able to be used to add the user and password

# useradd -p $(openssl passwd -1 $PASS) $USER

You can add other parameters to that as needed, like the -d for defining a user directory or -m for creating the user directory

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.