Find pid of a certain ssh instance

11,856

Give pgrep a go:

pgrep -f 'ssh .* -nNCTR'
Share:
11,856

Related videos on Youtube

Clodoaldo
Author by

Clodoaldo

Updated on September 18, 2022

Comments

  • Clodoaldo
    Clodoaldo over 1 year

    There can be more than one instance of ssh running:

    $ ps aux | grep ssh
    cpn       6098  0.0  0.0  58196  2032 ?        S    10:08   0:01 ssh cz -nNCTR 5433:localhost4:5432
    root      6313  0.0  0.0  64072  1168 ?        Ss   12:22   0:00 /usr/sbin/sshd
    root      6504  0.0  0.0  97816  3856 ?        Ss   15:48   0:00 sshd: cpn [priv] 
    cpn       6508  0.0  0.0  97816  1780 ?        S    15:49   0:00 sshd: cpn@pts/0  
    cpn       6552  0.0  0.0  57680   936 ?        Ss   16:16   0:00 ssh -fNL 5433:localhost4:5433 cz
    cpn       6554  0.0  0.0 103236   860 pts/0    S+   16:16   0:00 grep ssh
    

    pidof returns all running ssh pids:

    $ pidof ssh
    6552 6098
    

    I need to find the pid of the one with the reverse connection (-nNCTR).

    • cha0site
      cha0site over 11 years
      have you tried pgrep?
  • David Airapetyan
    David Airapetyan over 11 years
    Use ps axu | grep '[n]NCTR' to avoid the second grep. Better yet, use pgrep.