Paramiko 无法访问私钥 [英] Paramiko can not access private key
问题描述
从 Ubuntu 18.04 发行版升级后.到 20.04.
After a distribution upgrade from Ubuntu 18.04. to 20.04.
FileNotFoundError: [Errno 2] No such file or directory: '~/.ssh/id_rsa'
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
self.run()
File "/usr/lib/python3.8/threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "/home/cgi/.local/lib/python3.8/site-packages/distributed/deploy/old_ssh.py", line 50, in async_ssh
ssh.connect(
File "/home/cgi/.local/lib/python3.8/site-packages/paramiko/client.py", line 435, in connect
self._auth(
File "/home/cgi/.local/lib/python3.8/site-packages/paramiko/client.py", line 676, in _auth
key = self._key_from_filepath(
File "/home/cgi/.local/lib/python3.8/site-packages/paramiko/client.py", line 586, in _key_from_filepath
key = klass.from_private_key_file(key_path, password)
File "/home/cgi/.local/lib/python3.8/site-packages/paramiko/pkey.py", line 235, in from_private_key_file
key = cls(filename=filename, password=password)
File "/home/cgi/.local/lib/python3.8/site-packages/paramiko/rsakey.py", line 55, in __init__
self._from_private_key_file(filename, password)
File "/home/cgi/.local/lib/python3.8/site-packages/paramiko/rsakey.py", line 175, in _from_private_key_file
data = self._read_private_key_file("RSA", filename, password)
File "/home/cgi/.local/lib/python3.8/site-packages/paramiko/pkey.py", line 307, in _read_private_key_file
with open(filename, "r") as f:
但它就在那里:
$ cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
<no one is so stupid>
权限:
sudo$ ll /home/cgi/.ssh/id_rsa
-rw------- 1 cgi cgi 6363 Jul 29 2019 /home/cgi/.ssh/id_rsa
脚本在用户 cgi
下运行,作为 supervisord
的一部分.paramiko
无法读取的任何帮助?
The script is run under user cgi
as part of a supervisord
. Any help why paramiko
can not read it?
** 更新 **
看来我也不能直接从python3 shell打开它
It looks like that I also can not open it from python3 shell directly
但是可以使用绝对路径:
所以看起来 ~
在 python 环境中没有被解释为我的用户(cgi
).
So it looks like the ~
is not interpreted as my user (cgi
) within the python environment.
> os.path.expanduser("~")
< '/home/cgi'
但我不能chdir
或listdir
它:
> os.listdir('~')
< FileNotFoundError: [Errno 2] No such file or directory: '~'
推荐答案
正如@MartinPrikryl 正确指出的那样,未找到 ~
.这里
As @MartinPrikryl correctly pointed out, the ~
is not found. Here
在初始化 paramiko
连接之前执行这些命令:
Execute these commands before initalizing the paramiko
connection:
os.environ["HOME"] = "/home/cgi/"
os.path.expanduser("~/.ssh/id_rsa")
这篇关于Paramiko 无法访问私钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!