pythonssh

ssh not accepting access with keygen keys


I have a Desktop plus NAS setup, and did save backups via ssh. It has worked for years, but then the NAS crashed and was replaced. Both desktop and NAS are now regular computers running Linux Mint LMDE6. But now something is wrong in the SSH workings.

My backup program (https://sourceforge.net/projects/backuso/) needs to be started with sudo, as I need to copy root owned files. SSH is configured with keys to avoid having to enter passwords for the crontab controlled backups. Yet my Python programm is still asking for passwords, like shown here:

# Python code:
command = "ssh root@10.0.0.51 'ls \"/home/BackupGen10/BackupRemote/BackusoTEST/\"'"
proc = subprocess.run(command, text=True, encoding="UTF-8", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

# shell response:
root@10.0.0.51's password: 

But when I take this exact same command out of the big Backuso Python code and paste it into a tiny test program, it works perfectly well, NOT asking for passwords, and the output is as it should be:

Program code:
#! /usr/bin/python3
# -*- coding: utf-8 -*-
import subprocess
command = "ssh root@10.0.0.51 'ls \"/home/BackupGen10/BackupRemote/BackusoTEST/\"'"
proc    = subprocess.run(command, text=True, encoding="UTF-8", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
print("Items in stdout:")
for prs in proc.stdout.split("\n"): print(prs)

Output:
$ ./test_ssh.py 
Items in stdout:
2024-06-18_13-53-45-824424
2024-06-18_13-54-53-855948

What am I missing?


Solution

  • As so often, the problem was not in but in front of the computer :-(

    Once the keys are generated (ssh-keygen) you need to share the public one with each user ($ ssh-copy-id username@remote-user-IP) AND must do this for each user using this, so in particular yourself AND root!

    I may have made a typo when adding key as root, and not notice this failure, and therefor there was no key access for root :-(.

    Sorry for this confusion.