Detecting user@hostname from within ssh session

Viewed 583

I would like to create a version of pwd that returns the full pathway for scp so

user@hostname.ext:directory_path

I found that user can be detected using whoami. The directory path can of course be found using pwd. Can I also detect the hostname.ext part from the terminal? I found that hostname does not return the correct name but instead some abbreviation.

4 Answers

You can use hostname --fqdn to display the fully qualified domain name, ie the hostname (what you probably called an abbreviation) with the DNS domain name.

Example:

amessihel@tizighennif.tg:~$ alias spwd='echo "$(whoami)@$(hostname --fqdn):$PWD"'
amessihel@tizighennif.tg:~$ spwd 
amessihel@tizighennif.tg:/home/amessihel

One option with environment variables:

echo "$USER@$HOSTNAME:$PWD"

Second option with commands:

echo "$(logname)@$(hostname -f):$(pwd)"

Edit: if you want the IP address of your hostname, you can change hostname -f to hostname -i.

The second option is probably more reliable, since those environment variables may not be set. For more info about USER vs whoami vs logname and more, check out some answers to this question: https://unix.stackexchange.com/questions/76354/who-sets-user-and-username-environment-variables

The hostname and the FQDN associated in DNS records with IP addresses are different things and can be totally different names.

  • The hostname you find in /etc/hostname designates the current system specific internal name. Take it as a courtesy nickname you see displayed within your shell prompt once logged-in to the system.
    The hostname is unique to the system.

  • Multiple Fully Qualified Domain Names can point to one or more IP addresses that leads to one or more system hosts.
    An alternative to using DNS services, is to populates entries into the /etc/hosts with IP addresses and FQDN names.
    DNS FQDNs can resolve to different names or IP addresses depending on which DNS service you ask. This is often used to serve varying answers depending on the query location. Local DNS services (within the LAN) often gives different names and IP addresses. A System can be accessed by resolving multiple FQDN DNS Name.

It look like you are after the FQDN of this virtual hosts so it can be used to access it from outside or the internet.

If your local DNS service returns a generic name hpc.cm.cluster, it is because it is the FQDN that points to its LAN IP address within the cluster. It is not an internet exposed FQDN. For finding one of the publicly exposed FQDN and IP address, you must query a public DNS resolver outside the cluster or some other database.

I'd advise you to reroute your question to ServerFault (Networking / System Administration) instead as this place here is about programming related questions.

You can use gethostname() functionality in the socket module.

This will get the hostname of the machine where the Python interpreter is running:

import socket
print(socket.gethostname())
Related