Linux Debian user commands

These commands have come in handy a lot over the last couple of days, as I have been setting up Xen with a few Virtual Machines.

Replace [user] in the commands below with the name of your user (e.g. mike). If you need a good password it is recommended you use a strong one.

Add a user

How to add a user on linux via command line:

adduser [name]

Reset password

How to reset a user's password on command line:

passwd [name]

Delete user

Delete a linux user via command line:

deluser [name]

Give user sudo privileges

There are several ways you can add a user to the sudoers group - it is advised you stick to one method for each server.

You can see if there are users in the sudoers group with the following command:

grep -i --color sudo /etc/group

Add the user to the sudo "group"

If this is on a brand new server, you may need to apt-get install sudo

adduser [name] sudo

Add the user to the sudoers file

Edit the following file (must be done as root or someone with existing sudo privileges

sudo visudo

And add the following to the file:

username ALL=(ALL)   ALL

Alternatively, you can do it with one command:

echo ' username ALL=(ALL)   ALL' >> /etc/sudoers

List all the users on the system

cut -d: -f1 /etc/passwd

This will list out all the users - including system ones

Don't require password for sudo

Open the sudoers file:

sudo visudo

And add one of the following lines to the bottom of the file:

For a specific user:

[name] ALL=(ALL) NOPASSWD: ALL

For a group: (e.g. if the user is in the sudo group

%sudo ALL=(ALL) NOPASSWD: ALL

To confirm, the visudo file (which is really /etc/sudoers should look something like:

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) NOPASSWD: ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d
This website is currently having a full content audit - apologies if some of the code or content looks a bit funky!

View this post on Gitlab

You might also enjoy…

Mike Street

Written by Mike Street

Mike is a front-end developer from Brighton, UK. He spends his time writing, cycling and coding. You can find Mike on Twitter.