You are not logged in.

#1 2025-11-29 13:53:06

mplieske
Member
Registered: 2025-11-29
Posts: 3

How does sudo read password inputs?

I was playing around with how processes on Linux realize stdin, stdout and stderr.

I found out that the file descriptors under /proc/<PID>/fd/ are used for this.
As far as I could find out until now, /proc/<PID>/fd/0 is stdin.

So just to play around, I tried to provide my password to sudo via this file descriptor.

Here is what I tried:

# 1. Run a command with sudo.
sudo cat /etc/os-release

# 2. In another terminal get the PID of sudo
ps -aux | grep sudo

# Output:
# root       17239  0.0  0.0 258212 11552 pts/11   S+   14:22   0:00 sudo cat /asdf
# mplieske   17391  0.0  0.0 231272  2476 pts/10   S+   14:22   0:00 grep --color=auto sudo

# 3. append to stdin of sudo
echo "password" | sudo tee /proc/17239/fd/0

What happened was not what I would have expacted, namely my password being read by sudo also detecting the '\n' appended by the echo command.
What actually happened was, my password being displayed on the terminal where I executed sudo and nothing happening.

So how is this handled by applications like sudo? I guess password input is somehow special, as it has to be handled securely.
Probably also the fact, that password inputs are not displayed is somehow connected to this.

Last edited by mplieske (2025-11-29 13:59:17)

Offline

#2 2025-11-29 13:56:13

mplieske
Member
Registered: 2025-11-29
Posts: 3

Re: How does sudo read password inputs?

Also I figured, that maybe this section in the forum is not the best one for this topic, I didn't find a really fitting one though. sudo is also just an application after all.

Offline

#3 2025-11-29 14:11:02

Acry
Member
Registered: 2025-11-17
Posts: 4

Re: How does sudo read password inputs?

Hi mplieske!

`sudo` doesn’t read passwords from the normal stdin.

Sudo uses the terminal directly.
Password input is done with echo suppression.
Use `sudo -S` to read from stdin explicitly:

echo "mypassword" | sudo -S ls /root
pacman -Qo sudo
/usr/bin/sudo is owned by sudo

https://archlinux.org/packages/core/x86_64/sudo/
Upstream URL:    https://www.sudo.ws/sudo/

https://github.com/sudo-project/sudo/bl … ass.c#L132

ttyfd = open(_PATH_TTY, O_RDWR);

Offline

#4 2025-11-29 14:14:05

Lone_Wolf
Administrator
From: Netherlands, Europe
Registered: 2005-10-04
Posts: 14,565

Re: How does sudo read password inputs?

man sudo wrote:

-S, --stdin
    Write the prompt to the standard error and read the password from the standard input instead of using the terminal device.

The wording suggests that the 'terminal device' is what takes care of that.
You may have to look into sudo sourcecode to figure out what kind of device that is.

Moderator Note:
Configuring sudo is done by root, so I'm moving this to System Administration .


Disliking systemd intensely, but not satisfied with alternatives so focusing on taming systemd.

clean chroot building not flexible enough ?
Try clean chroot manager by graysky

Offline

#5 2025-11-29 14:19:12

mplieske
Member
Registered: 2025-11-29
Posts: 3

Re: How does sudo read password inputs?

Thank you for moving this Thread to the correct forum.

Ah, yes that makes sense, I remember the --stdin option, somehow I totally forgot about that.

I will take a look at the topic of terminal device.

Thank's for the quick responses smile

Offline

Board footer

Powered by FluxBB