Jayce
Posts: 67
Joined: Sun Jan 29, 2017 5:53 am

Deleting user 'pi'

Fri Sep 08, 2017 5:56 am

When I create a new user, it it possible to delete the original user 'pi' and make the new user as the default one?

User avatar
ab1jx
Posts: 885
Joined: Thu Sep 26, 2013 1:54 pm
Location: Heath, MA USA
Contact: Website

Re: Deleting user 'pi'

Fri Sep 08, 2017 6:12 am

As far as I know yes, in fact you'll probably have to do it first. I have a root and a pi account and that's all it will allow so far anyway.

It could be argued that keeping your pi account is insecure just because every Pi has one by default. Some people say that in Windows you should delete the administrator account and set up something else as the administrator. In principle that's true with a unix root account too except that's also account 0 so that's harder to replace. By default root can't log in to a Pi over ssh, so that closes a remote hole. You have to log in with another account then su to become root.

fred44nl
Posts: 292
Joined: Sat Jun 25, 2016 11:59 am
Location: Scharendijke, NL

Re: Deleting user 'pi'

Fri Sep 08, 2017 9:13 am

I have given the user pi an "impossible" password.
also I have set the user pi to be unable to log on.
I did this because I had been using user pi for some time.
so there are a lot of files and folders that are owned by user pi.
headless RPi 3B running from usbhdd.

User avatar
DougieLawson
Posts: 38830
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Deleting user 'pi'

Sat Sep 09, 2017 1:18 pm

The way I did it was to edit /etc/passwd, /etc/group and /etc/shadow to change all occurences of pi to dougie.

It's unothordox, unsupported and if you get it wrong you'll have a trashed system, so take a backup first (you can use a rescue SDCard to put things back).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Deleting user 'pi'

Mon Sep 11, 2017 12:50 pm

thanks @DougieLawson i love your idea.
i did a new setup, incl update upgrade, shh + VNC enable
with my usual small installs and settings.
passwd raspberry newpw newpw
shutdown
__________________________________________
this SD card i mount in card reader and boot my working system, to edit: "pi" to "po"
______________________
lsblk
tells my that the card reader / that SD card in sda2 / is already mounted, so i use:
THESD="/media/pi/037616fd-28fe-4652-8248-2042ea30b929/etc"
sudo nano $THESD/passwd
_: po: x:1000:1000:,,,:/home/po:/bin/bash
sudo nano $THESD/group
_: 15 times pi to po
sudo nano $THESD/shadow
_: change one time
_______________________
additionally i check /etc/sudoers, but now no pi, only %sudo

sudo nano $THESD/systemd/system/autologin\@.service
_: ExecStart=-/sbin/agetty --autologin po --noclear %I $TERM
goto .../home/ and move the home directory:
sudo mv pi po
shutdown
_______________________
when i boot that SD card i get a login window for po and must give password newpw
my own desktop picture gone?? must link again to it at /home/po/xxx
___must do this:
sudo raspi-config / 3 boot options / B1 desktop / B4 auto login as po /
(reboot)
now desktop auto login works fine!
anyhow sudo / raspi-config / ssh / VNC all working.

i already found some small differences, should check more.
but looks like the STRETCH Raspbian changes work well, and the DougieLawson hack too.

beta-tester
Posts: 1372
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: Deleting user 'pi'

Mon Sep 11, 2017 1:05 pm

i patch pi to new_user offline... similar as DougieLawson
just after flashing the SD card, i mount the root partition of the just flashed image to /mnt and run the script,
to patch the user "pi" to "new_user"
(and setup the locale, the keyboard layout and copy the WiFi configuration)

Code: Select all

EDIT2: i changed the script to change only pi to new_user and not spi or gpio
see complain of next post.
EDIT: you can do it also online, but then you have to create a temporary user that has root permissions and run the script with "MOUNT_POINT=".
or without creating a temporary, then using the /etc/rc.local file and execute the script from there at boot time.
Last edited by beta-tester on Mon Sep 11, 2017 4:39 pm, edited 5 times in total.
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Deleting user 'pi'

