This simple tutorial shows how to use Linux commands to create or delete user account in Ubuntu either Desktop or Server.
Every Linux Desktop today has a graphical interface to manage user accounts. But for working in recovery mode, or Linux server without GUI, the command skill is sometimes useful to work things out. And, here’s the quick tutorial that may be help.
Though the title said for Ubuntu 22.04, this tutorial works for most Linux, including Debian, Fedora, RHEL.
Linux command to create user account
We usually use
useradd command to create user account in Linux. Here are some examples show you how to use it.
Create a basic user without user home directory
First, you can simply run
useradd new_user_name to create a new account. Of course, you need to replace “new_user_name” to your desired name.
For example, create user ‘
sudo useradd chen
This command creates new user with default options, according to
/etc/default/useradd. In Ubuntu, it just creates the user and a group with same name, no home directory and no other permissions.
In some Linux, such as RockyLinux 9, the default option will create home directory for new user account. So, you may run this command instead to avoid it:
sudo useradd --no-create-home chen
After creating new user account, you may enable it by setting a password (replace username ‘chen’ to yours).
sudo passwd chen
Finally, you may login with the new user account. However, since it has NO home directory, many graphical applications won’t run since the user has no permission to write data to disk.
Create Home Directory for Existing User Account in Ubuntu
In case you created new account with the last command, then you want to generate home directory for it, run command:
sudo mkhomedir_helper chen
chen in command with yours username, then it create the home directory with same name. After that, this account works just like a standard user though default to
/bin/sh rather than
/bin/bash in Ubuntu.
Create a Standard user via Linux command
If you want to create a standard user, just like you do in Ubuntu Desktop with the “Settings -> Users” settings page.
Then, simply run command (replace
chen to your desired name):
sudo useradd chen --create-home --shell /bin/bash
To be short, you may also run
sudo useradd chen -m -s /bin/bash instead.
This command create new user ‘chen’, and create ‘/home/chen’ as its home directory. Since, it uses
/bin/sh as default shell, the
--shell /bin/bash change the shell to bash. And,
sh, however with more features and better syntax.
After creating the user, also run command
sudo passwd chen to enable it by setting password.
Standard user has full permission access to its home folder contents, basic web browsing, file editing, etc features in Desktop, but can neither install/remove applications, nor edit system-wide config files.
Create Administrator user via Linux command
To add an administrator account, just like the one that you create while installing the Ubuntu Linux system. Run command:
sudo useradd chen --create-home --shell /bin/bash --groups adm,sudo,dip,plugdev,lpadmin
Different to standard user, it just add
--groups option to add the new account to
sudo and a few other system groups. To be short, use this command instead:
sudo useradd chen -m -s /bin/bash -G adm,sudo,dip,plugdev,lpadmin
Finally, also run
sudo passwd chen to set password to enable the account.
To add user to other groups, you may run
sudo gpasswd --add user_name group_name command later.
Create a user with expiration date
You can also create an account that will be automatically disabled on given date.
For example, create admin account ‘chen’ that will expire on March 1, 2030, run:
sudo useradd chen --create-home --groups sudo --expiredate 2030-03-01
After enabling the account via
sudo passwd chen, you may then find out when the account will be expired by running command:
sudo chage -l chen
Linux command to delete user account
For removing user account in Ubuntu Linux, we can use
To remove an account while leaving home directory unchanged, just run (replace
chen to yours):
sudo userdel chen
To remove user account as well as its home directory, run command:
sudo userdel -r chen
You may also force remove an user account, even if the user is still logged in:
sudo userdel -rf chen