SoFunction
Updated on 2025-04-13

Implementing the method of automatically entering password in Linux

summary: This article introduces how to use the scp command to automatically enter passwords in Linux environment, including three methods: using SSH key authentication, sshpass tool and expect scripts.

text

1. Authenticate with SSH key

Step 1: Generate an SSH key pair

ssh-keygen -t rsa -b 4096

Step 2: Copy the public key to the remote server

ssh-copy-id username@Remote server address

Through the above steps, the scp command will no longer prompt for a password when transferring the file.

2. Use the sshpass tool

Install sshpass

sudo apt-get install sshpass #Debian Systemsudo yum install sshpass # RHELsystem

Automatic password entry using sshpass

sshpass -p 'Your password' scp document username@Remote server address:Target path

3. Use expect scripts

Expect script example

#!/usr/bin/expect -f
set timeout -1
spawn scp document username@Remote server address:Target path
expect "password:"
send "Your password\r"
expect eof

Run the expect script

chmod +x Script name./Script name

Summarize: Although there are many ways to automatically enter a password, please be sure to pay attention to password security. SSH key authentication is recommended to ensure the security of data transmission.

This is the article about how to automatically enter passwords in scp commands in Linux. For more related contents for Linux scp automatic passwords, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!