Whatever your reason is, you may need to create a new user on Raspberry Pi. The procedure is not really intuitive for a beginner, so I will explain it in this tutorial.
The command adduser is available on Raspberry Pi. It can be used to create a new username, and define its password. After that, other commands can be useful to change user privileges.
In this post, I will start with the reasons why you’ll probably need to create a new user, then the step-by-step procedure, and finally, I’ll give you a few extra tips about user management.
By the way, if you are new on Raspberry Pi, I can help you to get started. In my mini-course, you’ll easily improve your skills on Raspberry Pi, from the very first steps to your first entire projects. Currently, my e-book is included for free in this course. Click here to get all the details.
Why will you need to create a new user?
Let’s start by examining a few reason why you would need to create a new user on your Raspberry Pi.
As explained in my 17 security tips post, using the default user is not a good idea, and the reasons to look at how to create a new one.
Many people keep using the default user on various operating systems (Administrator, root and pi).
However, I recommend disabling it if possible, and create another user with the same privileges.
The pi user is one of the most brute-forced logins with root.
Hackers have a list of commonly used logins and try mainly these ones.
If possible, create a new user and disable the pi user to prevent this kind of attacks (I’ll show you how in the next part).
Another good reason, is for creating several users for different permissions or persons.
For example, if you use your Raspberry Pi at home as a desktop PC (check my review here if you’re interested), it’s probably a good idea to create a different username for each person that will use it.
You can also create an administrator user for you (don’t name it admin), and a standard user for the person that will use the Raspberry Pi with the basic permissions.
Finally, the last reason I see where you’ll need to create a new user is the installation of a new application.
Some applications need a specific user to run.
And, you can have something similar to manage the user of an application.
For example, services like FTP, Samba or even an email server often use system users for their access.
How to create a new user on Raspberry Pi?
Whatever the reason that led you here, here is how to create a new user on Raspberry Pi :).
Create the new user
The user creation is based on only one command:
Here is how to create a new user:
- Type the following command:
sudo adduser <username>
sudo adduser patrick
- You will need to answer a few questions:
$ sudo adduser pat Adding user 'pat' ... Adding new group'pat' (1002) … Adding new user 'pat' (1002) with group'pat' … Creating home directory '/home/pat' ... Copying files from'/etc/skel' … New password: Retype new password: passwd: password updated successfully Changing the user information for pat Enter the new value, or press ENTER for the default Full Name : FROMAGET Room Number : Work Phone : Home Phone : Other : Is the information correct? [Y/n]
As soon as the wizard is completed, you can log in with the new username.
But, the new user will have no particular permission.
We’ll learn how to add permissions in the next paragraph.
I’m not talking about the file permissions (maybe for another post), but about the user permissions (sudo or specific groups for example).
Add the sudo right
If you are following this tutorial to replace the pi user, you’ll probably look to add the sudo privilege to your new user.
Here is how to do this:
- Type the following command:
sudo adduser <username> sudo
In my case:
sudo adduser pat sudo
$ sudo adduser pat sudo Adding user 'pat' to group'sudo' … Adding user pat to group sudo Done.
That’s it, your new user is now allowed to user the sudo command.
But there is still one difference with the pi user:
The system will ask for your password every time you use the command.
Sudo without password
If you prefer to keep it as with the default pi user, you need to follow this procedure:
- You will need to create a new file under /etc/sudoers.d
- This file will allow the user of your choice to use sudo without password.
- You can create this file in only one command:
echo '<username> ALL=(ALL) NOPASSWD: ALL' | sudo tee /etc/sudoers.d/010_<username>-nopasswd
echo 'pat ALL=(ALL) NOPASSWD: ALL' | sudo tee /etc/sudoers.d/010_pat-nopasswd
That’s it, you can now use sudo without typing your password each time.
Add the user to a group
The other thing you’ll probably need to do after creating a new user, is to add it to some groups.
You can do this with the adduser command too:
sudo adduser <username> <group>
sudo adduser pat www-data
Delete a user
Once you have created your new users, you can delete the pi user (don’t forget to try the new admin user first).
You can also use this command to delete useless users on your system.
Here is the command syntax:
sudo deluser --remove-home <username>
The –remove-home is optional, as the name suggests it will delete the /home/<username> folder.
So, if you want to delete the pi user without deleting the home folder, type this:
sudo deluser pi
Before closing this post, here is a few extra tips to manage the users on Raspberry Pi.
The /etc/skel is used as a template directory (skeleton).
Each time you create a new user on your system, the files in /etc/skel will be copied to the new home directory (/home/pat for example).
pi@pizero:~ $ ls -latr /etc/skel total 20 -rw-r--r-- 1 root root 675 May 15 2017 .profile -rw-r--r-- 1 root root 220 May 15 2017 .bash_logout -rw-r--r-- 1 root root 3523 Nov 13 2018 .bashrc drwxr-xr-x 2 root root 4096 Nov 13 2018 . drwxr-xr-x 106 root root 4096 Dec 12 12:25 ..
You can add new files here if you want to set each new user with custom files you create.
List active users
All the users you create are stored in the /etc/passwd file.
So if you want to get the complete list of all users enabled on your system, you can just type:
The following command will do the same thing:
Manage sudo permissions
And to conclude, you can manage more precisely the sudo permissions for each user on your system.
In a previous part, I showed you a way to give all privileges to a specific user, but you can also edit the sudoers file to configure this as you want.
Here is the command you can use to open this file:
The file content looks like this:
As you can see, the root has “ALL” privileges on the system.
But you can be more specific, and give only one command to a user by using the following syntax:
<username> ALL=(ALL) <command>
pat ALL=(ALL) /path/to/command
If you are interested in this topic, you can check this post on Kifarunix to get more details.
That’s it! You now know how to create a new user on your Raspberry Pi and how to manage their permissions.
I hope this tutorial was useful for you.
If it’s the case, feel free to leave a comment below or to share it on any social network you want :).