Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
2  
good one I never even knew sudo could purged too! linux is just so damn customisable! –  user247696 Mar 15 at 15:43
    
Not really worth an answer, since it would be too late if this happened, but there is nothing preventing you setting a password for the root user. You can then use su, or log in directly as root if you have physical access to the machine (ssh will generally be configured to reject attempts to log in as root). –  IMSoP Mar 15 at 20:30
    
All answers do depend on some way of accessing the filesystem with privileges. For (counter)example: my Android Asus Memopad does not have sudo and has no "safe boot" or equivalent. The only way I know of is to go to xda-dev and d/l a prebuilt full install package. You're lucky, so to speak, that you don't have this limitation. –  Carl Witthoft Mar 15 at 21:30
    
@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. –  Registered User Mar 16 at 4:16
4  
Can't you just login as root? –  Konrad Höffner Mar 16 at 15:09

7 Answers 7

up vote 48 down vote accepted

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)

share|improve this answer
    
@RaduRadeanu This is interesting i am going to try this thanks u ...... –  Premkumar Mar 15 at 10:31
    
@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. –  Radu Rădeanu Mar 15 at 10:37
5  
@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? –  Gilles Mar 15 at 16:05
1  
@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. –  Radu Rădeanu Mar 15 at 16:22
2  
@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 Mar 15 at 22:56

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

share|improve this answer
15  
can you add the essential and relevant part from the link here? –  Registered User Mar 15 at 13:04
    
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) –  ExploWare Mar 18 at 5:18
    
How did you get internet connection on root shell? –  Avinash Raj Mar 23 at 14:19

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.

share|improve this answer

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.

share|improve this answer

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|improve this answer
    
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 –  BЈовић Mar 17 at 13:15
    
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 –  Avinash Raj Mar 17 at 13:20

sudo (and any other root privilege) only applies to the running OS. If you have been silly and removed sudo (or /usr/ for that matter) and don't have alternates like pkexec you can simply boot from something else, copy the missing software, and restart again.

Physical access nullifies any and all software security your system may have.

share|improve this answer
    
I recommend expanding this to explain what you mean by "copy the existing software." –  Eliah Kagan Mar 15 at 22:59
    
Not a very easy way to fix it though. –  Seth Mar 16 at 2:13
    
@Seth without op intervention I seriously doubt it could be fixed without writing another answer. –  Braiam Mar 16 at 3:16
    
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. –  orion Mar 17 at 7:53

This problem seems to be very ubuntu-specific. As non-ubuntu user I didn't even understand at first why sudo would be a special case in any way (a lot of distributions don't install it by default).

You don't need sudo at all. It's just a lazy shortcut that allows you to execute a root command without actually logging in as root. However, if you have to do anything more than a single command, it's just awful constantly prefixing everything with sudo. Not to mention that using sudo makes users ignorant about how permissions and root account work. It makes much more sense to just login as root, do the administration of the system and logout. That you do with su if you are already logged in as a regular user. Or you could log in directly as root.

Of course you need to have root password set, that's the reasonable configuration, otherwise you have a windows-like system where there are actions that nobody can perform and you are really locked out if somehow sudo isn't available (it requires working /etc/, set $PATH and other things mounted - which you may not have if something goes wrong early in boot).

share|improve this answer
    
This may be useful information, but it doesn't answer the question. –  Luís de Sousa Mar 17 at 8:10
    
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. –  Oli Mar 17 at 8:43
    
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. –  orion Mar 17 at 9:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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