how to change username in ubuntu 20.04 code example

Example 1: rename username ubuntu 20.04

usermod -l <newname> -d /home/<newname> -m <oldname>

Example 2: change username ubuntu

# -l changes login name
# -d changes home directory
# -m copies over contents from old home directory
usermod -l <newname> -d /home/<newname> -m <oldname>

# NOTE: will not work if you are currently logged on as user
# - workaround by temporarily enabling root account and running above command there
# [sudo passwd root] 
# - after root account has been used to change username lock it using
# [sudo passwd -l root]