Creating a new Unix username at the command line

To create a new Unix username, you use the useradd command as the root user or with sudo privileges. The basic syntax is sudo useradd username, where you replace "username" with the actual name you want. Unix usernames are case-sensitive, so "james" and "James" are two different accounts.

Most systems also require you to set a password for the new user when ready after creation. Run sudo passwd username and enter the password twice when prompted. Without a password, the account exists but cannot be used to log in.

If you want to create a home directory for the user at the same time, add the -m flag: sudo useradd -m username. This creates a folder under /home/ where the user can store files and configuration settings.

Key Takeaways

  • The command sudo useradd -m username creates a new user account with a home directory in one step.
  • You must set a password with sudo passwd username before the account can be used to log in.
  • Unix usernames are case-sensitive and typically use lowercase letters, numbers, and underscores only.
  • You need root or sudo access to create new user accounts; regular users cannot run useradd.
  • Different Unix systems (Linux, BSD, macOS) may have slight variations in how useradd works or what flags are available.

Choosing a username that follows Unix conventions

Unix usernames should be short, lowercase, and contain only letters, numbers, underscores, and hyphens. Avoid spaces, special characters, and uppercase letters, because many programs and scripts assume usernames follow this pattern. A username like "john_smith" or "user42" works well; "John Smith" or "user@domain" will cause problems.

Keep usernames between 3 and 32 characters. Very short names like "a" or "b" are hard to remember and straightforward to confuse. Very long names become tedious to type. Most systems allow up to 32 characters, though some older Unix variants limit it to 8.

Avoid usernames that match system commands or common program names. For example, "root", "bin", "mail", and "www" are already reserved on most systems. Trying to create a user with a reserved name will fail or cause unexpected behavior.

Setting permissions and group membership for the new user

When you create a user with useradd, the system assigns a user ID (UID) and a primary group. By default, the new user has minimal permissions and cannot perform administrative tasks. This is the correct setup for most accounts.

If the new user needs to run certain commands with root privileges, you can add them to the sudo group. Run sudo usermod -aG sudo username to add the user to the sudo group. The user can then run commands prefixed with sudo and will be prompted for their password.

You can also add the user to other groups depending on what they need to do. For example, sudo usermod -aG docker username adds the user to the docker group, which lets them run Docker commands without typing sudo each time. Use groups username to see what groups a user belongs to.

Logging in with the new username

Once the account is created and the password is set, the user can log in at the terminal prompt. At the login screen, type the username and press Enter, then type the password and press Enter again. If the credentials are correct, the shell prompt appears and the user is logged in.

If you are setting up a remote server, the user can also log in over SSH. The command is ssh username@hostname, where hostname is the server's address or IP. The system will prompt for the password, and after authentication, the user has a shell on the remote machine.

The first time a user logs in, the system copies default configuration files from /etc/skel/ into their home directory. This sets up basic shell settings and environment variables. The user can then customize these files to their preferences.

Verifying the new user was created correctly

To confirm that a user account exists and has the right settings, check the /etc/passwd file. Run cat /etc/passwd | grep username to see the user's entry. The output shows the username, UID, group ID, home directory path, and default shell.

You can also use the id username command to see the user's UID, primary group, and all groups they belong to. This is faster than reading /etc/passwd and gives you the information in a cleaner format.

If you need to see when the user last logged in, use lastlog -u username. This shows the date, time, and location of the most recent login. If the user has never logged in, the command returns no output.

Modifying or removing a username later

If you need to change a username after creation, use the usermod command. For example, sudo usermod -l newname oldname renames the user. The home directory path does not change automatically, so you may need to move it manually with sudo mv /home/oldname /home/newname.

To remove a user account entirely, use sudo userdel username. This deletes the user from the system but leaves their home directory intact. If you want to delete the home directory as well, add the -r flag: sudo userdel -r username. Be careful with this command — it cannot be undone.

Before removing a user, check what files they own on the system. Run find / -user username to locate all files belonging to that user. You may want to transfer ownership of important files to another user before deleting the account.

Troubleshooting common username creation problems

If you get a "permission denied" error when running useradd, you do not have root or sudo access. Ask your system administrator to create the account, or log in as root if you have the root password.

If the username already exists, useradd returns an error saying the user is already in the system. Use id username to see the existing account's details. If you want to use a different name, pick one that is not already taken.

If the password is not accepted after creation, make sure you ran sudo passwd username and entered the password correctly both times. Passwords are case-sensitive, so "Password" and "password" are different. If the user still cannot log in, reset the password with passwd again.

Frequently Asked Questions

What is the difference between useradd and adduser?

On some Linux systems, adduser is a wrapper around useradd that prompts you for information interactively. On others, they are the same command. useradd is more portable across different Unix systems, so it is the safer choice if you work on multiple machines.

Can I create a username with uppercase letters?

Technically yes, but you should not. Unix treats usernames as case-sensitive, so "John" and "john" would be two different accounts. Many programs and scripts assume usernames are lowercase, so mixing cases causes confusion and errors. Stick to lowercase.

What happens if I forget the password for a new user?

You can reset it with sudo passwd username and enter a new password. The user does not need to know the old password to change it. If the user forgets their own password, they can ask the system administrator to reset it the same way.

How do I prevent a user from logging in without deleting their account?

Run sudo usermod -L username to lock the account. The user cannot log in, but their files and account settings remain intact. To unlock it later, run sudo usermod -U username.

Can I set an expiration date for a user account?

Yes, use sudo usermod -e YYYY-MM-DD username to set an expiration date. After that date, the user cannot log in. This is useful for temporary accounts or contractors. Use chage -l username to see the expiration date.