I am tired everytime I want to run some commands and after I do Enter I find out that this operation require sudo. So I need to :

  1. use arrow up to retrieve my last command
  2. use Home to go to start of the command
  3. add sudo before the command
  4. Enter

This is frustrated process. Why not just prompt me to give the sudo privilege if the command require it? I am think a way like this:

  1. I run a command that need sudo , but I forget to add prefix sudo
  2. system detects that, and ask whether I want to run it as sudo(I just need to enter Y) or ask me to input password or whatever else such that I don't bother to do the process I stated above.
  • 4
    There is no way to know in general from outside the program that it wanted to run as root. Is there a particular set of commands you're encountering this with? sudo !! might be easier in any case. Jul 20, 2019 at 1:58
  • @MichaelHomer Thanks for your help. No, I am not talking about a particular set of commands, I want a generic way. And yes sudo !! would make me happy, thanks.
    – guo
    Jul 20, 2019 at 3:46

1 Answer 1


Nothing like that exists, and I don't think it's possible. Think about this question: How would the shell or kernel or whatever know that you really meant to run that command with sudo? For example, if a file were set to be read/write by root and read-only by everyone else, and you opened the file, how could it know that you wanted to edit the file and not just view it? Or let's say you do 'cp $HOME/myfile $HOME/myfile.bak'. Can the shell know that you really meant to move the file that's in /root, and so you should have used sudo?

For those situations where you do clearly know that you need to be root ('nano /etc/resolv.conf', for example), you'd either have to re-write nano to figure that out beforehand -- by replacing it with a function, alias, or script -- or have the shell somehow read the output of the command, figure out if you got an error and if it was due to not being root, and then try re-running the command with sudo. That would be a really big "if then" statement to account for all the different commands with all their different outputs and ways of working and so on.

If you did manage to write that really big "if then" statement, a couple of potential problems would be:

  • it's never going to entirely work for some commands, it'll just be best effort,
  • there's always going to be new commands that come around that have to be accounted for,
  • it's going to break something if you try to make a command be smart like that, but then you use it in a pipeline or a script that expects a command to just run and either work or fail, not stop and ask a question (like if you have a script 'cp' a config file from one place to another and then restart a service that depends on the file... better to have the script just fail altogether than to think the cp worked when it actually didn't!), and
  • sudo isn't just for root; you can do 'sudo -u bob vim /etc/bobsfile'...

So I don't think it's practical to build in something like that. But rest assured you're not the only one who wishes there were such a thing! I've been working with Linux for 20 years now and still wish it.

There's a couple of things that can help:

  1. Some commands know that they're supposed to be run as sudo and will do something like what you're asking. The systemd commands, for example. Try systemctl restart cups and it will ask you for your sudo password automatically if you're not already running it with sudo.
  2. If you run a command in bash that should have used sudo, you can immediately type sudo !! to run it again with sudo. The "!!" is replaced with whatever your last command was in that shell. On my system I can run "man bash | grep -A75 -P '^HISTORY EXPANSION'" to see how that works. You can also just open "man bash" and search for that.
  3. I have a few aliases for some commands that I would like to just run. For example, alias yum='sudo yum '. I don't have to remember to use sudo for yum. I just run "yum" and it adds sudo for me.

Hopefully something in there helps you out.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .