본문 바로가기

Python

(에러)Asyncssh error Host key is not trusted

Asyncssh를 사용할 때 "SSH connection failed: Host key is not trusted" 에러를 많이 볼 수 있을 것이다.

이것은 hostkey validation을 할 때에 default로 .ssh/known_hosts 파일안의 key들을 확인하는 절차인데

처음 ssh를 연결할 때

다음과 같은 절차를 거치게 되는데 이때 host key가 저장이 되는 것이다.

 

connection = asyncssh.connect(server_ip, username=username, client_keys=client_keys, known_hosts=None, server_host_key_algs=['ssh-rsa'])
async with connection as conn:
	...

 

따라서 다음과 같이 asynssh를 통해 연결하려고 할 때 처음 연결되었다면 known_hosts를 None으로 주어 host key validation 작업을 스킵하는 것이다.

 

Documents:https://asyncssh.readthedocs.io/en/latest/api.html#asyncssh.SSHClientConnectionOptions

  • known_hosts (see Specifying known hosts) – (optional) The list of keys which will be used to validate the server host key presented during the SSH handshake. If this is not specified, the keys will be looked up in the file .ssh/known_hosts. If this is explicitly set to None, server host key validation will be disabled.