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: '~'
6
  • 1
    Can you do open("~/.ssh/id_rsa", "r")? + What does os.path.expanduser("~") return? May 27, 2020 at 6:46
  • @MartinPrikryl Thanks for your response. I did some testing around it and updated the question.
    – gies0r
    May 27, 2020 at 18:35
  • 1
    So what do you get in shell, if you do cd ~, pwd? + What about Python os.path.expanduser("~")? May 27, 2020 at 18:38
  • >>> os.path.expanduser("~") --> /home/cgi. os.listdir('~') --> No such file or directory: '~'
    – gies0r
    May 27, 2020 at 18:40
  • 1

1 Answer 1

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")

Not the answer you're looking for? Browse other questions tagged or ask your own question.