Skip to content

Instantly share code, notes, and snippets.

@windsting
Last active July 23, 2017 04:46
Show Gist options
  • Save windsting/857a4f8416b1e4489a4810fe83168bb8 to your computer and use it in GitHub Desktop.
Save windsting/857a4f8416b1e4489a4810fe83168bb8 to your computer and use it in GitHub Desktop.
Create Users and Manage Their Sudo Privileges on Ubuntu and CentOS

Create Users and Manage Their Sudo Privileges

To demonstrate, I use the newuser as user name

on Ubuntu

Add a new user with adduser

# adduser newuser
Adding user `newuser' ...
Adding new group `newuser' (1001) ...
Adding new user `newuser' (1001) with group `newuser' ...
Creating home directory `/home/newuser' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for newuser
Enter the new value, or press ENTER for the default
        Full Name []: New User
        Room Number []: 101
        Work Phone []: 123456
        Home Phone []: 123456
        Other []:
Is the information correct? [Y/n] y

Add sudo privileges with usermod

# usermod -aG sudo newuser

Switch to the new account with su

# su - newuser

After this command, you should be login with the new account

Test it with a sudo command

$ sudo ls -al
[sudo] password for newuser:

If everything is ok, th ls -al should be executed.


on CentOS

Add a new user with adduser

# adduser newuser

Set password with passwd

# passwd newuser
Changing password for user newuser.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Add sudo privileges with usermod

# usermod -aG wheel newuser

Switch to the new account with su

# su - newuser

After this command, you should be login with the new account

Test it with a sudo command

$ sudo ls -al

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for newuser:
total 20
drwx------  2 newuser newuser 4096 Jul 23 04:16 .
drwxr-xr-x. 4 root    root    4096 Jul 23 04:16 ..
-rw-r--r--  1 newuser newuser   18 Dec  6  2016 .bash_logout
-rw-r--r--  1 newuser newuser  193 Dec  6  2016 .bash_profile
-rw-r--r--  1 newuser newuser  231 Dec  6  2016 .bashrc
-rw-r--r--  1 newuser newuser    0 Jul 22 18:20 .cloud-locale-test.skip

If everything is ok, th ls -al should be executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment