How to clone pi user on Raspberry Pi








After setting up your Raspberry Pi you will end up with a default pi user with default password.
Obviously you will want to change your password to something else than default. Also you may want to create clone of the pi user for mainly two reasons:
1. You may want multiple users to access your Raspberry board and it’s always good idea to not share the same system account between different users. Good practice would be to create different users account and clone those from the default pi user.
2. You may eventually want to disable bult-in pi account for security reasons and use another user account to work with your Pi.
If you create a new user with simple useradd command you will end up with a very restricted account without any group membership. A good starting point would be to clone “pi” user with all its group membership.
Assuming you are logged in as “pi”, just create a file “userclone.sh”, make it executable in the pi’s home directory and run it providing two arguments – source user and destination user name. This script will create new user, it’s home directory and clone group membership from existing user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #!/bin/bash SRC=$1 DEST=$2 SRC_GROUPS=$(groups ${SRC}) SRC_SHELL=$(awk -F : -v name=${SRC} '(name == $1) { print $7 }' /etc/passwd) NEW_GROUPS="" i=0 #skip first 3 entries this will be "username", ":", "defaultgroup" for gr in $SRC_GROUPS do if [ $i -gt 2 ] then if [ -z "$NEW_GROUPS" ]; then NEW_GROUPS=$gr; else NEW_GROUPS="$NEW_GROUPS,$gr"; fi fi (( i++ )) done echo "New user will be added to the following groups: $NEW_GROUPS" useradd --groups ${NEW_GROUPS} --shell ${SRC_SHELL} --create-home ${DEST} mkhomedir_helper ${DEST} passwd ${DEST} |







