[Raspberry Pi 3] How to Change Only the Ubuntu Username Without Changing the User ID (uid)

Linux

This continues from [Raspberry Pi 3] Installing Ubuntu 18.04 LTS (No Keyboard or Display Required).
Leaving the user and password as ubuntu/ubuntu is dangerous, so this is about changing them for now.
Creating a new user and then deleting the ubuntu user would be fine too, but I tried changing only the name while keeping uid=1000 and gid=1000.

Test Environment

Raspberry Pi 3
Ubuntu 18.04.04 LTS 32-bit

How to Do It

You need to log in as root before doing the work, so it may be a little more troublesome than adding a new user and deleting the ubuntu user.

Preparation: Allow root login over ssh (not needed if you have a keyboard and display and can work directly on the console)

  • Because you cannot change it while logged in as ubuntu, edit /etc/ssh/sshd_config so that root can log in with a password.
  • Set the root password.
$ sudo vi /etc/ssh/sshd_config
PermitRootLogin yes にする
$ sudo systemctl restart ssh
$ sudo -s
# passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
# exit

Log in as root and change the ID, home directory, and so on.

# usermod -l washi ubuntu
# usermod -d /home/washi -m washi
# usermod -c WASHI washi
# groupmod -n washi ubuntu
# passwd washi

Log in with the new user ID, set up .ssh/authorized_keys, and make key authentication login possible. After confirming that you can log in with key authentication, change the sshd settings so root login and password login are not possible.

$ sudo vi /etc/ssh/sshd_config
PermitRootLogin yes を削除。
PasswordAuthentication yes →  PasswordAuthentication no に変更。
$ sudo systemctl restart ssh

Login without a key is rejected.

> ssh -l washi 192.168.11.3
 washi@192.168.11.3: Permission denied (publickey). 
Me
Me

In the end, setting PasswordAuthentication no for ssh is more important than changing the uid.