2

I own a server under Ubuntu 16 on my LAN where I run a Apache2 with many vHost websites.

One thing bothers me :
The hostname of the server is the one from a vHost instead of the one written in the /etc/hostname file.

Any idea on how to fix this?

I have already edited the hostname and hosts files, run hostnamectl and restarted the system but it's still giving me the name from a vHost instead of giving me the name I want.

The name was shown in the motd for "last login from" when I used the server to connect to another one throught ssh and it show in Hostname part when I run a "Angry Ip Scanner" (a nMap equivalent).

9
  • You need to post more information. the hostname and the apache vHost name are unrelated. You set a new hostname with hostnamectl set-hostname new_hostname . You set the apache server name in the config files and use DNS to point to the server name.
    – Panther
    Jun 27, 2017 at 15:38
  • I have already try to edit the /etc/hostname file and restart the server, I have already try to run hostnamectl commande line, and when I run hostnamectl status it give me the right hostname I want BUT when I ssh to something or when I launch a nmap in my LAN, the server still give the hostname from one website it host under a vHost (ex : Last login: Tue Jun 27 13:18:03 2017 from website.AAA.com instead of Last login: Tue Jun 27 13:18:03 2017 from MYSERV01)
    – mowdep
    Jun 27, 2017 at 15:44
  • nmap and ssh use DNS or perhaps /etc/hosts but not /etc/hostname
    – Panther
    Jun 27, 2017 at 15:46
  • also directly editing /etc/hostname is a bad idea. you can break sudo if you do not also edit /etc/hosts
    – Panther
    Jun 27, 2017 at 15:48
  • ok but the name I want is set up too in my hosts file
    – mowdep
    Jun 28, 2017 at 9:24

1 Answer 1

1

From the comments, there is confusion here between the local hostname as set by hostnamectl , apache server names, and DNS (Domain Name Service).

HOSTMAME

This is a setting only used on your localhost.

To set the hostname of your local computer, use hostnamectl

sudo hostnamectl set-hostname new_hostname

IMO this is preferable to manually editing /etc/hostname and /etc/hosts as it is a single command, less prone to typos, and less likely to break sudo.

DNS

Domain Name Servers (DNS) is separate and define how computers connect to other computers the "phone book" analogy is used to describe how DNS works, but it is a network so that you can use a name rather then an ip address to connect over a network protocol to a server, ie http://google.com rather then an ip address.

For detailed information on DNS see https://webhostinggeeks.com/guides/dns/ or similar.

This is somewhat related to FQDN - https://serverfault.com/questions/269838/what-is-the-difference-between-a-hostname-and-a-fully-qualified-domain-name

In this discussion I will use FQDN to refer to the network name you use to connect to a server over a network protocol such as http or ssh. DNS servers use FQDN only such as foo.com and not foo, /etc/host can use non FQDN such as foo rather then foo.com or even both.

So at some point, as a part of your web server installation / configuration, you registered one or more FQDN with a DNS provider such as godaddy or similar. These services run and maintain DNS servers and, in your case, map your apache name servers to your public ip address.

NETWORK APPS USE /etc/hosts and DNS servers to resolve FQDN

Network applications will typically look first in /etc/hosts to resolve FQDN to an ip address. If there is no entry in /etc/hosts they will use DNS to resolve the IP address.

No network applications use the information a system administrator writes in /etc/hostname to resolve DNS.

NMAP

So when you run nmap, it uses reverse DNS service and /etc/hosts to resolve FQDN.

See the nmap documentation for details - https://nmap.org/book/man-host-discovery.html

See also this discussion on server fault - https://serverfault.com/questions/153776/nmap-find-all-alive-hostnames-and-ips-in-lan

There is probably some option or configuration to force nmap to use /etc/hosts or disable DNS lookup, and perhaps someone will post that information here, but it is beyond this discussion.

The short answer is nmap, ssh, http, and other network protocols use DNS and/or reverse DNS lookup to resolve your FQDN to an ipaddress as defined by DNS servers or /etc/hosts and not /etc/hostname on either your local machine or the remote server.

SSH

SSH will use both /etc/hosts and DNS servers.

See the ssh documentation or https://linux-tips.com/t/disabling-reverse-dns-lookups-in-ssh/222 for how to disable ssh from useing DNS services.

If ssh is not returning the hostname you expect you need to investigate further, see https://serverfault.com/questions/266897/why-is-hostname-lookup-in-ssh-returning-a-different-result for details on how to do this.

So in your case I assume ssh is also using DNS services rather then relying only on /etc/hosts to resolve FQDN you are using on the command line.

My practice I personally do not use my apache FQDN for my local hostname nor do I use my FQDN to ssh into my server nor do I typically pass FQDN to nmap nor do I use NMAP to determine the local hostname of a server.

nmap will tell you the FQDN registered to a FQDN if it exists it does not tell you what is in /etc/hostname of the remote server.

See Apache error "Could not reliably determine the server's fully qualified domain name"

Example:

NMAP by ip address

This works assuming the ip address is public and the remote host is up and not firewalled to block nmap ;)

bodhi@daemon:~$nmap --top-ports 10 8.8.8.8

Starting Nmap 7.40 ( https://nmap.org ) at 2017-06-28 16:10 MDT
Nmap scan report for google-public-dns-a.google.com (8.8.8.8)
Host is up (0.081s latency).
PORT     STATE    SERVICE
21/tcp   filtered ftp
22/tcp   filtered ssh
23/tcp   filtered telnet
25/tcp   filtered smtp
80/tcp   filtered http
110/tcp  filtered pop3
139/tcp  filtered netbios-ssn
443/tcp  open     https
445/tcp  filtered microsoft-ds
3389/tcp filtered ms-wbt-server

Nmap done: 1 IP address (1 host up) scanned in 2.15 seconds

NMAP by FQDN

This also works as long as the name you give is a registered FQDN

bodhi@daemon:~$nmap --top-ports 10 google.com

Starting Nmap 7.40 ( https://nmap.org ) at 2017-06-28 16:10 MDT
Nmap scan report for google.com (172.217.11.238)
Host is up (0.10s latency).
Other addresses for google.com (not scanned): 2607:f8b0:400f:800::200e
rDNS record for 172.217.11.238: den02s01-in-f14.1e100.net
PORT     STATE    SERVICE
21/tcp   filtered ftp
22/tcp   filtered ssh
23/tcp   filtered telnet
25/tcp   filtered smtp
80/tcp   open     http
110/tcp  filtered pop3
139/tcp  filtered netbios-ssn
443/tcp  open     https
445/tcp  filtered microsoft-ds
3389/tcp filtered ms-wbt-server

Nmap done: 1 IP address (1 host up) scanned in 2.36 seconds

NMAP with a bogus / non exisstant FQDN

bodhi@daemon:~$nmap foo.bar
Starting Nmap 7.40 ( https://nmap.org ) at 2017-06-28 16:10 MDT
**Failed to resolve "foo.bar".**
WARNING: No targets were specified, so 0 hosts scanned.
Nmap done: 0 IP addresses (0 hosts up) scanned in 0.55 seconds

If you are having difficulty we need specific information.

  • Post the contents of /etc/hostname and /etc/hosts
  • Tell us the FQDN of all your registered domain names
  • post the relevant sections of your apache configuration files
  • Post the exact nmap and ssh commands you are running and their output.

Without further information we can not clarify any confusion between your local hostname and a network FQDN or debug your connections if the FQDN are not resolving to the remote server you expect.

You must log in to answer this question.

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