How to create user in Linux? [SOLVED]


Tips and Tricks

Author: Omer Cakmak
Reviewer: Deepak Prasad

If you have not integrated Linux servers into a central user authentication such as LDAP/Active Directory, you will have to create local users many times.

The adduser and useradd commands are two commands for creating local users on Linux systems. Let's give examples of these commands now.

 

Introduction to useradd and adduser command

On Redhat-based operating systems, the useradd and adduser commands work the same job. The adduser command can be viewed as a symbolic link to the useradd command:

[foc@rhcsa-1 ~]$ ls -la /usr/sbin/
lrwxrwxrwx. 1 root root 7 Dec 3 2021 /usr/sbin/adduser -> useradd
-rwxr-xr-x. 1 root root 142376 Dec 3 2021 /usr/sbin/useradd

In Debian-based operating systems, the useradd command is the same as in Redhat basesd systems. But the adduser command is different.

You must run adduser or useradd with sudo privilege or as root user, otherwise you will get the following warning:

[foc@rhcsa-1 ~]$ adduser golinux
adduser: Permission denied.
adduser: cannot lock /etc/passwd; try again later.

If you run this command for a previously created user you may get the following warnings:

[foc@rhcsa-1 ~]$ sudo useradd golinux
useradd: warning: the home directory /home/golinux already exists.
useradd: Not copying any file from skel directory into it.
Creating mailbox file: File exists

 

Creating users in Redhat based OS

If you run useradd (adduser) in a terminal like this:

[foc@rhcsa-1 ~]$ sudo useradd golinux

This command will create home directory, mail directory, update /etc/passwd, /etc/group and /etc/shadow file as shown below:

  • /home/golinux/
  • /var/mail/golinux
  • /etc/passwd   ----------------   golinux:x:1001:1001::/home/golinux:/bin/bash
  • /etc/shadow  ----------------    golinux:!!:19399:0:99999:7:::
  • /etc/group     ----------------    golinux:x:1001:

The useradd command has many parameters. The most commonly used parameters are:

  1. -m, --create-home create the user's home directory
  2. -d, --home-dir HOME_DIR home directory of the new account
  3. -e, --expiredate EXPIRE_DATE expiration date of the new account
  4. -s, --shell SHELL login shell of the new account
  5. -G, --groups GROUPS list of supplementary groups of the new account
  6. -u, --uid UID user ID of the new account
  7. -U, --user-group create a group with the same name as the user account
  8. -p, --password PASSWORD encrypted password of the new account

If you want to create a non-standard user, you can give multiple values in a single line using these parameters:

sudo useradd golinux -m -d /home/golinux -s /bin/sh -G wheel -u 1111 -U -p '$6$/Rq89GYp6zd/D2dF$tlgruRwrnH0f1bqACj4GZtdbzVgqhkP5nEa7yZW5Gpo/IYkLdtmOwTmsfhPUce8o8plxo0r/Za1yQVFAQ4wej0'
NOTE:
You can use the following command to generate the password (With SHA512 based password algorithm):
openssl passwd -6 -stdin

The home directory has been created with the specified name.

[foc@rhcsa-1 ~]$ ls /home/
golinuxs

User expire date defined.

[foc@rhcsa-1 ~]$ sudo chage -l golinux
...
Account expires						: Dec 31, 2024
...

Shell information is given as /bin/sh

[foc@rhcsa-1 ~]$ cat /etc/passwd
golinux:x:1111:1111::/home/golinux:/bin/sh

The user is included in the wheel group.

[foc@rhcsa-1 ~]$ cat /etc/group
wheel:x:10:foc,golinux

User ID information was given as 1111.

[foc@rhcsa-1 ~]$ id golinux
uid=1111(golinux)

A group with the same name as the username has been created.

[foc@rhcsa-1 ~]$ cat /etc/group
golinux:x:1111:

Password information is given as a hash.

[foc@rhcsa-1 ~]$ sudo cat /etc/shadow
golinux:$6$/Rq89GYp6zd/D2dF$tlgruRwrnH0f1bqACj4GZtdbzVgqhkP5nEa7yZW5Gpo/IYkLdtmOwTmsfhPUce8o8plxo0r/Za1yQVFAQ4wej0 ...

To delete the created user, follow the steps below:

