How to create a new user in Linux
Published on Aug. 22, 2023, 12:15 p.m.
To create a new user in Linux, you can use the useradd command followed by the username of the new user you want to create. For example, to create a new user with the username “john”, you can run:
sudo useradd john
This will create the user account and assign a home directory with the same name as the username in the “/home” directory. However, by default, the new user will not have a password or a home directory. To add a password, you can use the passwd command followed by the username:
sudo passwd john
This will prompt you to enter a new password for the user. Once you have set the password, the new user account is created and ready to use. You can also use additional options and flags with the useradd command to customize the user account creation process, such as specifying the home directory or default shell.
To add a user to a group in Linux
To add a user to a group in Linux, you can use the usermod
command followed by the -aG
(or just -G
) option and the name of the group you want to add the user to. For example, to add user “john” to group “developers”, you can run:
sudo usermod -aG developers john
The -a
option adds the user to the group without removing them from any other groups, while the -G
option sets the list of supplementary groups for the user. After running this command, “john” will have access to any files or directories that are accessible to members of the “developers” group.
Note that changes made to a user’s groups will not take effect until the user logs out and back in again. Also, you need to have superuser privileges (i.e., use sudo
) to run usermod
command.
To remove a user from a group in Linux, you can use the gpasswd
command followed by the -d
option and the username and group name. For example, to remove user “john” from group “developers”, you can run:
sudo gpasswd -d john developers
This will remove user “john” from the “developers” group
Note that you need superuser privileges (i.e., use sudo
) to run the gpasswd
command.
If you want to delete the entire group, you can use the groupdel
command followed by the group name. For example, to delete the group “developers”, you can run:
sudo groupdel developers
This will completely remove the “developers” group and any users that are members of it.