Mon Sep 11, 2017 2:48 pm

beta-tester wrote:
Mon Sep 11, 2017 1:05 pm
i patch pi to new_user offline... similar as DougieLawson
just after flashing the SD card, i mount the root partition of the just flashed image to /mnt and run the script,
to patch the user "pi" to "new_user"
(and setup the locale, the keyboard layout and copy the WiFi configuration)

Code: Select all

#!/bin/sh
######################################################################
MOUNT_POINT=/mnt
OLD_USER_NAME=pi
NEW_USER_NAME=new_user
OLD_LOCALE=en_GB.UTF-8
NEW_LOCALE=de_DE.UTF-8
OLD_KEYBOARD_LAYOUT=gb
NEW_KEYBOARD_LAYOUT=de
######################################################################
SCRIPT_DIR="$(dirname "$0")"

######################################################################
# change user $OLD_USER_NAME to $NEW_USER_NAME
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/shadow;
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/group;
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/gshadow;
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/passwd;
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/subgid;
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/sudoers;
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/profile.d/sshpwd.sh;
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/systemd/system/autologin@.service;
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/dhcp/dhclient-enter-hooks.d/samba;

[ -f $MOUNT_POINT/etc/shadow- ] && \
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/shadow-;
[ -f $MOUNT_POINT/etc/group- ] && \
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/group-;
[ -f $MOUNT_POINT/etc/gshadow- ] && \
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/gshadow-;
[ -f $MOUNT_POINT/etc/passwd- ] && \
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/passwd-;

[ -f $MOUNT_POINT/etc/systemd/system/getty.target.wants/getty@tty1.service ] && \
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/systemd/system/getty.target.wants/getty@tty1.service;
[ -f $MOUNT_POINT/etc/lightdm/lightdm.conf ] && \
sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/lightdm/lightdm.conf;

[ -f $MOUNT_POINT/etc/sudoers.d/010_$OLD_USER_NAME-nopasswd ] && {
    sudo sed -i -e "s/$OLD_USER_NAME/$NEW_USER_NAME/g" $MOUNT_POINT/etc/sudoers.d/010_$OLD_USER_NAME-nopasswd;
    sudo mv $MOUNT_POINT/etc/sudoers.d/010_$OLD_USER_NAME-nopasswd $MOUNT_POINT/etc/sudoers.d/010_$NEW_USER_NAME-nopasswd;
}

sudo mv $MOUNT_POINT/home/$OLD_USER_NAME $MOUNT_POINT/home/$NEW_USER_NAME;

######################################################################
# change change locale $OLD_LOCALE to $NEW_LOCALE
sudo sed -i -e "s/^$OLD_LOCALE UTF-8/\# $OLD_LOCALE UTF-8/" $MOUNT_POINT/etc/locale.gen;
sudo sed -i -e "s/^\# $NEW_LOCALE UTF-8/$NEW_LOCALE UTF-8/" $MOUNT_POINT/etc/locale.gen;
sudo sh -c "cat << EOF  > $MOUNT_POINT/etc/default/locale
LANG=$NEW_LOCALE
LANGUAGE=$NEW_LOCALE
LC_ALL=$NEW_LOCALE
EOF";

######################################################################
# change keyboard layout $OLD_KEYBOARD_LAYOUT to $NEW_KEYBOARD_LAYOUT
sudo sed -i -e "s/$OLD_KEYBOARD_LAYOUT/$NEW_KEYBOARD_LAYOUT/g" $MOUNT_POINT/etc/default/keyboard;

######################################################################
# copy WiFi setup
[ -f $SCRIPT_DIR/wpa_supplicant.conf ] && \
sudo cp $SCRIPT_DIR/wpa_supplicant.conf $MOUNT_POINT/etc/wpa_supplicant/.
EDIT: you can do it also online, but then you have to create a temporary user that has root permissions and run the script with "MOUNT_POINT=".
or without creating a temporary, then using the /etc/rc.local file and execute the script from there at boot time.
i think
sudo sed -i -e "s/pi/po/g" filename
change every pi to po in that file, that would be very bad, like for
sudo cat /etc/group | grep pi
spi -> spo
gpio -> gpoo

