Training

When: Every first Sunday of every month -get a ticket- from $15 (Click Here).

Sunday, April 23, 2017

Tasks: ssh without passw and scp a file from server-A to server-B

Steps:

1- creating keys in server-A and copy them to server-B
$ touch ~/.ssh/filekey

2- creating key:
$ ssh-keygen -t rsa -b 2048

2.1- Enter filename in which to save the key: ~/.ssh/filekey

3- Sending key from A to B:
$ ssh-copy-id -i ~/.ssh/filekey.pub server-username@serverIP
-follow the prompts asking for passw for server-username to access serverIP

4- try it:
$ ssh server-username-B@server-B

5- copy file from A to B
$ scp /dir/server-a/file.tar.gz server-b@serverB-ip:/dirB/incoming-folder

More info: https://www.guyrutenberg.com/2007/10/05/ssh-keygen-tutorial-generating-rsa-and-dsa-keys/

Much better usage:


n public key based method you can log into remote hosts and server, and transfer files to them, without using your account passwords. Feel free to replace server1.cyberciti.biz and client1.cyberciti.biz names with your actual setup. Enough talk, let’s set up public key authentication. Open the Terminal and type following commands if .ssh directory does not exists:
mkdir -p $HOME/.ssh
chmod 0700 $HOME/.ssh

#1: Create the key pair

On the computer (such as client1.cyberciti.biz), generate a key pair for the protocol.
ssh-keygen -t rsa
Sample outputs:
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/vivek/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/vivek/.ssh/id_rsa.
Your public key has been saved in /Users/vivek/.ssh/id_rsa.pub.
The key fingerprint is:
80:5f:25:7c:f4:90:aa:e1:f4:a0:01:43:4e:e8:bc:f5 vivek@desktop01
The key's randomart image is:
+--[ RSA 2048]----+
| oo    ...+.     |
|.oo  .  .ooo     |
|o .o. . .o  .    |
| o ...+o.        |
|  o .=.=S        |
| .  .Eo .        |
|                 |
|                 |
|                 |
+-----------------+
You need to set the Key Pair location and name. I recommend you use the default location if you do not yet have another key there, for example: $HOME/.ssh/id_rsa. You will be prompted to supply a passphrase (password) for your private key. I suggest that you setup a passphrase when prompted. You should see two new files in $HOME/.ssh/ directory:
  1. $HOME/.ssh/id_rsa– contains your private key.
  2. $HOME/.ssh/id_rsa.pub – contain your public key.

Optional syntax for advance users

The following syntax specifies the 4096 of bits in the RSA key to creation (default 2048):
$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/vps-cloud.web-server.key -C "My web-server key"
Where,
  • -t rsa : Specifies the type of key to create. The possible values are “rsa1” for protocol version 1 and “dsa”, “ecdsa”, “ed25519”, or “rsa” for protocol version 2.
  • -b 4096 : Specifies the number of bits in the key to create
  • -f ~/.ssh/vps-cloud.web-server.key : Specifies the filename of the key file.
  • -C "My web-server key" : Set a new comment.

#2: Install the public key in remote server

Use scp or ssh-copy-id command to copy your public key file (e.g., $HOME/.ssh/id_rsa.pub) to your account on the remote server/host (e.g., nixcraft@server1.cyberciti.biz). To do so, enter the following command on your client1.cyberciti.biz:
ssh-copy-id -i $HOME/.ssh/id_rsa.pub user@server1.cyberciti.biz
OR just copy the public key in remote server as authorized_keys in ~/.ssh/ directory:
scp $HOME/.ssh/id_rsa.pub user@server1.cyberciti.biz:~/.ssh/authorized_keys

source: https://www.cyberciti.biz/faq/how-to-set-up-ssh-keys-on-linux-unix/



No comments: