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:
- 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.
- 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.
- 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.
sudo !!
might be easier in any case.sudo !!
would make me happy, thanks.