0

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:

But it is there:

$ cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
<no one is so stupid>

Permissions:

sudo$ ll /home/cgi/.ssh/id_rsa
-rw------- 1 cgi cgi 6363 Jul 29  2019 /home/cgi/.ssh/id_rsa

The script is run under user cgi as part of a supervisord. Any help why paramiko can not read it?

** Update **

It looks like that I also can not open it from python3 shell directly can-not-open-ssh-private-key-from-python

But it can be done, using the absolute path:

can-open-ssh-private-key-from-python-absolute-path

So it looks like the ~ is not interpreted as my user (cgi) within the python environment.

> os.path.expanduser("~")
< '/home/cgi'

but I can not chdir or listdir it:

> os.listdir('~')
< FileNotFoundError: [Errno 2] No such file or directory: '~'
gies0r
  • 4,723
  • 4
  • 39
  • 50

1 Answers1

0

As @MartinPrikryl correctly pointed out, the ~ is not found. Here

Execute these commands before initalizing the paramiko connection:

os.environ["HOME"] = "/home/cgi/"
os.path.expanduser("~/.ssh/id_rsa")
gies0r
  • 4,723
  • 4
  • 39
  • 50