[foc@rhcsa-1 ~]$ sudo userdel golinux
[foc@rhcsa-1 ~]$ sudo rm -rf /home/golinux
[foc@rhcsa-1 ~]$ sudo rm -rf /var/mail/golinux

 

Creating users in Debian based OS

We mentioned that the useradd command in Debian-based operating systems is the same as in Redhat basesd systems. Now let's talk about the steps to create a user with the adduser command.

User can be created in 3 different ways with adduser.

 

Method-1: Interactive CLI

Type the username you want to add after the adduser command, then enter the requested information:

foc@ubuntu22:~$ sudo adduser golinux
[sudo] password for foc:
Adding user `golinux' ...
Adding new group `golinux' (1001) ...
Adding new user `golinux' (1001) with group `golinux' ...
Creating home directory `/home/golinux' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for golinux
Enter the new value, or press ENTER for the default
	Full Name []: Golinux
	Room Number []: 1
	Work Phone []: +905122454787
	Home Phone []: +903327412547
	Other []:
Is the information correct? [Y/n] Y

 

Method-2: Non-Interactive CLI

Create a user by entering the parameters after the adduser command. Create a user by typing the home directory, shell information and user id information after adduser:

foc@ubuntu22:~$ sudo adduser --system golinux --shell /bin/sh --uid 1234 --home /home/golinuxs
Adding system user `golinux' (UID 1234) ...
Adding new user `golinux' (UID 1234) with group `nogroup' ...
Creating home directory `/home/golinuxs' ...

 

Method-3: Non-Interactive via input config file

Information is defined in a configuration file. This file is named after the adduser --conf parameter. User(s) are created using the information in this file.

Example information is found in the /etc/adduser.conf file. Either edit this file or create a new file. Let's create an example file:

foc@ubuntu22:~$ nano user_add.txt

DSHELL=/bin/sh
DHOME=/home/
FIRST_UID=1111
LAST_UID=59999
USERGROUPS=yes
USERS_GID=1111
EXTRA_GROUPS="cdrom floppy audio video plugdev users sudo"
ADD_EXTRA_GROUPS=1

Then type the user_add.txt file after the adduser command and create the user:

foc@ubuntu22:~$ sudo adduser golinux --conf user_add.txt
Adding user `golinux' ...
Adding new group `golinux' (1001) ...
Adding new user `golinux' (1111) with group `golinux' ...
Creating home directory `/home//golinux' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for golinux
Enter the new value, or press ENTER for the default
	Full Name []: Golinux
	Room Number []: 1
	Work Phone []: +905122454787
	Home Phone []: +903327412547
	Other []:
Is the information correct? [Y/n] Y
Adding new user `golinux' to extra groups ...
Adding user `golinux' to group `cdrom' ...
Adding user `golinux' to group `floppy' ...
Adding user `golinux' to group `audio' ...
Adding user `golinux' to group `video' ...
Adding user `golinux' to group `plugdev' ...
Adding user `golinux' to group `users' ...
Adding user `golinux' to group `sudo' ...

If you want to delete the user for any reason, you can use the following commands:

$ sudo userdel golinux
$ sudo rm -rf /home/golinux

Creating users With GUI Interface

Adding users from the terminal in Linux can also be done from the interface in operating systems with GUI. For this, System> Users path is followed. Then add user is unlocked and the password of the authorized user is entered:

Create a User in Linux


Then the Add User button is clicked and the information about the user is entered, the Add button is clicked:

How to create user in Linux? [SOLVED]


After this process, the user is created successfully:

How to create user in Linux? [SOLVED]

The user can be deleted from the same screen, the Admin role can be given, many features such as automatic login feature can be activated.

 

What is NEXT

 

Summary

We explained how to create a user with two different commands and their parameters. You can get help from the --help and man pages of both commands. You can further customize the user you want to create with these parameters.

Although it is not as extensive as the adduser and useradd commands, you can also create a user with the following commands:

  • useradd - Add an user
  • newusers - update and create new users in batch
  • lnewusers - Create new user accounts

 

References

askubuntu.com - How can I add a user and set the password for them at the same time?

 


		
Omer Cakmak

Omer Cakmak

He is highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on OpenStack, KVM, Proxmox, and VMware. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment