Create a user on Linux and enable sudo without password

Mini Tutorial on how to add Linux users and give passwordless sudo superpower. 💪

Creating an user on Linux is quite easy. Here is a little reference how you can create a user on Linux systems and enable sudo without a password.

You have to be logged in as root user to make the following commands work. If you already are logged in with a sudo enabled user account you can use the following commands prefixed with sudo .

Create user account

To create a new user account, we will use the useradd command.

useradd -g <group> -d <homedir> -m -s <shell> <username>

Example to add a user with the username stella:

useradd -g users -d /home/stella -m -s /bin/bash stella

Let's break down the options:

  • -g <group> This option followed by the group's name indicates which group the user should be added to. Here we use the “users” group, which already exists on most Linux distributions.
  • -d <homedir> Here we set the home directory for the user account. This is the user's login directory with full permissions for the user.
  • -m Please, create the user's home directory if it does not exist already. The directory will have full permissions for the user.
  • -s <shell> Here we choose the shell the user will login to. The most common options are /bin/bash and /bin/sh (which is the default on most systems).
  • <username> is the last parameter for the command which gives the username of the new user account. No explanation is necessary.

But there is more! You can get a full list of all Options by running man useradd.

Passwordless sudo

Now that the user is created, we can enable sudo. First check if sudo is installed on your system by running the command sudo. If the response is Command 'skd' not found or similar, you should install sudo first. You can do thois using your favorite package manager. Here with examples:

apt install sudo # Debian based systems
yum install sudo # on RHEL/CentOS

Now we add the possibility for sudo without password for our previously created user stella:

visudo

This will open your favorite editor (vim or nano). At the very end of the file, at the beginning of a new line, add the following line.

stella ALL=(ALL) NOPASSWD:ALL

This will enable the user account stella to run sudo without having to enter the password.

Now save and exit the editor.

Stella can now run commands in superuser mode using the prefix sudo followed by space and the command. Let's test it by switching the user to stella and run a privileged root command:

su stella
sudo ls -l /root/

If you don't get any "Permission denied" error and see the directory listing, everything is fine.

Happy sudoing! 🥳

Enjoyed this post?

My goal with this blog is to help people to get started with developing wonderful software while doing business and make a living from it.

Subscribe here to get my latest content by email.

I won't send you spam. You can unsubscribe at any time.

© 2024 Headystack. All rights reserved.
👋