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
How to create a new user on Raspberry Pi?
To create a new user, you need to use the adduser
command to define the new username and password
Then, you can add it extra permissions by adding it to a group or even allow it the sudo privilege
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 users management
Feel free to use the table of contents below to go directly to the section interesting you
Why will you need to create a new user?
Let’s start by examining a few reason why you could need to create a new user on your Raspberry Pi
Security
As explain in my 17 security tips post, using the default user is not a good idea, and good reason to look 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)
Multi users
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
Applications
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 examples, 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: adduser
Here is how to create a new user:
- Type the following command:
sudo adduser <username>
For example: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 user name
But the new user will have no particular permission
We’ll see how to add them in the next paragraph
Permissions
I’m not talking here 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 you password each time you’ll 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
For example: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>
For example: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
Extra tips
Before closing this post, here is a few extra tips to manage the users on Raspberry Pi
Skel
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:cat /etc/passwd
The following command will do the same thing:getent passwd
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:sudo visudo
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>
For example:pat ALL=(ALL) /path/to/command
If you are interested in this topic, you can check this post on Kifarunix to get more details
Conclusion
That’s it! You now know how to create a new user on your Raspberry Pi, but also how to manage its 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 🙂