Can sudo be reinstalled after being removed?

17,374

Solution 1

I can install applications using:

pkexec apt-get install <appname>

From man pkexec:

  pkexec allows an authorized user to execute PROGRAM as another user. If
  username is not specified, then the program will be executed as the
  administrative super user, root.

So, I suppose that pkexec apt-get install sudo should work as well.


Edit: now I can confirm: yes, sudo can be installed using using pkexec apt-get install sudo:

reinstall sudo

(click to enlarge)

Solution 2

You can always boot into Recovery Mode, drop to root shell and install it without sudo.

Solution 3

Yes, reinstalling sudo package would be possible via chroot method.

  • First boot from Ubuntu live disk.

  • Mount the previously installed Ubuntu partition into whatever directory you want.In my case, i mounted it in /media/ubuntu.

    sudo mkdir /media/ubuntu
    sudo mount /dev/sdaX /media/ubuntu   # /dev/sdaX - previously installed Ubuntu partition.
    
  • By default you didn't able to get internet connection after chrooted into a partition.So run the below command to make it work.

    for d in dev sys run proc; do sudo mount --bind /$d /media/ubuntu/$d ; done
    

Thanks to @Oli for this wonderful piece of code .

  • Now chroot into that mounted directory,

    $ sudo chroot /media/ubuntu
    # apt-get update
    
  • Install sudo package by running,

    # apt-get install sudo
    
  • Now exit out of chrooted environment.

    exit
    
  • Finally boot up your Ubuntu OS.Now test your sudo command, it will surely works.

Solution 4

Boot with the extra parameter init=/bin/sh on the kernel command line. This will put you directly into a root shell, from where you can simply run apt-get install sudo and then reboot. You may need to run /etc/init.d/networking start to get a working network connection first. Far simpler than messing around with recovery CDs or live disks, if you ask me.

Solution 5

If you already set or update the root user account password by this command sudo passwd root then you don't worry about purging sudo.Just login into your root account and then install sudo,

su
apt-get install sudo

enter image description here

Click here to enlarge

Share:
17,374

Related videos on Youtube

Registered User
Author by

Registered User

An engineer interested in Linux and Machine Learning.

Updated on September 18, 2022

