Skip to main content

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

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

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 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-**-***-**-***
  HostName ec2-**-***-**-***
    User ubuntu
       IdentityFile ~/.ssh/key_dev.pem

#Default GitHub
    User git
      IdentityFile ~/.ssh/id_rsa

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

Host github-personal
  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


Popular posts from this blog

upload images to AWS::S3 in ruby using aws sdk gem

Using gem aws-sdk for a ROR application for uploading images to s3
Uploading images to a fixed bucket with different folders for each object or application. The s3 keeps a limitation on the number of buckets creation whereas there is no limitation for content inside a bucket. This code will upload image for a user to s3 using aws-sdk gem. The bucket and the image uploaded are made public, so that the images uploaded are directly accessible. The input is takes is the image complete path where it is present, folder in which it should be uploaded and user_id for whom it should be uploaded. def save_screenshot_to_s3(image_location, folder_name,user_id) service = => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY) bucket_name = "app-images" if(service.buckets.include?(bucket_name)) bucket = service.buckets[bucket_name] else bucket = service.buckets.create(bucket_name) …

Fresh server start after instance degraded

So the issue was, amazon issued an warning regarding the server to be terminated, before i could think of making an backup the instance stopped.

The troubleshoot operations like reboot, stop and start didn't worked.
Simple Steps to start a fresh instance with the previous content on server.