TalosDEV
Programming and Computing Tutorials

Create a Linux user with sudo access from command line

March 22, 2020 

Creating a new user

In order to create a new linux user from the command line, we need to have sudo access.
The command we will use for that is ‘adduser’ and we will create a user with username “testuser” as follows:

$ sudo adduser testuser

After giving the above command we are asked to give our password (because we used sudo):

  [sudo] password for talosdev: 

After we give our password we see some output like this:

  Adding user `testuser' ...
  Adding new group `testuser' (1000) ...
  Adding new user `testuser' (1000) with group `testuser' ...
  Creating home directory `/home/testuser' ...
  Copying files from `/etc/skel' ...

And then we are asked to give the new user’s password twice:

  New password: 
  Retype new password: 
  passwd: password updated successfully
  Changing the user information for testuser

Then, we are asked to give some optional details about the user (we can just press enter if we want to leave them blank):

  Enter the new value, or press ENTER for the default
	  Full Name []: 
	  Room Number []: 
	  Work Phone []: 
	  Home Phone []: 
	  Other []: 

And finally, we are asked if the above information is correct:

  Is the information correct? [Y/n] Y
$ _

And that’s it, the new user is created as well as his/her home directory.

Giving the new user sudo access

In order to give the new user sudo access we will use the usermod command (using sudo of course, and giving our current user’s password if asked).
The usermod command actually adds the new user to a group named ‘sudo’, as shown here:

$ sudo usermod -aG sudo testuser 
  [sudo] password for talosdev:
$ _

And again, that’s all, the user ‘testuser’ is now a member of the ‘sudo’ group, and has gained sudo access.

Deleting a user with his home directory

When we want to delete a user in Linux, we have to take under consideration if we also want to delete the user’s home directory (and all the files in it) or not, or if we want to delete all the files created by the user elsewhere in our system.

I would recommend, to keep the files the user created elsewhere, because it can create problems sometimes, if these files are used by other users or in certain services, and so, we should only delete the user’s home directory.

The command for deleting a user is ‘deluser’ and optionally, if we also want to delete the user’s home directory we can add the option - -remove-home, aditionally, another option we could use is ’–remove-all-files’ in order to delete all the files created by the user in our system. The syntax goes like this:

$ sudo deluser [--remove-home] [--remove-all-files] testuser

In our case we will only use the first option:

$ sudo deluser --remove-home testuser
  [sudo] password for talosdev: 
  Looking for files to backup/remove ...
  Removing files ...
  Removing user `testuser' ...
  Warning: group `testuser' has no more members.
  Done.
$ _

After we give our current user’s password (if asked, due to ‘sudo’ at the beginning of the command), we see some output and the word ‘Done’ meaning that the user is now deleted as well as the user’s home directory.


Related Items