An alternative to pacman sudo nag?
Some pacman functions require sudo. When I forget the sudo, you get:
error: you cannot perform this operation unless you are root.
I then have to press the keys: up, home, s, u, d, o, space, enter
to rerun with sudo. I find this irritating and it breaks my flow.
Is there a better solution here? If sudo is required for an operation, it should be run with sudo, rather than erroring out. In most situations where sudo is dangerous, it would prompt for a password anyway, so there's very little danger here. I have almost never ran pacman with sudo when I didn't mean to, but I have forgotten the sudo many times.
I could alias pacman
to sudo pacman
. However, some operations don't need sudo, like -Q
. That alias would result in needless password prompts, which I also want to avoid.
How can I make pacman automatically use sudo when needed? I'd also like it to participate in sudo caching, so for example I don't want a situation like systemctl
which automatically asks for sudo password when needed, but fails to remember it.
2 answers
You don't. sudo
is for there for security purposes. If you alias it as sudo pacman
than you can mess up without knowing.
You can login as root su -
to execute consecutive commands.
0 comment threads
TL;DR
When pacman bugs you for sudo, in bash or zsh (and possibly others but I haven't verified that), run sudo !!
Full Answer
As I see it you have 2 options: command substitution or an alias (or new command entirely). Neither are a perfect solution but can help lessen the disruption to your flow.
Command History Substitution
This doesn't stop pacman from requiring sudo, nor will it stop it from bugging you, but it will make rerunning it with sudo much easier. If you just ran pacman -Syu
or some similar command that requires sudo and it nags you, you can run sudo !!
and it will autocomplete to sudo <whatever command you just executed>
.
For more info, see the relevant sections of the bash manual and zsh manual. (If anyone can verify that this works, or knows of a similar solution, for other shells, post a comment or edit this answer.)
Make an Alias or New Command
You can define an alias in ~/.bashrc
(or ~/.bash_aliases
if there's a reference to it in .bashrc,) to whichever pacman
command you want to use. For example:
-
alias pacs='sudo pacman -S'
oralias pacmans='sudo pacman -S'
-
alias pacr='sudo pacman -R'
oralias pacmanr='sudo pacman -R'
-
alias pacu='sudo pacman -U'
oralias pacmanu='sudo pacman -U'
This would require you to get muscle memory going to use these commands instead of pacman -S
, etc., and it's not something that would carry over from one system to the next, but it would stop pacman from nagging you about sudo. This could, in theory, also conflict with other potential commands that might have the same name (though I don't know of any that would, off the top of my head).
Making a new command is almost exactly the same, except instead of defining it in .bashrc, you would make a new file somewhere in your path (i.e. any of the directories listed when you execute echo $PATH
) and enter the text in the example aliases after the =
(minus the '
s) there.
1 comment thread