XKCD sudo

Creating a new user in Raspberry Pi


In this post we will explain how to create a new user in Raspberry Pi by replacing the default one. The basic configuration of groups and permissions will also be discussed.

Create a new user in Raspberry

It is a good and secure practice to change the default user pi and to create a new one, specially if you are going to enable SSH connections. First of all, let’s create a new user in Raspberry by typing:

Then your user, in my case named dummyUser, is created. Before using it you need to set its password by typing:

Make sure that you have correctly configured your keyboard, specially if your password includes special signs, such as brackets or underscores.

Editing the groups

In a next step you should include this user in all the groups where the pi user was. There are different ways to do this operation. From my point of view, the easiest way is just to edit the file /etc/group with vi or your preferred text editor. Then you can replace (if you are going to delete the default pi user) or add (using a comma) the new user in all the groups containing the pi user.

Option two consists of using the command groups when logged in with user pi to write down the groups containing pi and then add them, subsequently, to the new user.

The sudoers group

credit to: XKCD
As you may have already noticed, some commands need to start with sudo because they need privileges to run. You should never log in into your Raspberry Pi as root user rather to use the sudo command (SUperuser DO) to grant a command access as the superuser, since this is a much better (and securer) alternative.  This will allow you, first of all, to restrict user privileges and also to log the activities taken by users when sudoing.
Let’s take a closer look to the sudo configuration file. In your Raspberry you can find it in /etc/sudoers.

You will see a zone where the user privileges are specified. One option to include your user as a sudoer is just adding it in this section in a new line following the structure user host=(user:group) cmds:

Here dummyUser is the name of your new user and:

  • The first ALL denotes that this rule applies to all user root logged in from all hosts.
  • The second ALL allows user root to run commands as all users.
  • The third ALL allows user root to run commands as all groups.
  • The fourth and last ALL allows root to run all commands.

In the sudo file you will find also a zone where the groups are configured. Usually there will be a line like the following:

Note that if you want to force sudo to ask for a password, you should change the text NOPASSWD:ALL to PASSWD:ALL.

In this case, the easiest way to grant sudo access to your new user is just adding him to the sudo group with the following line:

Loging in automatically with a new user in Raspberry

As we decribed in a previous post, raspi-config utility can be used to automatically login in console or boot mode. However, it will only work with the default pi user.  Therefore, if you have some experience with bash, you can edit the script to adapt it to your user name (find the Github code here).

Configured raspi-config to show the new user in Raspberry

But it is much easier to directly edit the getty target file. To do it, first of all open the following file and then change the user pi by your own user:

Doing some cleaning

Once logged in with your new user in Raspberry, remember to delete the pi user, and also its home folder, by tipping:

You should also delete the pi and spi groups by typing: