Linux create user with sudo

How to create a sudo user on Ubuntu and allow SSH login

Chi Thuc Nguyen

Aug 7, 2019 · 2 min read

The sudo command is used to allow a permitted user to execute a command as the superuser or another user, as specified by the security policy.

In this guide, I will show you how to create a new user on an Ubuntu server and give it sudo access and allow SSH login to that user.

Create a sudo user

1. Log in to your server as the user with superuser privilege

2. Create a new user account

For this, we use adduser command. Don’t be confused with the useradd command here. useradd is a low level binary command compiled with the system, whereas adduser is a high level Perl script built on top of useradd .

You sho u ld always use adduser to create new user as it provides more user friendly and interactive procedure.

Then follow the instruction to finish the procedure

3. Add the user to the sudo group

On Ubuntu, members of the sudo group have sudo privileges by default.

4. Test

  • Switch to the new user account
  • Verify the superuser privileges by the sudo command

Add public key to allow remote SSH login for the new user

1. Switch to the new user account

2. Create .ssh folder in home directory

3. Create authorized_keys file in side the .ssh folder and add the public key

Use your favorite text editor for this. I use vim here, for example:

And paste your SSH public key here, save and close file

4. Verify SSH remote login

Open another terminal on your machine and try to remote SSH login using new user.

This should work if you have your SSH private key in

/.ssh/id_rsa file, otherwise you must specify the path to your private key with -i option:

If you can login successfully, congratulations!

Источник

Add a new user account with admin access on Linux

Create a new user account with admin (sudo) access on Ubuntu or Debian Linux

Commands to add or create a sudo user (admin) on an Ubuntu or Debian Linux server:

  • Open the terminal application
  • For remote Ubuntu/Debian server use the ssh command and log in as the root user using either su or sudo.
  • Create a new user named marlena , run: adduser marlena
  • Make marlena user ‘sudo user’ (admin) run: usermod -aG sudo marlena
  • Verify it by running the id marlena command
  • Log in as marlena: su — marlena . Than run sudo command for verification. For example: sudo ls -l /etc/shadow

A note about granting sudo account for an existing Ubuntu/Debian Linux user

The syntax is (must run as the root user):
# adduser sudo
## add user named ‘sai’ to sudo group ##
# adduser sai sudo
Another syntax:
usermod -aG sudo UserNameHere
Log in as the root user and add an existing user accout named ‘sai’ to sudo group:
usermod -aG sudo sai
id sai
See “How to create a new sudo user on Ubuntu Linux server” for more info.

Читайте также:  Изменить имя организации windows

RHEL or CentOS create a new user account with admin (sudo) access

Procedure to add or create a sudo user (admin) on CentOS or RHEL:

  • Open the terminal application
  • For remote CentOS server use the ssh command and log in as the root user using either su or sudo.
  • Create a new CentOS user named vivek , run: useradd vivek
  • Set the password, execute: passwd vivek
  • Make vivek user ‘sudo user’ on CentOS/RHEL, run : usermod -aG wheel vivek
  • Verify it by running the id vivek command

How to grant sudo access for an existing RHEL/CentOS Linux user

The syntax is as follows for the usermod command and id command:
# usermod -aG wheel User-Name-Here
Add an existing user account named kirsh to wheel group. In other words, type the following two commands:
# usermod -aG wheel krish
# id krish
See Add / Create a Sudo User on CentOS Linux 8 for more info.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Conclusion

You learned how to create a sudo (admin level access) user account on both Ubuntu/Debian and RHEL/CentOS Linux servers. See sudo docs here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How To Create a Sudo User on Ubuntu [Quickstart]

Published on March 28, 2016

Please refer to our Ubuntu 18.04 version of this tutorial instead.

The sudo command provides a mechanism for granting administrator privileges, ordinarily only available to the root user, to normal users. This guide will show you the easiest way to create a new user with sudo access on Ubuntu, without having to modify your server’s sudoers file. If you want to configure sudo for an existing user, simply skip to step 3.

Steps to Create a New Sudo User

Log in to your server as the root user.

Use the adduser command to add a new user to your system.

Be sure to replace username with the user that you want to create.

Set and confirm the new user’s password at the prompt. A strong password is highly recommended!

Follow the prompts to set the new user’s information. It is fine to accept the defaults to leave all of this information blank.

Use the usermod command to add the user to the sudo group.

By default, on Ubuntu, members of the sudo group have sudo privileges.

Test sudo access on new user account

Use the su command to switch to the new user account.

As the new user, verify that you can use sudo by prepending “sudo” to the command that you want to run with superuser privileges.

For example, you can list the contents of the /root directory, which is normally only accessible to the root user.

The first time you use sudo in a session, you will be prompted for the password of the user account. Enter the password to proceed.

If your user is in the proper group and you entered the password correctly, the command that you issued with sudo should run with root privileges.

Here is a link to a more detailed user management tutorial:

Источник

How to Create a Sudo User on Ubuntu and Debian

As a sudo user, you can run commands and access files as root user but with your own password. You don’t need to know the root password or switch to root user. You just add sudo before the command to run it with root privilege.

Читайте также:  Как установить архиватор по умолчанию windows 10

In Ubuntu and Debian-based Linux systems, sudo is practically synonymous with root but in reality, sudo is much more than that.

But I am not going to go into details on sudo here. I’ll just show how you can add a sudo user to Ubuntu or Debian.

That was too short, right? Don’t worry! I explain the steps in detail.

Adding a sudo user in Ubuntu and Debian

I am using Ubuntu in this tutorial, but the steps mentioned here should apply to Debian and many other Linux distributions as well.

