PHP Warning: ssh2 auth pubkey file(): Authentication failed for

PHP Warning: ssh2_auth_pubkey_file(): Authentication failed for

1 문제상황[ | ]

  • 공개키 파일을 가지고 SSH 인증을 시도했는데 실패함
root@zetawiki:~# php ssh_with_pubkey.php 
PHP Warning:  ssh2_auth_pubkey_file(): Authentication failed for root using public key: Username/PublicKey combination invalid in /root/ssh_with_pubkey.php on line 4
Public Key Authentication Failed...

2 확인[ | ]

root@zetawiki:~# cat ssh_with_pubkey.php 
<?php
$conn = ssh2_connect( '135.79.246.80' );
if( !ssh2_auth_pubkey_file($conn, 'root',
	'/root/.ssh/id_rsa.pub', '/root/.ssh/id_rsa') ) {
	die("Public Key Authentication Failed...\n");
}
echo "OK...\n";
/root/.ssh/id_rsa.pub, /root/.ssh/id_rsa 파일로 인증시도
root@zetawiki:~# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSUGPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XAt3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/EnmZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbxNrRFi9wrf+M7Q== root@zetawiki.example.com
root@zetawiki:~# cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIGrAgEAAiEA1+FPfSd+EaXYYU0LlPZ0K+k3uWFUb+s/8NxV9PEl1psCAwEAAQIg
RjBfY9W/S4WcgKZIKbqnsjYuQO5nEAGAjx/XY9za48ECEQD2EpdAXBwXDBC9NQGD
yUAJAhEA4JblIvDA6WOWr4KO79oCgwIQUfoFi3pkHUV2uiHTfFzqSQIRAJOcWamn
... (생략)
-----END RSA PRIVATE KEY-----
→ 파일은 둘다 있음

3 키 복제[ | ]

  • ssh-copy-id 명령어를 이용하여 현재 서버의 공개키 파일를 대상서버로 복제해주자.
root@zetawiki:~# ssh-copy-id 135.79.246.80
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@135.79.246.80's password: ********
Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '135.79.246.80'"
and check to make sure that only the key(s) you wanted were added.

4 확인 2[ | ]

root@zetawiki:~# php ssh_with_pubkey.php 
OK...

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}