Friday, December 1, 2017

bash - How to copy clone folder to remote computer in one line?



I have two machines with the same folder structure. One is my personal computer, the other is a remote computer. Sometimes I want a program to run on the remote computer.

At the moment I always need to




  1. ssh to the remote computer

  2. Type my password

  3. copy the folder manually using rsync



Even if I would write a script for rsync (like the one shown below) I would still need to enter my password every time or hard code it inside the bash script (which would be stupid).




#!/bin/bash/
pathToSync="/path/to/my/Folder/"
rsync -r -l -p --progress "$pathToSync"/ myUsername@nameOfRemoteComputer:"$pathToSync"


Has someone an idea of how to solve this problem more efficiently?
Such that in the end I only need to write



sync myFolder nameOfRemoteComputer



The question has been answered.
As Addition to the correct answer given below. Here are the the bash commands to make ssh and rsync work without having to enter passwords. On the client computer type:



mkdir ~/.ssh # Maybe this folder already exists
chmod 700 ~/.ssh
cd ~/.ssh
ssh-keygen -t rsa # This creates a public key
cp authorized_keys authorized_keys_Backup # This backups old / existing keys
cat id_rsa.pub >>~/.ssh/authorized_keys
ssh-copy-id nameOfRemoteComputer




Done! (-> Check if ssh works without password now)
(I have the same username on client and remote computer otherwise it would be ssh-copy-id myRemoteUsername@nameOfRemoteComputer)



SCP



Since you have ssh access you can just use command scp to do your work.



In terminal enter:




scp -r /path/to/my/folder myUsername@nameOfRemoteComputer:/path/to/my/folder



If you also use the same username on both computers you can even ommit myUsername@ and just enter:



scp -r /path/to/my/folder nameOfRemoteComputer:/path/to/my/folder



The name of the remote copumter can be an IP, a domain name or a name assigned at /etc/hosts.



Avoid Entering Password for SSH Connection




You could setup an SSH Key Pair between your server and your local machine.



I would suggest you use Passwords and Keyes app. It should come pre-installed on Ubuntu.



enter image description here



New Key Pair



Press the green + and create a new Secure Shell Key. Follow the dialog boxes to create a new key pair.




Here you can find terminal commands to generate the key pair id_rsa and id_rsa.pub, you will need to get a copy of id_rsa.pub to your server to make the key pair work.



Add the contents of id_rsa.pub to the file ~/.ssh/authorized_keys at your server in a new line. You could use command: cat id_rsa.pub >>~/.ssh/authorized_keys after login in through ssh.



If you set a passphrase to the private key you will have to enter the passphrase to ulock it. Assign no passphrase for ease of use.


No comments:

Post a Comment

11.10 - Can't boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...