2

I have a raspberry pi. It has a user pi to which we automatically login on every boot. I need to rename the pi to myuser. To do this, I am following the below steps:

  1. sudo passwd root -> Assign a password to root user
  2. sudo reboot
  3. Login as root
  4. Run commands usermod -l myuser pi usermod -m -d /home/myuser myuser
  5. sudo reboot
  6. Login as myuser
  7. Edit the file /etc/sudoers and change pi to myuser

Following above steps will change the pi user to myuser.

I need to make a script of this. But the problem I am facing is how to keep the script running when rebooting and logging in as root/pi. For example, to login as root and can logout from the pi by using the command

sudo pkill -u pi

This will logout from pi and a login screen appears when I can login as root by entering username (root) and password. But then after login as root, how can I keep my script running so that it can run commands usermod -l myuser pi usermod -m -d /home/myuser myuser. Is there any way of doing this, or any alternative way of doing changing username.

Thanks.

1

It seems that the problem you're running into is that you're trying to modify the user account that you're logged in as. That could be a little tricky. But you could try something like the following:

# Become the root user (if you aren't already)
sudo su

# Set the password for the root user
echo "root:<PASSWORD>" | chpasswd

# Update the system user-datbase files to reflect the name-change
sed -i 's/pi/myuser/g' /etc/passwd
sed -i 's/pi/myuser/g' /etc/shadow
sed -i 's/pi/myuser/g' /etc/group

# Update the sudoers file to reflect the name-change
sed -i /etc/sudoers 's/pi/myuser/g'

# Move the user home directory to the new location (the UID stays the same, we don't need to run chown)
mv -i /home/pi /home/newuser

If you want to run this non-interactively while you're logged in as the pi user, you can create the following script:

#!/bin/bash

# update_user.sh

# Set the password for the root user
echo "root:<PASSWORD>" | chpasswd

# Update the system user-datbase files to reflect the name-change
sed -i 's/pi/myuser/g' /etc/passwd
sed -i 's/pi/myuser/g' /etc/shadow
sed -i 's/pi/myuser/g' /etc/group

# Update the sudoers file to reflect the name-change
sed -i /etc/sudoers 's/pi/myuser/g'

# Move the user home directory to the new location (the UID stays the same, we don't need to run chown)
mv -i /home/pi /home/newuser

And then run it using sudo like this:

sudo update_user.sh

NOTE: I think it's probably not a good idea to have a shell script lying around which contains your root password. You might want to consider not setting the root password programmatically like this.

My original solution is below.


Maybe I'm missing something, but I'm not sure why you have to reboot the device repeatedly. You should be able to make all of the changes that you want and then reboot when you're done. How about trying something like the following:

# Become the root user (if you aren't already)
sudo su

# Set the password for the root user
passwd

# Change the login name of the "pi" user to "myuser"
usermod -l myuser pi

# Update the "myuser" home directory
usermod -m -d /home/myuser myuser

# Edit the file /etc/sudoers and change pi to myuser
visudo

# Reboot the system
reboot

This is a mild alteration of the list of commands that you're using: sudo su first, execute all of the commands, and only reboot once you're done. This still isn't scriptable in its current form, since it requires user interaction - specifically to set the password and edit the sudoers file. If you really want to be able to run this unattended then you would need to automate those two tasks. If that's your goal, then you might want to modify the script to look more like the following:

# Become the root user (if you aren't already)
sudo su

# Set the password for the root user
echo "root:<PASSWORD>" | chpasswd

# Change the login name of the "pi" user to "myuser"
usermod -l myuser pi

# Update the "myuser" home directory
usermod -m -d /home/myuser myuser

# Edit the file /etc/sudoers and change pi to myuser (CAREFUL!)
sed -i /etc/sudoers 's/pi/myuser/g'

# Reboot the system
reboot

See this post for further discussion about setting or changing passwords programmatically:

  • I have tried the way which you have shown. I did execute the commands usermod after sudo su but the commands say pi is already used in the process. Thats why we need to logout from pi and login as root to execute usermod command. – S Andrew Nov 9 '17 at 12:21
  • Can you not log in as root? – igal Nov 9 '17 at 12:26
  • I can log in as root. But then scripting wont be possible. Because if I run a script from pi user i had to log out and log in as root then that script will exit because pi user session will expirer when you log out from pi and log in as root – S Andrew Nov 9 '17 at 12:33
  • What I meant was, can you not log in as root inititally (without logging in as "pi")? Also, could you describe how you're logging in to the system? Are you using ssh? Also, where do you want to run this script? On the target system itself, or on some other host? – igal Nov 9 '17 at 12:35
  • Yes I think I can login as a root initially. And i guess if I logged in as root initially i would be able execute commands edit the visudo file in the root user. I will try this and will update you. I am running the script on target system and I am not using ssh. I am accessing the direct terminal – S Andrew Nov 9 '17 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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