What if there is no way to recover your last iTunes Persistent ID from your PC/Mac? i.e. You have formatted your computer, have no longer access to it, or even worse, it’s been stolen.

In order to avoid erasing your iPhone/iPodTouch and starting all over again, there is still a chance to retrieve this magic ID, so you can then use it to make your computer’s iTunes library to match it.

Since a couple of guys have already asked about this, I though it might be of some help to describe how to do this.

I am aware that this method may seem fairly discouraging to most users, but I would also add that it’s not as complex at it may seem at first sight.

First of all, you will need to have access to your iPhone’s operating system (i.e. be able to browse through its files). The only way I know so far is to have the iPhone jailbroken. If you don’t know yet what this is,  iClarified has probably the best tutorials I have seen about this.

If you already have your iPhone jailbroken read on.

The idea is to get the iTunes Persistent ID directly from the very same file where it is stored inside your iPhone. This file is binary (and there is no XML equivalent pair as in the PC/Mac version of the iTunes library). The persistent ID is stored here, in a very similar way as Andrew found out for the PC/Mac versions of the library.

The Persistent ID is 8 byte long and spans exactly from byte 37-44. Bear in mind that the iPhone platform is little-endian, while PC and Intel Macs are big-endian. This means that the bytes are stored in a different order on the iPhone and on the computer (either PC or Intel Mac).

Probably I may be getting a bit too technical, so the best way to illustrate this method is to actually describe a sample process to read this ID.

I have chosen a very simple approach where you only need to log on your iPhone via ssh and type a couple of commands.

Get network access from your computer to your iPhone

1) Make sure your iPhone is jailbroken and has the SSH package installed. If not, you can use Cydia or Installer to get it on your device.
2) Connect your iPhone to the same Wi-Fi network your computer has access to. In the example below, my WiFi network is named LUCERO.

wifi1

3) Take note of the IP address of your device. This can be easily retrieved by touching the blue > sign at the right of your WLAN’s name. A new screen with some connection’s details appear. In the screenshot below, the IP address of the iPhone is 192.168.1.101.

wifi2

4) I will highly recommend to turn off momentarily the Auto-Lock feature of the phone, since the SSH connection will be guillotined if the iPhone enters in standby mode (blank screen). Go to Settings, General, Auto-Lock and select Never. You can turn this option on again once you are done reading the ID.

autolock-off

5) Connect via ssh to your phone, using the IP address you have written before (192.168.1.101 in my example). MacOS has a nice SSH client built in, and you only need to type ssh and the address in a command line window (i.e: ssh 192.168.1.101). On Windows, I would recommend downloading and installing putty, a really nice and free ssh protocol client.

putty

6) Once you have your ssh connected to your iPhone you will be prompted to enter a user and a password. User is “root” and password is “alpine” (without the quotes!).

login as: root
root@192.168.1.101's password:
localhost:~ root#

View the contents of the iTunes database file

7) Don’t take my word for it and locate the actual location of the iTunes library file on your iPhone.

localhost:~ root# find / -name iTunesDB
/private/var/mobile/Media/iTunes_Control/iTunes/iTunesDB
localhost:~ root#

8 ) This is the file that contains your lost Persistent ID. You can choose to view its contents as you want, but I think good old vi editor is an easy choice for this purpose. Don’t worry about what you may have heard about the toughness of this editor. We are only going to use it very briefly, so you don’t need to be a unix geek.

vi /private/var/mobile/Media/iTunes_Control/iTunes/iTunesDB

If the above command fails, you need to install the vi package (again, use Cydia or Installer).
The result is an scrambled window with some funny characters. Remember iTunesDB is a binary file, so we need to turn on the hexadecimal editor mode of vi.
Press [escape] to enter in the command mode and type

:%!xxd

Now the window should look like this:

vi-hex

9) Now take note of the bytes 37 to 44 and write them somewhere or copy them to a text file. In the screenshot below, these bytes are  2e94 2b95 87f2 8cde.

10) Hurray you are a genius and have extracted the Persistent ID! The hardest part is now done. Give yourself a kiss and keep on reading. We are almost there.

Re-write the Persistent ID value in the appropriate (byte) order

10) Now write these value again in reverse order (remember the big endianness of the PC/Intel Macs); bearing in mind that a byte is shown as 2 hexadecimal characters, so you have to take them in pairs. I would also remove any white space in between so you can copy and paste the result in iTunes DB Cloner. Lost? Don’t worry, check the sample value: 2e94 2b95 87f2 8cde converted to big endian is DE8CF287952B942E.

Update your computer’s iTunes library to match the Persistent ID on your device

11) Now use this value in iTunes DB Cloner to alter your PC’s iTunes library, so it matches the persistent ID already stored in your iPhone. If you are using a Mac, update the value following Andrew’s instructions.

itdbc-update

12) You can now logoff from your iPhone’s unix shell. Type q! in your vi window. and then logout or press CONTROL+D to end your terminal session. Remember to turn on again the Auto-Lock feature to some value or your iPhone will remain on if you don’t send it to sleep manually by pressing the power button.

… and that’s all folks. Hope it was of some help to those of you as desperate to save your iTunes library as to have read all this.

This post was written in bit of a hurry, so I might have not been clear enough. Any comments or requests for clarifications will be welcomed.