beta-tester
Posts: 1372
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: Deleting user 'pi'

Mon Sep 11, 2017 3:45 pm

KLL wrote:
Mon Sep 11, 2017 2:48 pm
beta-tester wrote:
Mon Sep 11, 2017 1:05 pm
...
i think
sudo sed -i -e "s/pi/po/g" filename
change every pi to po in that file, that would be very bad, like for
sudo cat /etc/group | grep pi
spi -> spo
gpio -> gpoo
good point!... i will change the script.
(please remove my faulty script from your quote)

here my new script version (v3) with the correction and optimization.

Code: Select all

#!/bin/sh
######################################################################
# (v3)
MOUNT_POINT=/mnt
OLD_USER_NAME=pi
NEW_USER_NAME=new_user
OLD_KEYBOARD_LAYOUT=gb
NEW_KEYBOARD_LAYOUT=de

######################################################################
SCRIPT_DIR="$(dirname "$0")"

######################################################################
patch_a_to_b_in_c() {
    if [ -f $MOUNT_POINT$3 ]; then
        echo -e "\e[32mpatching \e[0m$1\e[32m to \e[0m$2\e[32m in file \e[0m$MOUNT_POINT$3";
        sudo sed -i -r "s/(^|[^-_[:alnum:]])($1)($|[^-_[:alnum:]])/\1$2\3/g" $MOUNT_POINT$3;
    fi
}

######################################################################
# change user $OLD_USER_NAME to $NEW_USER_NAME
patch_user() { patch_a_to_b_in_c $OLD_USER_NAME $NEW_USER_NAME $1; }

patch_user /etc/shadow;
patch_user /etc/shadow-;
patch_user /etc/group;
patch_user /etc/group-;
patch_user /etc/gshadow;
patch_user /etc/gshadow-;
patch_user /etc/passwd;
patch_user /etc/passwd-;
patch_user /etc/subgid;
patch_user /etc/subuid;
patch_user /etc/sudoers;
patch_user /etc/profile.d/sshpwd.sh;
patch_user /etc/systemd/system/autologin@.service;
patch_user /etc/systemd/system/getty.target.wants/getty@tty1.service;
patch_user /etc/lightdm/lightdm.conf;
patch_user /etc/dhcp/dhclient-enter-hooks.d/samba;
patch_user /etc/polkit-1/localauthority.conf.d/60-desktop-policy.conf;
patch_user /etc/xdg/lxsession/LXDE-pi/sshpwd.sh
patch_user /etc/sudoers.d/010_$OLD_USER_NAME-nopasswd;

[ -f $MOUNT_POINT/etc/sudoers.d/010_$OLD_USER_NAME-nopasswd ] && sudo mv $MOUNT_POINT/etc/sudoers.d/010_$OLD_USER_NAME-nopasswd $MOUNT_POINT/etc/sudoers.d/010_$NEW_USER_NAME-nopasswd;

sudo mv $MOUNT_POINT/home/$OLD_USER_NAME $MOUNT_POINT/home/$NEW_USER_NAME;

######################################################################
# change keyboard layout $OLD_KEYBOARD_LAYOUT to $NEW_KEYBOARD_LAYOUT
patch_a_to_b_in_c $OLD_KEYBOARD_LAYOUT $NEW_KEYBOARD_LAYOUT /etc/default/keyboard;

######################################################################
# copy WiFi setup
[ -f $SCRIPT_DIR/wpa_supplicant.conf ] && \
sudo cp $SCRIPT_DIR/wpa_supplicant.conf $MOUNT_POINT/etc/wpa_supplicant/.
{ I only give negative feedback }
RPi B (256MB), B (512MB), B+, ZeroW; 2B; 3B, 3B+; 4B (4GB)

Return to “Beginners”