18

I have booted my system from a live Ubuntu CD, and I need to fix some package problems. I have mounted my hard drive, and now I want to run apt-get as if I booted normally. ie change the working directory for apt-get so it will work on my hard drive. I have done this before, but I can't remember the syntax. I think it was only some flag, like this:

apt-get --root-directory=/mnt/partition1 install....

But I only get "Command line option...is not understood". Any ideas?

Avall
  • 850
  • 1
  • 5
  • 14
  • question is not about programming. Probably belongs on Serverfault. (Moderators will migrate it if needed, don't copy&paste it onto Serverfault) – Simon B. Mar 09 '16 at 14:00

3 Answers3

24

Also this should work:

sudo apt-get -o Dir=/media/partitioni1 update
enzotib
  • 1,596
  • 1
  • 8
  • 14
  • 3
    apt-get -o RootDir=/tmp/test_apt sets (almost) all paths to be in the different root. btw on a running system, if you copy /etc/apt, /usr/lib/apt, and mkdir -p usr/lib etc var/cache var/lib/dpkg var/lib/apt/lists/partial var/cache/apt/archives/partial and finally touch var/lib/dpkg/status, then apt is going to work in that root. It can even work as a non-root user if you add the option -o Debug::NoLocking=1. The nolock option is necessary because I couldn't find a way to set the lock file inside the different root directory. – akostadinov Apr 17 '12 at 05:29
  • Work means using search and downloading packages and such operations. Actually installing is not possible if some essential packages are not already there. debootstrap can help if the goal is actually installing packages in a new root for whatever reason. – akostadinov Apr 17 '12 at 05:44
  • @akostadinov Your comments could be written as a good answer here. – WinEunuuchs2Unix Aug 03 '18 at 11:42
  • How would it work if you have a deb file _outside_ of the `Dir`/`RootDir`? I think apt is getting confused with this in my tests. – eftshift0 Aug 23 '23 at 12:24
  • Just made a lousy test and `dpkg --root` does work and does _not_ get confused with the file being outside of the root directory.... but my question about `apt` remains in place. – eftshift0 Aug 23 '23 at 12:33
12

chroot /mnt/partition1

If your system uses several disk partitions you may have to mount some of them in order to get the package system working (I stopped setting up multiple partitions 10 years ago when hard disks started to get too large for raw physical backup).

This wouldn't work if you don't already have a usable debian system in that location. – akostadinov

If you can't get the package system working when chrooting, perhaps it is too messed up to ever be trusted again - in my experience the effort to bring it back to life rarely pays. If that happens, be happy you can still access your HD, backup your data and perform a clean reinstall.

Some relevant comments from other answer:

apt-get -o RootDir=/tmp/test_apt sets (almost) all paths to be in the different root. btw on a running system, if you copy /etc/apt, /usr/lib/apt, and mkdir -p usr/lib etc var/cache var/lib/dpkg var/lib/apt/lists/partial var/cache/apt/archives/partial and finally touch var/lib/dpkg/status, then apt is going to work in that root. It can even work as a non-root user if you add the option -o Debug::NoLocking=1. The nolock option is necessary because I couldn't find a way to set the lock file inside the different root directory. – akostadinov

Work means using search and downloading packages and such operations. Actually installing is not possible if some essential packages are not already there. debootstrap can help if the goal is actually installing packages in a new root for whatever reason. – akostadinov

Community
  • 1
  • 1
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • 4
    This wouldn't work if you don't already have a usable debian system in that location. – akostadinov Apr 16 '12 at 13:38
  • If you can't get the package system working when chrooting, it is to messed up to be ever trusted again - in my experience the effort to bring it back to life rarely pays. My advice is: be happy you can still access your HD, backup your data and perform a clean reinstall. – Paulo Scardine Aug 29 '13 at 20:42
5

Running chroot /mnt/partition1 will start a new shell in which the root of the filesystem is /mnt/partition1. Assuming the apt-get on your hard drive still works correctly, you can proceed from there.

dpkg --root=/mnt/partition1 -i mypackage.deb is an option that doesn't require chroot, but does require you to download the package yourself.

Jander
  • 5,359
  • 1
  • 22
  • 21