Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

An alternative to pacman sudo nag?

+1
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Command repetition with modifications (2 comments)

2 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+1
−1

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

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' or alias pacmans='sudo pacman -S'
  • alias pacr='sudo pacman -R' or alias pacmanr='sudo pacman -R'
  • alias pacu='sudo pacman -U' or alias 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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »