In a remote shell, how can I find out from which computer I logged into the remote machine?
ssh always sets the SSH_CONNECTION
environment variable in the remote shell to a value containing the client's and server's host and port.
This also works from non-interactive shells and on machines which do not have utmp/systemd/whatever (e.g. on your router or camera).
ssh root@unq 'echo $SSH_CONNECTION'
192.168.38.152 35466 192.168.38.1 22
Then you can do a reverse host lookup on that address:
ssh [email protected] 'nslookup "${SSH_CONNECTION%% *}"'
...
Name: 192.168.38.152
Address 1: 192.168.38.152 bursku.und
You can use w
, who
or even last
.
Also you can check sshd
logs, journalctl -u sshd -n 100
On my Red Hat 7 machine, I run who am i
or who am I
or who -m
.
The last column will show the machine name where I logged in from (in parenthesis). If I am on my local machine, the last column will show my console/display ID. On my machine it is (:0).
Caveat
This only works on an interactive shell.
ssh ScottieH@RemoteServer who -m
will give unexpected results.
On my Red Hat 7 machine, It spews an error.
YMMV