1. Start your machine in recovery mode -> Resume to normal boot. You should get a prompt with root. Follow the steps below:
root@demo:~$ mount -o remount,rw /
root@demo:~$ passwd yourusername
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
root@demo:~$ reboot
2. You can use chroot. Start your machine with Live CD/DVD/USB
Start a terminal and mount your root disk as per the following steps below
Check which is your root disk? :
ubuntu@demo:~$ sudo fdisk -l
Disk /dev/sda: 111,8 GiB, 120034123776 bytes, 234441648 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 083E1E28-FEE5-4BF0-B7CE-84520FB93B9D
Disposit. Start Final Sectores Size Tipo
/dev/sda1 2048 391167 389120 190M EFI System
/dev/sda2 391168 58660699 58269532 27,8G Linux filesystem
/dev/sda3 58660864 234440703 175779840 83,8G Linux filesystem
In this case my root disk is /dev/sda2
ubuntu@demo:~$ mkdir disk
ubuntu@demo:~$ sudo mount /dev/sda2 disk
You can check the files to ensure it's the root partition
ubuntu@demo:~$ sudo ls disk
bin dev home lib media proc sbin sys var
boot initrd.img lib64 mnt root snap tmp vmlinuz
cdrom etc initrd.img.old lost+found opt run srv usr vmlinuz.old
ubuntu@demo:~$ sudo chroot disk
Enter "passwd" command followed by your username
root@demo_chroot:~$ passwd yourusername
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Exit from chroot
root@demo_chroot:~$ exit
Finish unmounting and reboot
ubuntu@demo:~$ sudo umount disk
ubuntu@demo:~$ reboot