Having multiple users on your Linux system is a must when there are more then 1 users.
Or do you need to setup a 2nd user for security line on the raspberry pi?
Lets take a look on how to setup a new user on Debian like Linux Distro’s
- Linux PC
Setting up the user
On the raspberry pi the standard user is called ‘pi’ with a standard password ‘raspberry’.
Thought this is very handy this isn’t quite secure, so we’ll add a new user.
Afterwards we can delete the Pi user or simply use a different password for it.
We add a new user with the ‘adduser’ command, ‘username’ in here is the requested new user.
We are using the terminal to add this user:
sudo adduser 'username'
Once you have pressed enter you’ll find that the pi (or any Debian system) will give you an answer in response:
Adding user `‘username’ ... Adding new group `‘username’ (1001) ... Adding new user `‘username’ (1001) with group `‘username’ ... Creating home directory `/home/username’ ... Copying files from `/etc/skel' ... New password: Retype new password: passwd: password updated successfully
Here you can see that the user ‘username’ is being added to the raspberry pi, including the required groups and directories.
It also asks for a password for this user.
You’ll have to type it in twice. You’ll notice that you don’t get any feedback from the terminal when typing in the password.
Once you have typed in a password you’ll be asked to input some user information, you can leave this empty if you like. End with an additional enter to save.
Changing the user information for ‘username’ Enter the new value, or press ENTER for the default Full Name : Room Number : Work Phone : Home Phone : Other : Is the information correct? [Y/n]
Now we have set up a new user we will give it sudo rights. This requires multiple steps, lets start with adding the user to the sudo group.
sudo adduser ‘username’ sudo
Don’t forget to replace ‘username’ with your username.
If this goes well we should get this output in out terminal.
Adding user ‘username’ to group ‘sudo' ... Adding user ‘username’ to group sudo Done.
So, now we have created a new user and added it to the sudo group. This means that this user is now capable of using the ‘sudo’ commands. Some commands require extra rights to be executed. To execute these you need to be a ‘root’ user or have the capability to use sudo.
Sudo will allow you to run a command as a root user for that one command.
There is an option in Linux to execute the Sudo command without giving a password.
Before we do this we will jump over to the new account.
sudo login 'username'
Using the command above you can switch users without the need to logout.
This will require the users password.
Now that we are logged in we can change the sudo file. Opening the sudo file should be done using the visudo command. This command will check if you made mistakes in the setup and let you correct it before you no longer have access. When you mess up this file you will no longer be able to use the sudo command unless you can repair it with the root user.
On the pi we will use a different approach:
sudo nano /etc/sudoers.d/010_pi-nopasswd
in either of these files we will add following line:
'username' ALL=(ALL) NOPASSWD: ALL
in this line all you need to do is replace ‘username’ with the newly created user. When this is done, press ‘CTRL + O’ and then ENTER to save the file.
To close it press ‘CTRL + X’. When this is done lets reboot and check if we still have sudo right on the new user:
sudo reboot now ... sudo apt update
This last command is a harmless one, all it will do is check for updates.
Deleting a user
If you wish to delete a user make sure there is still another user on the system that has root permission. deleting a user uses the deluser command:
sudo deluser 'username'
To check if the user is deleted we’ll head over to the passwd file using the cat, less or more command.
cat /etc/passwd less /etc/passwd more /etc/passwd
Using cat you can use the grep command:
cat /etc/passwd | grep pi
A little explanation is required here. The cat command will show you the content of the file requested, in this case the passwd file.
after the cat we obviously enter the filename (either just the name of the file or the full path depending on where we are in our Linux system) to only show us what we want we use a search command in the same line. This is done using the ‘pipe’. This ‘pipe’ or ‘|’ will allow us to run a command on the first command. in this case we want to search within the file, for this we use the ‘grep’ command. This will pull out (and show them in the console) all the lines containing the string we put after it, in this case ‘pi’.
If all went as it should, we should have no responce after running this command.
So how do we know we got the command right? Easy, we try the same command but use ‘root’ as user we wish to search:
cat /etc/passwd | grep root root:x:0:0:root:/root:/bin/bash