How can I install the sudo package?

$ apt-get install sudo
E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?
  • 1
    I can't believe that this is actually possible XD. Note to self: DONT DELETE SUDO! ;) Jan 23 '17 at 2:26

If you've actually deleted the sudo command, you can reinstall the package that provides it by running:

pkexec apt-get update
pkexec apt-get --reinstall install sudo

This works because most Ubuntu systems have two separate ways that let administrators perform actions as root, sudo and Polkit. When sudo is broken, Polkit is usually still intact.

You must be logged in as an administrator to do this, of course. If you're using an account who was allowed to use sudo (back when sudo existed on the system), that should work fine.

On a minimal system or Ubuntu server system, Polkit may be unavailable, and with some configurations you will not be able to run the pkexec command over an SSH session.

You have indicated that pkexec, while not deleted like sudo, is also broken. Specifically, on your system it has the wrong ownership or permissions. It is supposed to be setuid root, but it is not, and therefore it cannot be used.

It would be useful to know how /usr/bin/sudo was deleted, and how /usr/bin/pkexec's permissions (or ownership) got changed. These are two, seemingly separate breaking changes to your system. If a recursive (-R) chown or chmod has been performed on /, /usr, or /usr/bin, that would explain how pkexec's permissions were changed, and an appropriate fix would do more than just changing them back for that one executable.

With that said, if the only two things wrong with your system are that sudo is missing and pkexec has wrong permissions or ownership, this is still easily fixed, though you will have to reboot into recovery mode or chroot in from a live environment. I suggest recovery mode.

  1. Boot in recovery mode.
  2. Remount / readwrite by running: mount -o remount,rw /
  3. Ensure pkexec has correct ownership: chown root:root /usr/bin/pkexec
  4. Fix pkexec's permissions: chmod 4755 /usr/bin/pkexec
  5. Boot into your system (exit and follow the prompts), or reboot with reboot.
  6. Follow the above instructions for reinstalling the sudo package by running apt-get with pkexec.

This has the advantage of working whether or not any Internet connection is available in recovery mode, without involving downloading .deb files and manually installing them. With --reinstall, apt-get will replace all the package files for sudo, which can fix additional problems. Plus, this also fixes your broken pkexec.

However, this is not the only approach. In particular, the method given in San Lin Naing's answer should work, too. The main difference is that, in the method given here, no package management operations are carried out in the chroot, which is only used to repair pkexec so it can then be used to manage packages when booted normally.

  • it's give me error:pkexec must be setuid root Jan 22 '17 at 16:36
  • okey my sudo file is deleted? when i run pkexec apt-get update command it gives me must be setuid root error.i can not install anything and when i try to reinstall sudo it gives error like Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied) E: Unable to lock the administration directory (/var/lib/dpkg/), are you root? Jan 22 '17 at 16:49
  • yes i acidentlly deleted the sudo file. the output of ls -l /usr/bin/sudo is cannot access '/usr/bin/sudo': No such file or directory Jan 22 '17 at 17:01
  • Let us continue this discussion in chat. Jan 22 '17 at 18:35

You need root permission to install any software. But, you had deleted the sudo package.

If you have access as a root, you may be able to install sudo again.

Reference to this answer.

Try to start with bash mode with root user.

  1. Reboot your system.
  2. Long press Shift key or Esc key.
  3. Choose Advanced options for Ubuntu.
  4. Choose Ubuntu, with Linux x.x.x-xx-generic (recovery mode).
  5. Choose root Drop to root shell prompt
  6. Type mount -o rw,remount / command to gain read write access.
  7. Type apt install sudo. Follow on screen instructions.

If installation finished, It is done!

If not, this may need network connection.

Lets reboot your system to normal boot and download required files. If so, Ctrl + D will go menu page again and choose resume. This will go to normal boot.

You may need to install sudo offline. This may be another research. Here you can find .deb package. You can download with your OS version. Download your file that is sudo_1.8.19-3_xxxx.deb. And then remember your download file path.

Reboot your system in bash mode (recovery mode) again and follow 1-6 steps.

And then dpkg -i *file_path/sudo_1.8.19-3_xxxx.deb. This will install sudo. Finished! Ctrl + D and choose resume to normal boot.



yes i acidentlly deleted the sudo file. the output of ls -l /usr/bin/sudo is cannot access '/usr/bin/sudo': No such file or directory

Just a thought:

You still have the media (DVD / USB stick) from which you installed Ubuntu to your system?

  1. Boot from it and mount your PC's system partition.
  2. Copy /usr/bin/sudo from the live system to the mounted partition.
  3. Check that the file's owner and permissions are the same.
  4. Reboot to your system.

You can build sudo from source

mkdir sudo-src
cd sudo-src
apt-get source sudo
cd sudo-x.y.z
./configure --prefix=$HOME
make install
  • 5
    A non-root user who has no way to perform actions as root has no way to install sudo in such a way that it will be able to let users to run commands as other users (such as root). Specifically, the sudo executable must be setuid root, and non-root users cannot change a file's ownership to root. It is very good that this will not work -- if it did, then anyone with an account could escalate their own privileges to root and run arbitrary code by installing their own version of sudo! But unfortunately this means the method given here won't help. Jan 22 '17 at 18:59
  • This is great idea! Where did you get the 'source'? Have you tried this and it was successful? I'm curious how make install works here when the OP doesn't have access to escalation as part of their question.
    – earthmeLon
    Jan 22 '17 at 19:00
  • sudo is an open source project. It's source is available on project's site and likely through your distribution's package manager as source package.
    – loa_in_
    Jan 22 '17 at 23:17
  • @earthmeLon There are several ways to get the source, and if source repos are enabled in /etc/apt/sources.list, the way here will successfully retrieve it. If build-essential is installed, it can even be built this way. But you can never install a working sudo using this method. A non-root user simply does not have the capability to cause sudo or its supporting files to be owned by the root user, and without that, sudo cannot do anything. Try it and see: the make install step fails with an "Operation not permitted" error running chown. Jan 23 '17 at 2:55
  • Yes, I was encouraging @GAD3R to come to that conclusion.
    – earthmeLon
    Jan 23 '17 at 3:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.