0

I'm working on an industrial application for the Pi, and for that we need to, of course, change the username. I don't want to do this manually everytime, so I want to use a script. So far I have written a script for writing the image to the SD card and an install script that installs all the right dependencies, repositories etc. Now I have had good results changing the username manually based on the answer of Mike Lutz in this question, which states

exec sudo -s
cd /
usermod -l newname -d /home/newname -m oldname

unfortunately, this does not work if you want to use it in a script, because the script will still be running on your old username, and therefor the username can't be changed.

So what I have resorted to now, is in the imaging to SD card script, I have written the following:

NEWNAME=pareto
boot_path=/media/pareto/pi_boot
filesystem_path=/media/pareto/pi_filesystem

#check for mounted sd card => unmount
echo "Unmounting SD card"
sudo umount /dev/mmcblk0p1
sudo umount /dev/mmcblk0p2

# mount SD card partitions to the right folders 
echo "Mounting SD card partitions"
sudo mount -t vfat /dev/mmcblk0p1 $boot_path
sudo mount -t ext4 /dev/mmcblk0p2 $filesystem_path

# replace username 'pi' with '$NEWNAME'
echo "Replacing all instances of user 'pi' with '$NEWNAME'"
for i in passwd shadow group gshadow sudoers; do
sudo sed -i "s/:pi/:$NEWNAME/g" $filesystem_path/etc/$i
sudo sed -i "s/^pi:/$NEWNAME:/g" $filesystem_path/etc/$i
sudo sed -i "s/\/pi:/\/$NEWNAME:/g" $filesystem_path/etc/$i
done

# change the home folders name to correspond with $NEWNAME 
sudo mv $filesystem_path/home/pi $filesystem_path/home/$NEWNAME

So far it seems to work, but it feels very dirty. Is there a better way to change the username from a script (either via SSH/UART console) and if not, am I missing some important files I should change as well?

  • 1
    You say "and for that we need to, of course, change the username". There is no of course about it. What do you actually hope to achieve that can't be done by more conventional means? – Milliways May 5 '17 at 8:02
  • 2
    If you insist on doing this why not create a new user with the same permissions as presumably the Pi user (that you want to change) then delete/lockout the pi user. You may also want to look at config management tools that make this easier (e.g. puppet, chef, ansible). – Steve Robillard May 5 '17 at 8:04
  • @Milliways, good point. The reason to change the username is for security (changing only the password while leaving the standard username makes it slightly easier to break into) but also because we just want it to be called our product's name. SteveRobillard, I thought about this but then I would still be unable to delete the pi user using the same script. Thanks for the recommendation, I will look into them. – F. Pareto May 5 '17 at 8:17
  • It sounds like the real problem here is your insistence that everything be done using "the same script". Anyway, the question would be more appropriate to our larger sibling site Unix & Linux. – goldilocks May 5 '17 at 11:08
  • @goldilocks okay let's forget about using "the same script". With ease of setup in mind how would you go about doing this? I want to minimize input from humans to minimize mistakes after programming the 100th in a row. – F. Pareto May 5 '17 at 11:58
2

If I wanted to do this (and I still think it is futile - pi is just a text label for user 1000, and the number can be used in many contexts), I wouldn't do it on a working system.

AFAIK the string pi only appears in 3 places; /etc/passwd, /etc/group and a directory in /home. I would just edit in the appropriate files on a mounted image. (I haven't actually done this so it may need testing.)

  • I generally edit those manually to set up my username. One thing to ensure is that you use the same password for the pi and new user during the transition, otherwise the password hash is wrong which causes problems. – joan May 5 '17 at 8:29
  • It also appears in the /etc/sudoers file, though that may not matter as long as you keep the group memebership the same. – Steve Robillard May 5 '17 at 8:55
  • Are you implying that people would be able to log in using the UID? Otherwise, I don't see how that is an argument for it's futility, especially when one of the stated reasons is: "we just like how it looks". Maybe it's just me but your answer comes across a bit.. strange. I'm sure you mean well, but first you tell me that what I want to do is futile (unnecessary and not true), and then you proceed to say the string pi is in only 3 places when my code shows you there are at least 2 more. For the rest you are proposing the exact thing I'm proposing... Am I missing something? – F. Pareto May 5 '17 at 9:24
  • @F.Pareto I never said it was "not true". You DID NOT say you wanted to change for reasons of vanity in your question, only and for that "we need to, of course, change the username". I actually said "AFAIK the string pi only appears in 3 places", and NO you were asking a question about the problems of changing on a working OS - I suggested a change on an image, where you can do anything without restriction. – Milliways May 5 '17 at 9:48
  • @F.Pareto I do not believe you can login with a numeric ID, but very many GNU/Linux commands (e.g. chown chmod) do permit this. If, as it appears, in your comments you stated you wanted to make a custom image, rather than each being individual, I would have suggested that you actually made a custom image. – Milliways May 5 '17 at 9:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.