Jetson Lost Password Recovery

Recently, I’ve been using an NVIDIA Jetson TX2 running a version of Ubuntu from Jetpack. Unfortunately, I forgot my password, and couldn’t figure out how to get back into the machine. On normal Ubuntu installations on most machines, you can boot into recovery mode and change your user password from there. However, after trying numerous key combinations to no avail, I discovered that there is no Ubuntu recovery mode on the Jetson.

If this has happened to you, there might still be hope. If you haven’t changed the nvidia or ubuntu users, you may be able to log into them with the default passwords — nvidia and ubuntu, respectively.

But, maybe you’re like me and you changed those passwords. How do you get in? A cursory google search does not give promising results — one user says “If you’ve customized passwords and can no longer remember for one of those two accounts, then your options are limited to some fairly time-consuming methods to change it.” 1

That sounds ominous, but there’s actually a method that isn’t too time-consuming that manages to preserve all your data. After I figured out the steps, the actual execution time was about an hour and a half, where most of the time was sitting around and waiting for commands to finish running. Given that the alternative was re-flashing with a clean Ubuntu install, re-installing, re-configuring, and re-building all software, and dealing with lost data, I think this is a good method.

Before we dive in, here’s an overview of the strategy:

  1. Save an image of the disk on the Jetson
  2. Mount that image on your host computer
  3. Change the /etc/shadow file to contain the right password
  4. Re-flash the Jetson with the new image

Step 1: Save an image of the disk

Connect your Jetson to your computer and boot it into recovery mode by holding down the recovery button while pressing and holding the power button. Use the micro USB port on the Jetson and whatever USB output you want into your host computer - I don’t see any reason that the USB A output on the Jetson wouldn’t work, but numerous forum posts specify to use the micro USB port.

If you haven’t already done so, install the sdk manager from NVIDIA on your host computer.

Then, you want to find the flash.sh file inside the sdk directory you just downloaded. For me, this was in nvidia/nvidia_sdk/JetPack_4.2.3_Linux_GA_P3310/Linux_for_Tegra/. cd into that directory, then run the following command. This will save the disk image of the Jetson.

1
> sudo ./flash.sh -r -k APP -G backup.img jetson-tx2 mmcblk0p1

Step 2: Mount the image on your host computer

After running the previous command, you should have two files in the directory you ran the command: backup.img and backup.img.raw. You’ll need to mount the .raw version of the file as a loopback device. This allows us to access the contents of the filesystem.

1
> sudo mount -t ext4 -o loop backup.img.raw /mnt

There should be no output if it works correctly. If you get an error message, double check that you’re using the .raw file and that the size of the raw file is not 0. It will not work with the regular .img file.

Step 3: Change the /etc/shadow file

Get the list of all mounted devices:

1
> df -aTh

At the bottom of the output, you should see something similar to:

/dev/loop12 ext4 28G 16G 11G 59% /mnt

Look for the line near the bottom that has ext4 in the second column - this is the disk image that you just mounted. In the last column, you’ll see the directory the files are in (in the above example, they are in /mnt). Thus, to get to the /etc/shadow/ file in the disk image, you just have to open /mnt/etc/shadow/ in your text editor.

At the bottom of the /etc/shadow/ file, you should see your username (or the nvidia username), followed by a colon, some hash, another colon, and a bunch more information that doesn’t matter for this exercise.

The hash is the hash for the password you entered. You want to replace this with a hash that corresponds to a password you know. There are several options for how to generate a hash for a password, but the easiest option is a bit of a hack: you’re already using an Ubuntu computer as a host computer, which has its own /etc/shadow file, which ostensibly contains hashes to passwords that you already know. Just replace the old hash with one of those.

If it bothers you that both computers have the same password, you can log into your Jetson after this tutorial is over and change the password. You just need to be able to log in once.

Step 4: Reflash the Jetson with a new image

First, unmount the image with the following command, where /mnt is whatever your mount directory was in step 3.

1
> sudo umount /mnt

Then, if you’re still in the NVIDIA sdk directory that contains flash.sh, you should see a subdirectory called bootloader. Inside this subdirectory is a file called system.img. You want to replace the system.img file with the backup.img.raw file.

For example, for my sdk installation, I want to copy backup.img.raw to nvidia/nvidia_sdk/JetPack_4.2.3_Linux_GA_P3310/Linux_for_Tegra/bootloader/system.img.

After you’ve done this, you just need to flash the Jetson normally:

1
> sudo ./flash.sh -r -k APP jetson-tx2 mmcblk0p1

This should take a while, but you can track the progress on the screen. After this is done, you’re safe to disconnect the Jetson from the host computer, connect it to a monitor, boot normally, and test out your new password.


Footnotes

Retrieving Your Top Genres on Spotify Using GDB and Valgrind with ROS Projects