admin管理员组

文章数量:1401013

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 (/) 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 [email protected] 'ls \"/home/BackupGen10/BackupRemote/BackusoTEST/\"'"
proc = subprocess.run(command, text=True, encoding="UTF-8", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

# shell response:
[email protected]'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 [email protected] '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?

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://sourcefe/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 [email protected] 'ls \"/home/BackupGen10/BackupRemote/BackusoTEST/\"'"
proc = subprocess.run(command, text=True, encoding="UTF-8", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

# shell response:
[email protected]'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 [email protected] '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?

Share Improve this question asked Mar 23 at 14:03 ullixullix 4952 gold badges7 silver badges16 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

When run with sudo, the ssh will use root's private keys, not yours. You could see it in the debug output of sudo ssh -v ... (-v stands for verbose, for even more details -v -v).

To use the private key from other location use the -i identity_file option. Example (adjust path):

sudo ssh -i /home/someuser/.ssh/id_rsa ...

Note that if the key is protected by a passphrase, you'll be prompted too, but this time for the key passphrase, not for the login password.

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.

本文标签: pythonssh not accepting access with keygen keysStack Overflow