To change the username in Ubuntu it is strongly advised that you are not logged in with the username you're trying to change. As such we created temporary user and logged in as this user in order to change the username.

sudo useradd tempuser

sudo passwd tempuser

Now logout and log back in with tempuser

Assuming you want to change the current username from bob to john:

sudo nano /etc/hostname

Change the hostname to john or anything that suites your needs.

sudo nano /etc/hosts

Same as above step.

Next, run the following command to change the username from bob to john:
sudo usermod -l john bob

The next step is to rename the home folder from bob to john:

sudo usermod -d /home/john -m john

The final step is to login with john and remove the temporary user created above:

sudo userdel tempuser