64

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?

Registered User
  • 9,631
  • 14
  • 53
  • 85
  • 3
    good one I never even knew sudo could purged too! linux is just so damn customisable! – user247696 Mar 15 '14 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 '14 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 '14 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 '14 at 04:16
  • @AdityaPatil No argument - I just wanted to make sure people were aware of the difference but wrote too little. – Carl Witthoft Mar 16 '14 at 12:16
  • 4
    Can't you just login as root? – Konrad Höffner Mar 16 '14 at 15:09
  • @CarlWitthoft I wasn't argumenting, I just extended your comment a little further. – Registered User Mar 17 '14 at 02:43
  • Sure, just do sudo apt-get install sudo, oops too late :-) – boatcoder Mar 18 '14 at 03:49
  • If sudo was removed, then why would it be needed to reinstall it if Ubuntu is no longer aware of it? – Erokhane Mar 18 '14 at 06:33

7 Answers7

61

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)

Radu Rădeanu
  • 169,590
  • @RaduRadeanu This is interesting i am going to try this thanks u ...... – Premkumar Mar 15 '14 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 '14 at 10:37
  • could I theoretically sudo apt-get purge pkexec? – BlueBuddy Mar 15 '14 at 12:57
  • @Thebluefish No. Doesn't exist such a package called pkexec. – Radu Rădeanu Mar 15 '14 at 13:14
  • Ah good to know. Pretty new to Linux myself, and I didn't want to take the chance :p – BlueBuddy Mar 15 '14 at 13:16
  • 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 'SO- stop being evil' Mar 15 '14 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 '14 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 '14 at 22:56
  • 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. – Paddy Landau Mar 17 '14 at 19:55
  • @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? – Radu Rădeanu Mar 17 '14 at 20:01
  • i got bash: pkexec: command not found lol – Vladimir Ch Feb 02 '22 at 12:49
29

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

  • 16
    can you add the essential and relevant part from the link here? – Registered User Mar 15 '14 at 13:04
  • If there's no recovery available in the grub boot menu, press e 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 '14 at 05:18
  • 1
    How did you get internet connection on root shell? – Avinash Raj Mar 23 '14 at 14:19
22

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.

Avinash Raj
  • 78,556
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.

Braiam
  • 67,791
  • 32
  • 179
  • 269
Jules
  • 301
1

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

Avinash Raj
  • 78,556
  • 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 '14 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 '14 at 13:20
-1

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.

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

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).

orion
  • 97
  • This may be useful information, but it doesn't answer the question. – Luís de Sousa Mar 17 '14 at 08: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 '14 at 08: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 '14 at 09:39