RHEL Help Red Hat Enterprise Linux Tutorials


11
Aug/09
1

Add A User Via Command Line

Adding a user in RHEL or CentOS can easily be done using the system-config-users gui tool. However, sometimes you need to add users via the command line. This can be helpful when adding a lot of users at once or for scripting.

To add a user via command line, use the useradd command. The example below uses the useradd command to add the user joe to the local machine:

/usr/sbin/useradd -u 5000 -g users -G wheel -d /home/joe \
-c "Joe's Account" -p '$1$826q9/$mBqVr.FxKdLkWr7a0OEAi0' joe

Here's a quick breakdown of the command:

  • -u sets the user's unique identification number (UID)
  • -g sets the user's primary group
  • -G adds the user to additional groups
  • -d set's the user's home directory
  • -c set's the account description
  • -p set's the user's password using an encrypted password hash

To create the password hash for the -p option, use the grub-md5-crypt command:

/sbin/grub-md5-crypt

It will ask you to type the new password twice and then output the encrypted hash:

$1$826q9/$mBqVr.FxKdLkWr7a0OEAi0

Once you have the password hash, you can include it in the above useradd command.

It's worth noting that you can set the user's password after account creation using the passwd command:

/usr/bin/passwd joe