Run a script at remote server without ssh password

Hello,
What I want to do is to run a file on remote server by running a script at localhost but script should not ask ssh password of my remote server when script is executed.

Scenario1:
To copy files from server2 to data server:

$ scp -r root@server2_ip:/var/www/html/*.* /var/

When I enter above code, it asks for my server2 ssh password

Scenario2:
I want to run a script existing in server1 and it will be run at server2

$ ssh ./ex.bash server2_ip "bash -s -- '<--time bye>' '<end>'"

When I enter above code in terminal, it asks for my servers ssh password.

I want to run ssh or scp commands from a script via crontab, but don't know how the script will remember remote server's ssh password.

I tried different variations but no result.

root:sshpass@server2_ip:/path
root@sshpass:server2_ip:/path

How may I do this?

Thanks in advance
Boris

You just need to set up public/private keys.

ssh-keygen Tutorial � Generating RSA and DSA keys | Guy Rutenberg

Hello Gandolf989,
Thanks for your recommendation but it seems a bit intricate..
Btw, password is not localhost ssh password. It's remote server's ssh password

There is really only a couple of steps:

on the local server do the following:

  1. mkdir ~.ssh
  2. chmod 750 ~
  3. chmod 700 ~/.ssh
  4. ssh-keygen -t rsa -b 2048
  5. cat id_rsa_<server_name>.pub

On the remote server do the following:

  1. mkdir ~.ssh
  2. chmod 750 ~
  3. chmod 700 ~/.ssh
  4. touch ~/.ssh/authorized_keys
  5. chmod 600 ~/.ssh/authorized_keys
  6. edit the ~/.ssh/authorized_keys and paste in the contents of the *.pub file from the local server

Then test to see if you can ssh from the local to the remote server without a password.

1 Like

Thanks Gandolf,
I will test it on Saturday and will report the result.

b.regards
Boris

Hello,
After I enter those codes to local and remote servers, it asks again root password to establish an ssh connection. In server permit root login is prohibit. Could it be the reason?

Thanks in advance
Boris

If you are connecting as root to the remote server and the setting is PermitRootLogin=no then you will be unable to do this. If you are connecting as another user (non-zero uid) then this flag will have no effect, however you have to consider what happens with the standard profiles for that account logging in, e.g. does it assume a terminal, lock you into an application etc.

Robin

1 Like