0

My task is to create a bash script that will copy files from server2 in my server1. The issue is that I can't connect from one server to another. I have whitelisted server1 IP on my server2 22 port. When I try to connect, I receive:

Permission denied (publickey).
lost connection

Both servers have the following lines added in /etc/ssh/sshd_config:

PubkeyAuthentication yes
AuthenticationMethods publickey,password
PasswordAuthentication yes

So, can I implement a script that will allow me to connect to the remote server without asking me for a password (as it's a script and I don't want to write my password so anyone can see it) and having the publickey enabled? Both servers are using the same ppk key. Usually, I connect with WinSCP app. I have the same username, password and ppk key on both of them.

CentOS Linux release 7.9.2009 (Core)

Andrew Schulman
  • 8,811
  • 21
  • 32
  • 47
Daniel
  • 1
  • 1
  • This is not really a question for Server Fault. It would better fit on [su]. Also, you did not specify what system you will run your script on. – Martin Prikryl May 03 '21 at 09:53
  • Have you checked file permissions? In $HOME/.ssh folder files authorized_keys, id_rsa and id_rsa.pub should be 600 and 644 for pub file. Did you try using ssh-copy-id? This usually solves the problem if folder permissions are correct – LincolnP May 03 '21 at 17:32
  • Yes, I tried. Same result [root@server1 .ssh]# ssh-copy-id -i root@server2-ip /bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" /bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys Permission denied (publickey). [root@server1 .ssh]# cat ~/.ssh/id_rsa.pub | ssh user@server2-ip 'umask 0077; mkdir -p .ssh; cat >> .ssh/authorized_keys && echo "Key copied"' Permission denied (publickey). – Daniel May 04 '21 at 05:45

0 Answers0