7

In a remote shell, how can I find the domain name of the computer from which I logged into the remote machine?

Example: My local machine is mi.pona.com. On this machine I run

ssh toki@sina.pona.com

to login into the remote machine sina.pona.com. In the shell which opens (running on the remote machine) I want to find out from which computer I logged in, so I want to get the result "mi.pona.com". Is there a command for this?

2

2 Answers 2

5

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

5
  • 2
    who am i doesn't work. who am I doesn't work. who -m returns the same as who. and why not just use w?
    – Aaron F
    Oct 8, 2020 at 10:18
  • what about whoami ?
    – JacobIRR
    Oct 8, 2020 at 15:12
  • @AaronF w doesn't show me the address, but w -f does. who am i is showing me the same output as who and who -m. The manpage says "If ARG1 ARG2 given, -m presumed: 'am i' or 'mom likes' are usual." That seems to indicate that who am i should always be the same as who -m. This is GNU's who v8.31.
    – JoL
    Oct 8, 2020 at 15:49
  • @JoL "who am i is showing me the same output as who and who -m" - in which case I take back what I said. I just found a RHEL7 box and tried it and it works. Not sure why it doesn't on my local Manjaro installation, even though my manual for who says "If given two non-option arguments, ‘who’ prints only the entry for the user running it (determined from its standard input), preceded by the hostname. Traditionally, the two arguments given are ‘am i’, as in ‘who am i’". Also, strangely enough, w shows addresses on RHEL but doesn't on Manjaro (I've never tried running w locally before).
    – Aaron F
    Oct 8, 2020 at 16:27
  • whoami vs who am i vs who -m, etc. is OS (Flavor, version & perhaps platform) dependent. Tjat is why I specified all of the programs that I know work on my machine -- and specified the OS version. Also note the YMMV caveat: Your Mileage May Vary). The last OS I used that had w was actually an alias to who -m. Not every one has access to the last database.
    – Scottie H
    Oct 8, 2020 at 16:58
5

You can use w, who or even last.

Also you can check sshd logs, journalctl -u sshd -n 100

Not the answer you're looking for? Browse other questions tagged or ask your own question.