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:
sudo adduser dummyUser
Añadiend el usuario `dummyUser' ...
Añadieno el nuevo grupo ` dummyUser' (1001) ...
Añadindo el nuevo usuario ` dummyUser' (1001) con grupo ` dummyUser'...
Creando el directorio personal `/home/dummyUser'...
Copiando los ficheros desde `/etc/skel' ...
Introduzca la nueva contraseña de UNIX:
Vuelva a escribir la nueva contraseña de UNIX:
passwd: contraseña actualizada correctamente
Cambiando la información de usuario para dummyUser
Introduzca el nuevo valor, o pulse INTRO para usar el valor predeterminado
Nombre completo : Dummy User
Número de habitación :
Teléfono del trabajo :
Teléfono de casa :
¿Es correcta la información? [S/n] S
Then your user, in my case named dummyUser, is created. Before using it you need to set its password by typing:
sudo passwd dummyUser
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
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.
"/etc/group" 59 lines, 816 characters
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.
pi adm dialout cdrom sudo audio video plugdev games users netdev input
sudo useradd -m -G adm,dialout,cdrom,sudo,audio,video,plugdev,games,users,netdev,input dummyUser
The sudoers group
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
sudo vi /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
dummyUser ALL=(ALL:ALL) ALL
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:
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) NOPASSWD:ALL
Note that if you want to force sudo to ask for a password, you should change the text
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:
sudo useradd -m -G sudo dummyUser
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).
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:
sudo vi /firstname.lastname@example.org
ExecStart=-/sbin/agetty --autologin pi --noclear %I $TERM
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:
sudo deluser -remove-home pi
You should also delete the
spi groups by typing:
sudo groupdel spi
sudo groupdel pi