The commands used here are standard Linux commands and these should be installed on most Linux distributions by default. However, I am not sure if all Linux distributions have a group named sudo.

Let’s now get started on creating a sudo user or sudoer in Linux command line.

Step 1: Create a new user [if it doesn’t exist already]

Please check the users present on your Linux system. If the user that you want to grant sudo access doesn’t exist, the first step would be to create that user.

Keep in mind that the user will be a regular user when you create it. You add it to the sudoer list in the next step.

Note: Adding a new user will also create a user group named the same as the user in Ubuntu.

To create a user, one must be a sudoer or a root. This is to ensure that only people who are having rights over the system or in charge of protecting the system are creating new users and nobody else.

I am going to use adduser command here.

When you type your password, it will be invisible, but you can also use delete or backspace whenever you like. You may also use Ctrl+U Linux shortcut to delete the entire line and start from the beginning.

Here’s a sample screenshot for the creation of a new user named handbook:

With the adduser command, the home directory for the user is also created.

It also asks for details for name, phone numbers, and others. You may enter the details if you want to, but it is not mandatory. After confirmation, the user will be created successfully.

You may verify the creation of the new user by checking the content of the /home directory or grep on the username in /etc/passwd file.

Step 2: Make it a sudo user

Once the user is created, he/she can be easily converted into sudo user by adding it to the sudo group. Yes, that’s it.

You must use -aG options. The a ensures that you are appending the group.

In the above command:

  • usermod changes user’s properties and rights
  • –a option tells to append the given group to the user without removing the user from existing groups. If you miss it, the user will be removed from other existing group and that will become a pain for you.
  • Option -G lists the groups to which user is to be added. In our case, it is “sudo”. So it is added directly to the command.

Successful addition doesn’t produce any output.

Step 3: Verify sudo access

Now, you should verify if the user has sudo access or not. My favorite way is to use this command:

At the end of this command’s output, you can see if you can run all commands as sudo (ALL : ALL) ALL.

Alternatively, you can change user in command line or simply log out and log in as the new sudo user and then try to run a command as sudo.

From this point, You can write any command that needed sudoer privileges by prepending the command with “sudo”.

That’s it. You successfully created a sudo user.

I hope you liked this quick tip. If you have any questions or suggestions, feel free to leave a comment below.

Источник

How To Add User To Sudoers & Add User To Sudo Group on Ubuntu

Home » SysAdmin » How To Add User To Sudoers & Add User To Sudo Group on Ubuntu

Читайте также:  Крестики рядом с ярлыком windows 10

The sudo command is the preferred means to handle elevated permissions. In supported versions of Ubuntu, using the sudo command will grant elevated permissions for 15 minutes.

Standard user accounts are restricted from performing sensitive tasks, such as viewing the contents of the /root directory. This prevents the accidental use of commands with massive consequences. It also makes it more difficult for intruders to compromise a system. However, sometimes you need to run an administrative command. Sudo – or Super User Do – grants you privileges to run sensitive tasks.

This simple tutorial will show you how to add a new user on Ubuntu and provide sudo access.

  • A system running a supported version of Ubuntu
  • Access to a root user account or another account with sudo privileges
  • Access to a terminal window/command line (CtrlAltT)

Note: Linux Sudo Command tutorials also available for CentOS and Debian.

Steps to Add Sudo User on Ubuntu

Step 1: Create New User

1. Log into the system with a root user or an account with sudo privileges.

2. Open a terminal window and add a new user with the command:

The adduser command creates a new user, plus a group and home directory for that user.

You may get an error message that you have insufficient privileges. (This typically only happens for non-root users.) Get around it by entering:

3. You can replace newuser with any username you wish. The system will add the new user; then prompt you to enter a password. Enter a great secure password, then retype it to confirm.

4. The system will prompt you to enter additional information about the user. This includes a name, phone numbers, etc. – these fields are optional, and can be skipped by pressing Enter.

Step 2: Add User to Sudo Group

Most Linux systems, including Ubuntu, have a user group for sudo users. To grant the new user elevated privileges, add them to the sudo group.

In a terminal, enter the command:

Replace newuser with the username that you entered in Step 1.

Again, if you get an error, run the command with sudo as follows:

The -aG option tells the system to append the user to the specified group. (The -a option is only used with G .)

Note: Usermod command is a useful tool for user management. To learn more about its options, refer to our guide How To Use The Usermod Command In Linux.

Step 3: Verify User Belongs to Sudo Group

Enter the following to view the groups a user belongs to:

The system will respond by listing the username and all groups it belongs to, for example: newuser : newuser sudo

Step 4: Verify Sudo Access

Switch users by entering:

Replace newuser with the username you entered in Step 1. Enter your password when prompted. You can run commands as normal, just by typing them.

However, some commands or locations require elevated privileges. If you try to list the contents of the /root directory, you’ll get an access denied error: ls /root

The command can be executed with:

The system will prompt for your password. Use the same password you set in Step 1. You should now see the contents of the /root directory.

Now you know how to add and create a user with sudo privileges on Ubuntu.

Before sudo, users would log in to their systems with full permissions over the entire system with the su command. This was risky as users could be exploited by tricking them into entering malicious commands. These vulnerabilities were solved by limiting account privileges. However, administrators still had to log out of their account and into an admin account to perform routine tasks.

The sudo command in Ubuntu strikes a balance – protecting user accounts from malicious or inadvertent damage while allowing a privileged user to run administrative tasks. To learn more about the difference between these commands, check out Sudo vs. Su.

Источник

Оцените статью