Using SCP, SSH commands over remote servers to acces and transfer data

We often have to interact with linux servers over remote m/c's for deployment or maintenance issues. I would specify very basic commands which comes handy few times .


Running multiple commands on a remote machine after ssh: 

Write the commands in a shell script and scp the file, now ssh to remote server and execute the file.
In my case i have written a ruby script

ssh ubuntu@ec2-addr /bin/bash << EOF
  exec ssh-agent bash ; ssh-add /home/ubuntu/.ssh/key.pem
EOF

system("ssh ubuntu@ec2-address /bin/bash << EOF
     exec ssh-agent bash
     ssh-add /home/ubuntu/.ssh/key.pem
   
     GIT_TRACE=1 git clone ubuntu@ec2-address:/home/ubuntu/vol/story.git
EOF")

refer: Add Private Key permanently with ssh address on ubuntu

Identity file was added in .ssh/config file. Now when i try to take a git pull, it says permission denied as it's trying to use the key place in config file and not id_rsa.pub which was previously set.
The config file is present in .ssh/config. In case config file is not created then create one.

The file could be
HostName host-address
    User username
       IdentityFile ~/.ssh/key.pem

To manage with multiple keys for different host names, use the below configuration in .ssh/config file.


Host ec2-**-***-**-***.ap-southeast-1.compute.amazonaws.com
  HostName ec2-**-***-**-***.ap-southeast-1.compute.amazonaws.com
    User ubuntu
       IdentityFile ~/.ssh/key_dev.pem

#Default GitHub
Host github.com
  HostName github.com
    User git
      IdentityFile ~/.ssh/id_rsa

Host github-public
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_public

Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_personal


Ssh to a remote instance:

ssh -i path-to-private-key username@instance-address

Scp a file or folder to a remote instance from local m/c:

File: scp -i path-to-private-key path-to-file-on-local-m/c username@instance-address:storage-path

Here you have two options with storage-path, replacing "storage-path" with " ."(Dot) will send the file to home folder of ec2 instance or complete path can be provided.

Folder: scp -r -i path-to-private-key path-to-folder-on-local-m/c username@instance-address:storage-path

Scp a file or folder from a remote instance to local m/c:
run the below commands from local m/c

File: scp -i path-to-private-key username@instance-address:path-to-file-on-remote-mac local-path-to-store-file

Folder: scp -r -i path-to-private-key username@instance-address:path-to-folder-on-remote-mac local-path-to-store-folder



Bijendra Singh

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.

0 comments: