Set hostname and FQDN in Ubuntu without reboot

Set hostname and FQDN in Ubuntu without reboot

I will first start with definition of FQDN because many people have confusion over it. Hostname and FQDN both are little bit different.It is best to give the reference from wikipedia so that there should be no dispute.

FQDN or Fully Qualified Domain Name: With reference form Wikipedia

A fully qualified domain name (FQDN), sometimes also referred as an absolute domain name,[1] is a domain name that specifies its exact location in the tree hierarchy of the Domain Name System (DNS). It specifies all domain levels, including the top-level domain and the root zone.[2] A fully qualified domain name is distinguished by its lack of ambiguity: it can only be interpreted one way. FQDNs first arose out of the need for uniformity as the Internet was quickly growing in size in the late 1980s

For example, given a device with a local hostname myhost and a parent domain name example.com, the fully qualified domain name is myhost.example.com. The FQDN therefore uniquely identifies the device —while there may be many hosts in the world called myhost, there can only be one myhost.example.com. In the Domain Name System, and most notably, in DNS zone files, a fully qualified domain name is specified with a trailing dot. For example,

somehost.example.com.

Hostname : With reference from Wikipedia

A hostname is a label that is assigned to a device connected to a computer network and that is used to identify the device in various forms of electronic communication such as the World Wide Web, e-mail or Usenet. Hostnames may be simple names consisting of a single word or phrase, or they may have appended a domain name, which is a name in a Domain Name System (DNS), separated from the host specific label by a period (dot). In the latter form, the hostname is also called a domain name. If the domain name is completely specified including a top-level domain of the Internet, then the hostname is said to be a fully qualified domain name (FQDN).

Example: ‘saturn’ and ‘jupiter’ could be the hostnames of a couple of devices connected to a network called Alpha. Within Alpha the devices are reached by their hostname. Alpha could be configured so that its domain name is ‘alpha’ (in lower case letters). In that case, the mentioned devices’ hostnames, including the domain-name, would be ‘saturn.alpha’ and ‘jupiter.alpha’, respectively. With these names they could be reached in a private network of networks, each with its own domain name. If alpha is registered and can be reached as alpha.net on the Internet, then the fully qualified domain names for the devices would be ‘saturn.alpha.net’ and ‘jupiter.alpha.net’

How to find hostname in linux by command

(1) To get the hostname of system use the below given command

hostname

See the output of command

root@mailubuntu:~# hostname
mailubuntu
root@mailubuntu:~#

How to find FQDN in linux by command

Use the below command in linux to find FQDN

hostname -f

See the below given output

root@mailubuntu:~# hostname -f
mailubuntu.example.com
root@mailubuntu:~#

How to set hostname and FQDN in Ubuntu without reboot

Step 1: Get the ip address of system by using the command ifconfig

Step 2: Edit the /etc/hosts file. And follow the given pattern

IP-Address-of-system hostname.domainname.TLD hostname
eg.
192.168.168.15 mailubuntu.example.com mailubuntu

Here,
Hostname = mailubuntu
Domainname = example
TLD = .com

Note: Read about TLD

FQDN = hostname+domainname+tld
Hence, FQDN = mailubuntu.example.com

While installating Ubuntu, system asked me for giving hostname at that time I gave hostname as mailubuntu. Hence , in /etc/hosts file you can see the line 127.0.1.1 mailubuntu, which is I commented by using # symbol.

So,uncomment the line “127.0.1.1 hostname”
Write new line with this pattern as I described short above

IP-Address-of-system hostname.domainname.TLD hostname
eg.
192.168.168.15 mailubuntu.example.com mailubuntu

See the reference of my system’s /etc/hosts file

root@mailubuntu:~# vi /etc/hosts
127.0.0.1	localhost
#127.0.1.1	mailubuntu
192.168.168.15  mailubuntu.example.com mailubuntu
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
root@mailubuntu:~#

Step 3: Edit /etc/hostname file and give hostname. Here again hostname I am using is mailubuntu

root@mailubuntu:~# vi /etc/hostname
mailubuntu

Step 4 : Now restart the hostname service.

/etc/init.d/hostname restart

or 

/etc/init.d/hostname.sh restart

Step 5: Now check hostname and FQDN by using given below command

hostname  (for hostname)

hostname -f (for FQDN)

See the below help option of hostname command

root@mailubuntu:~# hostname -h
Usage: hostname [-v] [-b] {hostname|-F file} set host name (from file)
hostname [-v] [-d|-f|-s|-a|-i|-y|-A|-I] display formatted name
hostname [-v] display host name

{yp,nis,}domainname [-v] {nisdomain|-F file} set NIS domain name (from file)
{yp,nis,}domainname [-v] display NIS domain name

dnsdomainname [-v] display dns domain name

hostname -V|–version|-h|–help print info and exit

Program name:
{yp,nis,}domainname=hostname -y
dnsdomainname=hostname -d

Program options:
-s, –short short host name
-a, –alias alias names
-i, –ip-address addresses for the host name
-I, –all-ip-addresses all addresses for the host
-f, –fqdn, –long long host name (FQDN)
-A, –all-fqdns all long host names (FQDNs)
-d, –domain DNS domain name
-y, –yp, –nis NIS/YP domain name
-b, –boot set default hostname if none available
-F, –file read host name or NIS domain name from given file

Description:
This command can get or set the host name or the NIS domain name. You can
also get the DNS domain or the FQDN (fully qualified domain name).
Unless you are using bind or NIS for host lookups you can change the
FQDN (Fully Qualified Domain Name) and the DNS domain name (which is
part of the FQDN) in the /etc/hosts file.
root@mailubuntu:~#

10 thoughts on “Set hostname and FQDN in Ubuntu without reboot”

  1. On Ubuntu 14.04 LTS there is no /etc/init.d/hostname and `sudo service hostname restart` generates this output stop: Unknown instance: hostname stop/waiting. I think sudo service networking restart works though.

    Reply
  2. Thanks for a quick reply Sharad… all I want is that whatever mail I compose in mutt and send through postfix should reach its destination, and yes, it will also include messages through mixmaster remailer.
    Now you will have to guide whether I should purchase a domain name or not, or are there other methods to do so (including setting own postfix mail server…..). I have BSNL India as my ISP, if this info is of any use.
    Thanks.

    Reply
  3. Dear Sharad,
    Still confused after with domain name / domain address.All my sent mails through mutt and postfix in 14.04 remain undelivered and are returned back to me vide error 742, citing reason for failure as ” fully qualified address required”.
    Consider me a novice in linux. Suppose my computer is ‘ abc@xyz:~$’.
    What is my hostname in this ? I do not have any ‘domain’. Now could you instruct me from scratch how to get a domain name or address and howto configure postfix so that all my mails are delivered to their addressees. I do not want them to be sent through gmail or other MSP facility ,but through postfix…
    Thanks….

    Reply
    • Hello Nashtrik,

      The post is about setting Hostname and FQDN of your system. It is not related to get domain name. You have to buy domain name first from Domain Registerar like Godaddy, namecheap, name.com etc. They are tons of lot domain registerar. select anyone.

      You have to configure postfix . In case you are considering to make it mail server in public domain, then domain name is required to purchase. Setting own mail server has different steps. Let me know if you want to work on postfix mail server.

      Regards
      Sharad

      Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.