You are not logged in.

#1 2025-11-25 15:39:26

archie83
Member
Registered: 2025-11-22
Posts: 6

Avoid typing sudo for each command

How is it possible to ommit sudo if sudo was added to a command within the lifetime of a sudo session. Currently, i have to add sudo for each command which requires sudo. Assume the following scenario and assume the lifetime of sudo is 5 minutes
1. Run "sudo pacman -Syu"
2. Run "pacman -Syu"
3. Wait 6 minutes
4. Run "pacman -Syu"

So, currently, step 2 and 4 fails because of the following error: error: you cannot perform this operation unless you are root. But i only want that step 3 fails.

Offline

#2 2025-11-25 15:51:07

WorMzy
Administrator
From: Scotland
Registered: 2010-06-16
Posts: 13,462
Website

Re: Avoid typing sudo for each command

You cannot run sudo without calling sudo.

"sudo pacman -Syu" runs pacman as root.
"pacman -Syu" runs pacman as whatever user is running the command (i.e. you).

If you want to run things as root without prefixing each command with 'sudo', become root. e.g.

sudo -i
pacman -Syu

Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

#3 2025-11-25 16:01:15

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,086

Re: Avoid typing sudo for each command

You could wrap pacman in a local script or function that checks the parameters and injects sudo if $UID != 0
sudo takes care of credential caching but iirc it defaults to 5 minutes, you'd have to extend that in your sudoers

Offline

#4 2025-11-25 16:04:37

twelveeighty
Member
Registered: 2011-09-04
Posts: 1,448

Re: Avoid typing sudo for each command

If you're talking specifically "pacman -Syu", you can add an alias to ~/.bashrc: `alias syu='sudo pacman -Syu'`. Then calling `syu` will always invoke `sudo pacman -Syu`.

Offline

#5 2025-11-25 17:41:43

archie83
Member
Registered: 2025-11-22
Posts: 6

Re: Avoid typing sudo for each command

I wanted it to work this way with all commands, pacman was only an example. I thought it worked that way in Ubuntu but i might be mistaken.

Offline

#6 2025-11-25 17:44:54

archie83
Member
Registered: 2025-11-22
Posts: 6

Re: Avoid typing sudo for each command

WorMzy wrote:

You cannot run sudo without calling sudo.

"sudo pacman -Syu" runs pacman as root.
"pacman -Syu" runs pacman as whatever user is running the command (i.e. you).

Why not? Since the sudo session is still active, the command can simply become the root user. I mean i don't have to type in the password anyways, so why is there a need to add "sudo"?

Offline

#7 2025-11-25 17:45:34

Scimmia
Fellow
Registered: 2012-09-01
Posts: 13,724

Re: Avoid typing sudo for each command

I really doubt it did. That would be a MASSIVE security and safety issue, to just run any random command as root because the password was used a bit ago.

Offline

#8 2025-11-25 18:41:58

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,086

Re: Avoid typing sudo for each command

I wanted it to work this way with all commands, pacman was only an example … I mean i don't have to type in the password anyways, so why is there a need to add "sudo"?

Because something needs to elevate the privilege.

That can be
* you
* the process (which has several implications, see below)
* you via a local script/alias/whatever

Processes can detect that they're run as unprivileged user and know that they require UID0 privileges (pacman does that)
They can then promt the user to authorize elevation (eg. systemctl does that, using polkit)
The problem is that the process either needs to hard depend on an elevation approach (sudo, doas, pkexec, su, …) or mandate a configuration for that and at this point you're equivalent to

seth wrote:

You could wrap [foo] in a local script or function that checks the parameters and injects sudo if $UID != 0

I thought it worked that way in Ubuntu but i might be mistaken.

Ubuntu *might* have patched such self-elevation into a limited set of specific processes but I believe that when I see it.
In case of sudo it would also be necessary to check for a tty and either abort or use sudo -S and some GUI input for the password prompt.

Offline

#9 2025-11-25 19:34:03

twelveeighty
Member
Registered: 2011-09-04
Posts: 1,448

Re: Avoid typing sudo for each command

IIRC Ubuntu used the 'wheel' group a lot and also pre-configured elevation systems to make it appear as if you could run root-privileged tools as non-root.

In the off-chance that this is more of an X/Y problem: do you actually just despise typing five extra characters or are you running into some kind of scripting / configuration issue where the `sudo ` prefix is problematic?

Offline

#10 2025-11-25 20:44:06

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,086

Re: Avoid typing sudo for each command

Fwwi, I'm pretty sure ubuntu added you to %wheel and set that to "ALL = NOPASSWD: ALL" but I'm not sure they injected automatic sudo leverages into random commands (it's been a couple of years, though)

Offline

#11 2025-11-25 22:59:52

cryptearth
Member
Registered: 2024-02-03
Posts: 2,092

Re: Avoid typing sudo for each command

// --- edit: well - had to cut it down a bit

archie83 wrote:

How is it possible to ommit sudo if sudo was added to a command within the lifetime of a sudo session.

it isn't - because that's what history came up and stuck with - if you don't want to use sudo don't use linux - but go use windows

Offline

#12 2025-11-25 23:03:50

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 75,086

Re: Avoid typing sudo for each command

Ftr, you can use su, doas, pkexec and, if need be, run0 (though it's not strictly a privilege escalation tool and has more implications) instead of sudo.
The only way to automatically run a process as root is to set the SUID bit on a root owned binary, which is generally a TERRIBLE IDEA and I'm just bringing this up to tell you to under no circumstances consider that a valid approach in case you're researching the topic.

Offline

Board footer

Powered by FluxBB