I learned all about how linux keeps track of passwords when I tried out the squeeze image and found out about the "User is required to immediately change their password (root enforced)" message that adding a new user brings up. I was thinking "What IS this?!? I AM root, and I DON'T want this!!!" so after reading just about every manpage on user authentication (even going so far as find / -type f -exec grep "User is required to immediately change their password" '{}' \; ...

).
Suffice it to say I now change the default username of pi before I even insert the sd card into the pi.
the important files are these
/etc/passwd
/etc/group
/etc/shadow
/etc/gshadow
about these files
all of these files basically consist of individual entries, one per line, and each entry consists of several colon delimited fields. so for example, in /etc/passwd, which contains a list of users on the system, you will find the following line.
pi

1000:1000:,,,:/home/pi:/bin/bash
their are 7 fields here: username : (x here means user has encrypted password) : uid : gid : home dir : shell
users are added to groups by placing a comma separated list of users in a group as the last field on that groups line like so.
pi

1001:pi,user2,user3,etc...
here again the x denotes the presence of shadow passwords (eg the gshadow file contains secure group information).
I could go into detail about shadow passwords but thats not needed to change a username.
Since all you need to do is change the word pi to something else in all these files the following will suffice to change your username. the following script will do it (actually I was musing about this for a while and considering releasing my own batch of scripts to customize fresh images akin to raspi-config).
Code: Select all
#/bin/bash
[ $UID = 0 ] || exit 1
#if were here we are root, usually, but be root anyway
echo "you are about to regex things, this is generally considered heavy handed and you are advised to take caution. ctrl-c now to quit without doing anything, otherwise press enter to continue."
#pause the script
line
#ok, the real work
read -p 'Enter Username: ' NEWNAME
#rough test to ensure uniqueness
grep $NEWNAME /etc/passwd
[ $? = 1 ] && exit 1
#back the files up to the fat partition so you can copy them over to a flash stick or something and put them back
cp -t /boot /etc/passwd /etc/group /etc/shadow /etc/gshadow
#do the change
for i in {passwd, shadow, group, gshadow, sudoers}; do
sed -i s/pi/$NEWNAME/g /etc/$i
done
#freshen the backups
for i in {passwd, group, shadow, gshadow}; do cp /etc/$i /etc/$i-; done
#I think were done, normally I do the changes by hand in nano and use regexes very sparingly
#it is possible to back up these file first before doing this by copying them to another directory as root
#so if your careful you can use this blunt instrument of a script
#oh, and I added sudoers up their because otherwise your new username wouldn't have root access via the sudo command
This is the first edition of this tool, I practically wrote it just now, so, feel free to ask a friend or if you have a better idea, then please modify this script as you please.