Using linux scp command (non-interactive) to transfer file to another computer

We will perform the transfer by using non-interactive SCP.


1) Ensure SCP do not prompt for Password

1.1 Generate Private-Public Keys


[sysadmin@sourceMachine~]# ssh-keygen -t rsa


Generating public/private RSA key pair.

Enter file in which to save the key (/home/sysadmin/.ssh/id_rsa): /home/sysadmin/.ssh/id_rsa

Next it will prompt for the passphrase you wish to use. This is basically the password for your key. Just press the [ENTER] key for no passwd.

Enter passphrase (empty for no passphrase)

Enter same passphrase again:

Your identification has been saved in /home/pete/.ssh/id_rsa

Your public key has been saved in /home/pete/.ssh/

They key fingerprint is:


Now if you list the contents of your .ssh directory you should see your private and public key.


[sysadmin@sourceMachine~]# ls .ssh



1.2 Put newly generated public keys in the authorized key file on Target Machine


[sysadmin@sourceMachine~]# scp .ssh/ target@targetMachine


Connect to the remote machine and cat the contents of the public key to a file called authorized_keys in your .ssh directory of your home directory or what ever file your version has specified on the Target Machine's /etc/ssh/.sshd_config file   


[sysadmin@sourceMachine~]# cat >> .ssh/authorized_keys


Be sure to use the double ">>" so you do not overwrite any other authorized keys you may have added to the authorized_keys file. Remove the file from your home directory.   


[sysadmin@sourceMachine~]# rm -f


Ensure only sysadmin user and view and edit the authorized_keys file.   


[sysadmin@sourceMachine~]# chmod 600



1.3 Configure /etc/ssh/.sshd_config file on Target Machine to allow Public Keys Authentication


[root@targetMachine~]# vi /etc/ssh/.sshd_config


Set the following configuration:

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile /home/target/.ssh/authorized_keys


1.4 Restart the sshd service


[root@targetMachine~]# /etc/init.d/sshd restart




2. Ensure sysadmin user is allowed to SSH into target machine


[root@targetMachine~]# vi /etc/ssh/.sshd_config


Set the follow configurations


AllowUsers sysadmin



3. Setup the transfer script in the source machine

3.1 Transfer Script


[sysadmin@sourceMachine~]# vi bin/


Type the following line in the file...

scp -P <port_number> <backup_file> sysadmin@<host_address>:<backup_directory/file_name>


3.2 Ensure the script is executable

[sysadmin@sourceMachine~]# chmod a+x bin/


3.3 Setup crontab to run this transfer script


[sysadmin@sourceMachine~]# vi bin/crontab.txt


Type the following line in the file...


30 4 * * *      /home/sysadmin/bin/


3.4 Run the contab


[sysadmin@sourceMachine~]# crontab bin/crontab.txt