Secure Password Passing Techniques for su/sudo/ssh Commands in Linux Systems

Nov 20, 2025 · Programming · 12 views · 7.8

Keywords: Linux password passing | sudo command | SSH authentication | automation scripts | secure authentication

Abstract: This paper comprehensively examines technical solutions for passing passwords to su, sudo, and ssh commands in Linux environments, focusing on the -S option of sudo command for standard input password verification. It details various automation authentication technologies including sshpass tool, expect scripts, and SSH key authentication. Through comparative analysis of different methods' advantages and disadvantages, it provides secure and reliable password passing solutions suitable for automation scripts and system administration scenarios.

Technical Challenges and Background of Password Passing

In Linux system administration and automation script development, there is frequent need to pass passwords to commands like su, sudo, and ssh for authentication. These commands typically read passwords from terminal devices (TTY) rather than standard input or command-line parameters, creating technical challenges for automation scripts. This paper systematically analyzes multiple password passing technical solutions based on practical application scenarios.

Password Passing Solutions for sudo Command

The sudo command provides the -S option, allowing password reading from standard input instead of terminal devices. This functionality holds significant value in automation scripts. Specific implementation methods include:

echo "password" | sudo -S command_to_execute

The above command passes the password to sudo -S through piping, achieving non-interactive authentication. However, this method presents security risks as the ps command may expose password information. To enhance security, storing passwords in protected files is recommended:

cat password_file | sudo -S command_to_execute

Alternatively, using the here-string operator:

sudo -S <<< "password" command_to_execute

Password Automation Solutions for SSH Connections

SSH connections support both password authentication and public key authentication. In automation scenarios, password authentication requires specific tools for implementation.

Usage of sshpass Tool

sshpass is a tool specifically designed for SSH password automation, allowing password passing through command line or files:

sshpass -p "password" ssh user@hostname

File-based password passing requires ensuring file permission security:

echo 'password' > password_file
chmod 0400 password_file
sshpass -f password_file ssh user@hostname

Automation with expect Scripts

When systems lack sshpass, the expect tool can achieve interactive automation:

expect -c 'spawn ssh user@hostname; expect "password:"; send "actualpassword\r"; interact'

expect simulates user interaction, automatically sending passwords when detecting password prompts, achieving complete login processes.

SSH Key Authentication Solution

For long-term automation needs, passwordless SSH key authentication is recommended:

ssh-keygen -t rsa
ssh-copy-id user@hostname
ssh user@hostname

This method achieves secure authentication through public key encryption technology, avoiding security risks associated with password passing.

Security Considerations and Best Practices

During password passing, attention must be paid to: file permission control, password storage encryption, and network transmission security. Prioritizing SSH key authentication is recommended, followed by sshpass or expect solutions, with sudo -S option as the last resort.

Technical Solution Comparison and Selection

Appropriate technical solutions should be selected for different scenarios: temporary tasks can use sudo -S, SSH connections recommend sshpass, complex interaction scenarios suit expect, and long-term automation should configure SSH key authentication. Various methods have distinct advantages and disadvantages in security, convenience, and compatibility aspects.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.