[EdLUG] SSH - ssh-copy-id problem | RPi | Ubuntu.

Justin B Rye justin.byam.rye at gmail.com
Tue Mar 30 12:09:55 UTC 2021


Robert McWilliam wrote:
> On Tue, 30 Mar 2021, at 06:48, Justin B Rye wrote:
>> I don't think the commands are failing, exactly - that ought to give
>> intelligible messages.  The remote machine is just hanging at the
>> point of running it.
> 
> Yeah, one way to check what the SSH session is hanging on would be
> from another shell on the pi run something like:
> pstree -sup $(ps aux | grep sshd |  awk '{print $2}' | sort -n | head -n 1)
> 

As a general rule of thumb, any complicated pipeline like that is
really a job for pgrep (which you probably get in the same package as
ps).  The above simplifies down to

 pstree -sup $(pgrep sshd)

> I don't think pstree is in the default install so would need to add it.
> The sort and head part is to get the  sshd process with the lowest
> PID, that'll probably be the daemon rather than any processes spawned
> from connections if the pi hasn't been on for too long, but if it's
> been on (and doing stuff involving spawning processes) for long enough
> that PIDs have wrapped then it might not be.  In which case something
> like this might work:     
> pstree -sup $(ps aux | grep /usr/sbin/sshd | grep -v grep |  awk '{print $2}')

While I'm playing "shell golf": the "u" option to ps is no help here,
and you shouldn't need to pipe grep into "grep -v grep" when you can
just make the first grep pattern something that doesn't match itself:

  pstree -sup $(ps ax|grep ss[h]d|awk {print\$1})

> or:
> pstree -sup $(systemctl status sshd | grep "Main PID" | cut -d: -f2 | cut -d" " -f2)
> I'm almost inclined to recommend that one as first try since ti is
> going to source of truth about the ssh daemon but I don't know how
> consistent systemd status output is between versions (or you might
> be using a distro that's not using systemd...).

Mind you, we don't really need to do any of this complicated scripting
when we can say that the procedure is
 a) run "systemctl status sshd"
 b) see what it says the "Main PID" is - call that number "$P"
 c) run "pstree -sup $P"
 
> If none of those work then need to find the PID for SSH daemon
> manually, or run "pstree -sup" without a PID, it'll then give the
> whole process tree, if you odn't have a GUI environment running that
> might not be too bad.

Personally if I'm trying to debug something like this I just run

 watch pstree

and rely on my own eyes to pick out the important bits.  The trouble
is, it's unlikely to catch the moment when sshd runs the commandline
sent by ssh-copy-id.  Boosting the logging verbosity of the server
seems more likely to give useful information.
-- 
Justin B Rye
http://jbr.me.uk/



More information about the EdLUG mailing list