Question: How to run scp command without prompting for any password in Linux?
The password-less operation can be added to cron or schedule in case of reducing manual intervention. The various methods of copy operations include rsync, bacula, and keygen with scp.
1. Generate a key using ssh-keygen for any particular user and server. For example, you can follow this:
# ssh-keygen -t rsa -b 4096 -C "root@localhost" Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 33:4c:b1:a1:b0:f7:3a:85:0c:85:b9:6b:eb:c6:69:8b root@localhost The key's randomart image is: +--[ RSA 4096]----+ | .o. o | | o+ . + | | o.o o | | .+ = | | .o S | | o o o | | o oo | | .* . | | E+o. | +-----------------+
We can see the generated key as shown below with .pub extention:
# ls -l .ssh/ total 8 -rw-------. 1 root root 3239 Aug 13 13:11 id_rsa -rw-r--r--. 1 root root 736 Aug 13 13:11 id_rsa.pub
2. Once the the public key is generated, copy the .pub file as authorized_keys under .ssh directory under user home for the user in destination server. Once done, try to do ssh which will connect without password.
# cat .ssh/id_rsa.pub | ssh root@server2 'cat >> .ssh/authorized_keys'
# ssh root@server2
Now you can copy a file using scp as shown below:
# scp test_file.txt root@server2:/tmp/ test_file.txt 100% 20 0.0KB/s 00:00