Let's say my eth0 interface has multiple IP addresses and I can connect with SSH to any of these. Is there a way to find out which of those I am connected to?

(The background is that certain operations, like removing an IP from the host, is better not done from the given IP address. To avoid this, I want to check this first, before I am closing myself out.)

  • 1
    I'm a little confused. When you connect via ssh, you do something like "ssh user@IP_ADDRESS" right? Or are you looking for the source of the connection?
    – user47584
    Oct 17, 2014 at 16:15
  • @barrycarter The example mentioned in the question makes it quite clear why he is interested in the server IP rather than the client IP. If you connect using a hostname, which resolves to multiple IPs, you won't automatically know, which one you are connected to.
    – kasperd
    Oct 17, 2014 at 16:55
  • @kasperd OK, but then why not connect to "ssh user@IP_ADDRESS" instead? Seems much easier?
    – user47584
    Oct 17, 2014 at 16:56
  • @barrycarter If you find the need to know in the middle of a session, it is easier to type echo "$SSH_CONNECTION" than to log out and start a new ssh connection.
    – kasperd
    Oct 17, 2014 at 16:58

2 Answers 2


You can simply type:


Or if you want only the server IP address and not the other fields:

echo "$SSH_CONNECTION" | cut -f3 -d' '

If you are using screen and reconnect to a previously started screen session, notice that running these commands inside screen will show the connection from which screen was started and not the session where you reconnected to screen.

  • Or echo "$SSH_CONNECTION" | awk '{print $1}' if you prefer awk over cut.
    – ThoriumBR
    Oct 17, 2014 at 16:12
  • @ThoriumBR That would have to be $3.
    – kasperd
    Oct 17, 2014 at 16:55
  • Thanks, especially for mentioning screen - sometimes we do exactly that.
    – dezso
    Oct 20, 2014 at 8:18

The easiest way would be to use netstat and check for connections in there.

user@host:~$ sudo netstat -ntp
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   
tcp      128      0            ESTABLISHED 16774/ssh
[more stuff removed]
  • In my case, this boils down to netstat -ntp | grep :22.
    – dezso
    Oct 20, 2014 at 8:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .