Post

Creating a New User on the Raspberry Pi

Creating a new user on a Linux machine is a straightforward operation, assuming you know the command line and you remember the list of commands required.

I always find myself looking for this list, which I’m publishing here once and for all.

Short on time? Go to TL;DR.

This commands must be run as superuser (after running sudo su command).

1
2
# Create a new user
useradd bruce965
1
2
# Set a password for this user
passwd bruce965
1
2
# Make the new user a sudoer (optional)
adduser bruce965 sudo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Add this user to the same groups as pi (optional)
usermod --append --groups $(groups pi | sed -E "s/^(\S+)\s:\s(.+)$/\2/" | sed "s/\s/,/g") bruce965

# List pi groups with: groups pi
# pi : pi adm dialout cdrom sudo audio video plugdev games users input netdev spi i2c gpio

# Discard user (but keep groups) with: sed -E "s/^(\S+)\s:\s(.+)$/\2/"
# pi adm dialout cdrom sudo audio video plugdev games users input netdev spi i2c gpio

# Replace spaces with commas with: sed "s/\s/,/g"
# pi,adm,dialout,cdrom,sudo,audio,video,plugdev,games,users,input,netdev,spi,i2c,gpio

# Final result should be something like the following
# usermod --append --groups pi,adm,dialout,cdrom,sudo,audio,video,plugdev,games,users,input,netdev,spi,i2c,gpio bruce965
1
2
# Change new user's shell to BASH (optional)
usermod --shell /bin/bash bruce965
1
2
3
# Clone pi user's home and assign it to the new user
cp --recursive /home/pi/ /home/bruce965/
chown --recursive bruce965: /home/bruce965/

TL;DR

1
2
3
4
5
6
7
8
9
sudo su
THEUSER=newusername
useradd $THEUSER
passwd $THEUSER
adduser $THEUSER sudo
usermod -a -G $(groups pi | sed -E "s/^(\S+)\s:\s(.+)$/\2/" | sed "s/\s/,/g") $THEUSER
usermod -s /bin/bash $THEUSER
cp -r /home/pi/ /home/$THEUSER/
chown -R $THEUSER: /home/$THEUSER/
This post is licensed under CC BY 4.0 by the author.