Comments

  • Registered User
    Registered User almost 2 years

    This seems to be a chicken-egg problem. The most common task using sudo is installing and removing software.

    sudo apt-get purge <appname>
    

    But sudo itself can be removed.

    sudo apt-get purge sudo # Do not run this command on production computers!
    

    This is where the fun comes

    ubuntu@ubuntu:~$ sudo
    bash: /usr/bin/sudo: No such file or directory
    

    Although it's obvious that no person in his right mind will purge sudo (other than me), someone can be fooled to run this command (not directly, in its hex mode, or whatever it's called) or a person could SSH in disguised as tech guru and do the mess.

    So is there a way of reinstalling sudo?

    • user247696
      user247696 over 10 years
      good one I never even knew sudo could purged too! linux is just so damn customisable!
    • Registered User
      Registered User over 10 years
      @CarlWitthoft Android is a linux derivative, than actually being a linux distro. Also google has made many changes in it(which most linux fans hate). Rooting, which is well known on android smartphone, is nothing but an attempt to gain the powers of real linux on android.
    • Carl Witthoft
      Carl Witthoft over 10 years
      @AdityaPatil No argument - I just wanted to make sure people were aware of the difference but wrote too little.
    • Konrad Höffner
      Konrad Höffner over 10 years
      Can't you just login as root?
    • Registered User
      Registered User over 10 years
      @CarlWitthoft I wasn't argumenting, I just extended your comment a little further.
    • boatcoder
      boatcoder over 10 years
      Sure, just do sudo apt-get install sudo, oops too late :-)
    • Erokhane
      Erokhane over 10 years
      If sudo was removed, then why would it be needed to reinstall it if Ubuntu is no longer aware of it?
    • Eliah Kagan
      Eliah Kagan almost 5 years
  • Premkumar
    Premkumar over 10 years
    @RaduRadeanu This is interesting i am going to try this thanks u ......
  • Radu Rădeanu
    Radu Rădeanu over 10 years
    @Premkumar Test it with precaution! The last command from the screen shoot should be probably in your case only: sudo apt-get install gksu ubuntu-minimal. But better, check which packages was removed in the same time with sudo.
  • Thebluefish
    Thebluefish over 10 years
    could I theoretically sudo apt-get purge pkexec?
  • Registered User
    Registered User over 10 years
    can you add the essential and relevant part from the link here?
  • Radu Rădeanu
    Radu Rădeanu over 10 years
    @Thebluefish No. Doesn't exist such a package called pkexec.
  • Thebluefish
    Thebluefish over 10 years
    Ah good to know. Pretty new to Linux myself, and I didn't want to take the chance :p
  • h3.
    h3. over 10 years
    @Thebluefish The package is called policykit-1 (as of 12.04). It can be removed, but at the expense of removing packages that you'd usually want on a desktop system such as gnome-core and ubuntu-desktop (on a server, it's more likely not to be required by any necessary package). However, Radu, what determines that your account can use pkexec apt-get …? Is this something that you configured, or does it happen automatically, and if so, what determines that an account can do this?
  • Radu Rădeanu
    Radu Rădeanu over 10 years
    @Gilles I don't remember to be made some configurations in this sense in the past. So, I suppose that is happen automatically. If you know what determines that an account can do this, please share; this is outside of my area of expertise.
  • Eliah Kagan
    Eliah Kagan over 10 years
    @Gilles I think members of the sudo group can use pkexec. (My experience is that adding users to this group confers pkexec abilities and removing them from it removes pkexec abilities; in this way, pkexec abilities seem to be conferred the same was as sudo abilities in the default sudoers config, which makes sense since being able to sudo and pkexec as root are mainly what makes a user an administrator in Ubuntu.) I don't think uninstalling sudo will remove the sudo group. But I am not totally sure my suppositions are true under all reasonable circumstances.
  • Eliah Kagan
    Eliah Kagan over 10 years
    I recommend expanding this to explain what you mean by "copy the existing software."
  • web.learner
    web.learner over 10 years
    Not a very easy way to fix it though.
  • Braiam
    Braiam over 10 years
    @Seth without op intervention I seriously doubt it could be fixed without writing another answer.
  • orion
    orion over 10 years
    Well the proper way of doing that would be chroot, but there are so many less barbaric ways. For instance, logging in as root (the NORMAL way), or, if you somehow don't have root password, just set init=/bin/bash as kernel parameter, and you pass by the entire init system including the login authentication.
  • Luís de Sousa
    Luís de Sousa over 10 years
    This may be useful information, but it doesn't answer the question.
  • Oli
    Oli over 10 years
    Indeed. This really isn't that relevant to a system where root's password is disabled and sudo is installed by default. And if you look at the other answers, you'll see the last paragraph isn't true either.
  • orion
    orion over 10 years
    A user that contemplates removing sudo from ubuntu should also consider setting the root password (other questions pointed out how to do that). A user that doesn't care about this stuff, can keep using sudo.
  • BЈовић
    BЈовић over 10 years
    this works if the root account is enabled, and on ubuntu it is usually disabled. su on my pc gives "su: Authentication failure", although I provided correct password
  • Avinash Raj
    Avinash Raj over 10 years
    yes, you are correct that's what i said.If you already updated the root user account password then you don't worry about purging sudo.Now try this, sudo passwd root, it will allows you to update root user account password.Now relogin into your root account by running su command.Once you able to login then you will be free to purge sudo.Now purge sudo by sudo apt-get purge sudo.Login into your root account to reinstall sudo package by running su and apt-get install sudo
  • Paddy Landau
    Paddy Landau over 10 years
    I understand that sudo (with its relatives gksudo and gksu, which are not quite the same as each other despite what people will tell you) is being replaced by pkexec. I have read that sudo will not be present in future versions of Ubuntu, though I don't know if that is correct. When pkexec replaces sudo, I don't know what will replace gksudo.
  • Radu Rădeanu
    Radu Rădeanu over 10 years
    @PaddyLandau I don't think that sudo will not exists in future versions of Ubuntu. Maybe you read about gksudo. See Why is pkexec preferred over gksudo for graphical applications?
  • ExploWare
    ExploWare over 10 years
    If there's no recovery available in the grub boot menu, press <kbd>e</kbd> and add ` single` to the bootline. Then, when booted, set a root password using passwd. Reboot normaly, on the commandline enter su followed by the new password, and run apt-get install sudo (without the sudo-prefix)
  • Avinash Raj
    Avinash Raj over 10 years
    How did you get internet connection on root shell?
  • Vladimir Ch
    Vladimir Ch over 2 years
    i got bash: pkexec: command not found lol