Please note that LinuxExchange will be shutting down on December 31st, 2016. Visit this thread for additional information and to provide feedback.

Hello,

I have an ssh server running in daemon mode, I believe, so /etc/init.d/sshd stop wont work. I've tried finding this listening port with netstat (netstat -atx |grep 22) but there are too many processes to list.

How do I find a pid using netstat?

asked 11 May '10, 16:33

Jasper's gravatar image

Jasper
11112
accept rate: 0%

Please accept an answer, or provide more details so we can help.

(14 Jun '11, 11:36) rfelsburg ♦



This should do the trick for you:

netstat -anp | grep sshd | grep -v "::"

You'll get something like this:

unix  3      [ ]         STREAM     CONNECTED     111331653 17322/sshd: greg
unix  2      [ ]         DGRAM                    111331645 17322/sshd: greg

The '17322' is the PID in this case.

link

answered 11 May '10, 16:48

gregularexpressions's gravatar image

gregularexpr...
197117
accept rate: 50%

add pipe: | perl -ne 'm,. (d+)/([^ ]+): ., && print "$1 $2'

(11 May '10, 20:30) memnoch_proxy

True, or the below awk would work also (with sort and unique to weed out duplicates):

netstat -anp | grep sshd | grep -v "::" | awk -F" " {'print $8'} | awk -F"/" {'print $1'} | sort | uniq

(11 May '10, 22:14) gregularexpr...
netstat -lntp | grep sshd

or

cat /var/run/sshd.pid
link

answered 12 May '10, 05:17

maszynista's gravatar image

maszynista
2513
accept rate: 33%

edited 12 May '10, 05:22

Is there a reason you can't use ps?

ps -ef | grep sshd

would give you the PID easily.

link

answered 11 May '10, 23:50

codebunny's gravatar image

codebunny
40816
accept rate: 38%

As root, run:

netstat -tunap

you should receive something that looks like this:

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name

tcp 0 0 0.0.0.0:37 0.0.0.0:* LISTEN 3425/inetd

tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 3640/X

tcp 0 0 0.0.0.0:113 0.0.0.0:* LISTEN 3425/inetd

tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 7586/sshd

0.0.0.0:* indicates port 22 is listening on all addresses.

To search only for sshd, pipe netstat with grep:

netstat -tunap | grep sshd
link

answered 12 May '10, 07:11

Slug's gravatar image

Slug
31113
accept rate: 0%

edited 12 May '10, 07:17

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×5
×1
×1
×1

Asked: 11 May '10, 16:33

Seen: 99,843 times

Last updated: 14 Jun '11, 11:36

powered by OSQA