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
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



Comments

Popular posts from this blog

Cannot allocate memory error while computing files - Setup Swap space

ActionView::Template::Error (Cannot allocate memory - nodejs /tmp/execjs20131021-26716-fckzo3.js 2>&1 Nodejs is used to compile javascript at runtime. In RoR applications, this error comes when server is started in production mode which compiles the assets or while running rake assets:precompile . check for swap space and allocate

RubyConf 2013 at pune..retrieved from drafts

A great experience while interacting with the ruby community from different places. Lots of knowledge and inspiration flows. Met many folks from india and abroad.

Understanding TOP command and purpose

$top top - 12:24:34 up 9 days, 21:58, 0 users, load average: 5.98, 5.32, 4.30 Tasks: 13 total, 1 running, 12 sleeping, 0 stopped, 0 zombie %Cpu(s): 5.5 us, 1.5 sy, 0.0 ni, 92.6 id, 0.0 wa, 0.0 hi, 0.5 si, 0.0 st KiB Mem: 12969522+total, 11112360+used, 18571628 free, 135900 buffers KiB Swap: 0 total, 0 used, 0 free. 49328208 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 40 root 20 0 1466540 912540 12568 S 7.7 0.7 67:03.03 bundle 43 root 20 0 1413152 860252 11116 S 6.7 0.7 65:41.24 bundle The load averages indicate the average number of processes waiting for CPU time over the specified time periods. Shows running processes and their status. Buffer is the amount of data used while it's being written or read. The numbers are in KiB's showing the RAM available on system us - user process sy - system process process ID (PID), user, priority (PR), virtual memory usage (VIRT), resident memory usage (RES), shared memory usage (SHR), CPU usage (%