I'm renting a server, running Ubuntu 16.04 at a company, let's call it company.org.

Currently my server is configured like this:

  • name: server737263
  • new name: company.org

Here is my fully qualified domain name:

User@server737263:~ $ hostname --fqdn server737263.company.org

This is not surprising.

I also have domain.org as the domain name, domain.org as domain.org. What I would like to do is rename my server as server1.domain.org.

This means setting my hostname as server1 and my domain name as domain.org.

How can I do this right?

Indeed, the man page for hostname is not clear. For me at least:

SET NAME

  • When called with a single argument or with the -file option, the commands set the NIS/YP hostname or domain name. hostname uses the sethostname(2) function, and all three domain names, ypdomainname and nisdomainname, use setdomainname(2). Please note that this only lasts until the next reboot. Change /etc/hostname for permanent changes.

Full domain name

  • You cannot change a fully qualified domain name with a hostname or DNS name.

Sounds like editing /etc/hostname isn't enough? Because if he did change the hostname, it would change the FQDN. There is also a trick I've read to change the hostname using the sysctl command kernel.hostname=server1 , but nothing says whether this is correct or an ugly trick.

    What is the correct way to set the hostname?

    What is the correct way to set a domain name?

One Solution collect form web for “How to set the hostname and domain name correctly?”

Hostname setup:

    You'll want to edit /etc/hostname with the new hostname.

    Then run sudo hostname $(cat /etc/hostname) .

Setting up your domain:

    Then, in /etc/resolvconf/resolv.conf.d/head , you will then add search your.domain.name to the line search your.domain.name (not your FQDN, just the domain name).

    Then run sudo resolvconf -u to update your /etc/resolv.conf (alternatively, just reproduce the previous change in your /etc/resolv.conf).

Both:

Finally, update the /etc/hosts file. There must be at least one line starting with one of your IP (loopback or not), your FQDN and your hostname. grepping from ipv6 addresses, your hosts file might look like this.

DNS is a protocol used primarily for conversion hostname(hostname), like www.example.com to IP addresses like 192.168.1.0, and vice versa. On the Internet, the relationship between servers is based on IP addresses, not on hostname(hostname) , which the user enters into programs such as browsers or telnet clients. This means that the system needs a tool that would allow you to find the IP address of the server by its hostname(hostname) . There are several solutions for this. For example, reading the /etc/hosts file or querying the NIS server. But the use of DNS is the most common.

As already mentioned, the DNS protocol is used to find IP addresses by hostname and vice versa. This is often used to determine the hostname of the client that is connecting to the server. DNS can also be used to determine the mail server address, as well as to obtain additional information about the host. For example, its location, operating system or its owner. However, most often DNS is used for its intended purpose.

Most systems use the DNS protocol to send queries to a server that looks up IP addresses by hostname(hostname) . A typical system is only a DNS client and never responds to requests from servers or other clients. While many companies and providers have one or more DNS servers on their network. So all other hosts on this network can use them. If your company already has a DNS server, then you do not need to read this chapter.

The domain name system is divided into zones (called domains), each of which has a name like example.com or foo.com.au. Zones have a hierarchy. This means that the foo.com.au zone is in the com.au zone, which in turn is part of the au zone. At the very top of this hierarchy is located. or root(root) zone.

For each zone, there is at least one DNS server, which is the main one and has all the information about that zone. There are also several secondary or slave servers that have a copy of the information from the main server. This will allow you to make a backup of the main server if it becomes faulty. One DNS server can serve several DNS zones or not serve any. The server is usually responsible for providing information about the zones it serves.

Before the server can serve a certain zone, it must be registered with the parent zone. Most parent zones are like . com, .net, .ru are inaccessible to ordinary people and are managed by companies that have the right to register child zones in these parent zones. They are usually called registrars. This means that you can't just install a DNS server that serves a domain like example.com and make it accessible from the Internet. You must pay to have your domain registered. Any registrar company can do this. So, by paying, you will register a domain name that is a subdomain of the main domain, such as a .com domain.

Each zone includes many DNS records, each of which has a name, type, and values. The most common type of record is the address, or A record for short. This record type contains the following information: IP address and associated hostname(hostname) . Another type is NS or name server) records that contain information about the DNS server serving the zone or subdomain. And the third type is MX or mail server) record, which contains information about the host that serves mail for this zone.

Each zone must have at least one additional server that could replace the main server if it is unavailable for some reason. Additional servers will also allow you to distribute the load on the main server. This happens because other servers, in search of information about DNS records, randomly select a DNS server from the desired zone. In fact, there is no way for other servers to know which server is the main one and which is the secondary one for a certain zone.

Additional servers can request a copy of all zone records using an intra-zone transfer. This is done when the secondary DNS server receives a certain zone for service for the first time, and also when the server determines that the zone has changed or the records are out of date. The main server can be configured to notify secondary servers when a zone has changed and they need to update, allowing them to always have new information.

Each zone has its own serial number, which is a simple counter that increments whenever any entry from the zone has changed. This number is used by additional servers to determine whether changes have been made to the zone, and if so, those changes need to be copied. In most cases, it doesn't matter what the serial number looks like. However, some domains require the number format to be date-oriented - YYYYMMDDnn.

Typically, one server contains only master zones or only slaves zones. However, it happens that the server is the main one for some zones and at the same time, additional for others. There is no rule on the number of servers serving a zone. The important zones.com and root (root, main) have 13 service servers, since these zones make up a huge part of the Internet and receive a large number of calls. The more additional servers per zone, the better, as long as they constantly synchronize their content.

When a DNS server receives a request from a client to search for a record, it first checks with itself to see if the required record is included in the zones that it serves. If so, he can respond to the client immediately. However, if the required record is not found on this server, then the server will send requests to other servers to find it. The search begins with one of the servers responsible for the root (main) zone, which will direct to another DNS server, and this to the third, and so on, until the required record is found. If the required record does not exist, then one of the DNS servers will say so and the search will stop.

For example, imagine if a DNS client queries the server for the IP address for the host www.webmin.com.

The server will go through the following steps to determine the IP address:

1. First it will make a request to one of the root servers, such as a.root-servers.net(198.41.0.4), to try to find the IP address for www.webmin.com. The Root server will respond with a list of DNS servers serving zone.com. One of them is a.gtld-servers.net(192.5.6.30).

2. Next, he will make a request to the server serving.com zone requesting the IP address for www.webmin.com. The response will be a list of DNS servers, one of which is au.webmin.com(203.89.239.235). This is the main DNS server for the webmin.com domain.

3. The DNS client will then make a request to the webmin.com server to find out the address for www.webmin.com. The answer will be 216.136.171.204, which is the correct IP address.

4. The DNS client will receive this IP address and cache the result. Now he will know what IP address www.webmin.com is and will not need to go through all these steps again. But this result has a TTL, so after this time, the IP address data will become outdated, and the DNS client will go through these steps again in order to find out the IP address for www.webmin.com. This is done so as not to go through these steps again and again, creating a lot of traffic to the Internet.

As you can see, a DNS server can find the IP address of any host on the Internet by following a simple algorithm. It is impossible to find only the address of the root servers. They are read from a file. Since root server addresses rarely change, they are stored in a specific file.

The relationship between IP addresses and their hostname is stored in DNS separately from the relationship between the hostname and the IP address. This is done in order to make it possible to search for hostname (host name) by IP address using a similar algorithm to the one described above. This is explained by the fact that inconsistencies may occur between the relationships of the IP address to the hostname, and the hostname to the IP address. For example, www.webmin.com is associated with 216.136.171.204, but 216.136.171.204 is associated with usw-pr-vhost.sourceforge.net! This causes confusion, but it is a real fact. When a client wants to find a hostname for an IP address like 216.136.171.204, it converts that address into an entry like 204.171.136.216.in-addr.arpa. As you can see this is the same IP address, just backwards and in-addr.arpa added to the end. The special zone in-addr.arpa is served by root DNS servers, and its subdomains are accessible to other DNS servers. Typically, each Class C zone (for example, 171.136.216.in-addr.arpa) is served by the DNS servers of the companies or Internet service providers that own those zones. This means that they can create entries that define the relationship between an IP address and its hostname. All these records are of a special type - PTR. The big problem is with the method of finding anything less than class C (which includes 256 addresses) and served by a single DNS server. Thus, if a server serves the zone example.com, which contains only one entry, www.example.com with the address 1.2.3.4, then some server will not be able to find this address. Therefore, it is better to have this one entry in the DNS server of the Internet service provider or hosting company whose network includes the web server for www.example.com. Only organizations may need to own a Class C network and maintain the reverse zone for that network using their own DNS server.

Many organizations have an internal network that uses private IP addresses starting, for example, with 192.168. A network like this connects to the Internet through a firewall using NAT. Some people with a home network organize their Internet access as follows. One of the machines has direct Internet access. It is a Gateway. The rest connect through it. It turns out that they operate on the Internet under the same IP address.

Also, in such networks you may need a DNS server to issue hostnames to computers on the internal network. This is possible by creating a zone with an arbitrary name like home or internal, which contains entries about internal computers on the network, and also by creating a reverse zone for the 192.168 network, so that IP addresses can be found upon request. The server can also be configured to look up the real Internet hostname, just like any normal DNS server. However, such a search will lead nowhere, since your local zone is not registered on the Internet.

In Linux, the hostname is used by many services and utilities. If the hostname is not set correctly, you will constantly receive error messages and warnings. It all depends on how you use the operating system. If this is an ordinary home computer, a personal laptop, then you can set any name and ignore errors. And if you are using a computer as a server, then it is very important to set the hostname correctly.
Here are some reasons to configure the hostname:
  • The hostname appears in the Bash prompt, immediately after the @ symbol. This makes it much easier to understand which server you are logged into.
  • MTA (message transfer agent) use the host name when sending letters (depending on the MTA configuration).

What is the difference between a domain name and a hostname?

Oddly enough, this is not such a simple question as it might seem at first glance. The meaning of some terms may vary depending on the context. Let's start with domain name. If you know how Domain Name Systems (DNS) work, then you know what a domain name is. The Domain Name System stores an A or AAAA record, which is a mapping between a domain name and an IP address. That is, when talking about a website, the domain name usually means its address (for example, “www.site”).
Hostname is a symbolic name assigned to a device connected to a network that can be used to provide access to that device. What name should I write as the hostname? Everything is confusing here; in the documentation for different Linux distributions you can see opposite recommendations. But most discussion participants agree that it is better to specify a short name as the hostname, and in the /etc/hosts file you should write domain names first, and then the host name.
It is also worth mentioning the term Fully Qualified Domain Name(from English "fully qualified domain name"). FQDN is obtained by appending the name of the parent domain to the host name. For example, there is a server running Apache, it is assigned the host name websrv1. And there is a server with databases, it is assigned the name dbsrv. And let their parent domain be example.org. Then the fully qualified domain names would be websrv1.example.org and dbsrv.example.org.
So what is the difference between a domain name and a hostname? I don't have a clear answer, but it can be said that the hostname can depend on the domain name. I guess we can say that the FQDN should be equal to the domain name. That is, if you have a website www.example.org, then the server hostname can be equal to www. And this rule should also work in the opposite direction. That is, if you use websrv1 rather than www as the host name, then you should add the corresponding entry to the DNS. In this case, the DNS server may not distribute this record outside its subnet; it may be a DNS server for internal needs.

Setting up a hostname in Ubuntu

There is a special hostname command in Linux; if you call it without arguments, it will display the current hostname.
To change the hostname, pass the new name as an argument:
  1. hostname web-srv-1
The new hostname will be active immediately after execution, but after a reboot the name from the /etc/hostname file will be restored. Therefore, you also need to change the hostname file. In other articles they write that you need to restart the hostname service, but in my Ubuntu 14.04 there is no such service. So I'll just reboot the operating system. By the way, Ubuntu has a special utility that changes both the current hostname value and the /etc/hostname file. It's called hostnamectl. If you call it without arguments, then in addition to the host name, it will also show the Ubuntu version, kernel version, architecture and computer type. And to set a domain name, you need to run the command:
  1. hostnamectl set-hostname web-srv-1
After this, you need to make changes to the /etc/hosts file. The IP address 127.0.1.1 must match the new hostname.
  1. 127.0.1.1 web-srv-1
And to complete the setup, you need to restart the network or restart the operating system.

Automated Hostname Configuration with Fabric

If you don't know what Fabric is, here's the documentation. I’ll just give the code of the function with which I configure the host name.
  1. def conf_hostname(hostname, domain = None):
  2. fqdn = hostname if domain is None else hostname + "." +domain
  3. sudo ("hostname %s " % hostname)
  4. sudo ("echo " %s " > /etc/hostname" % hostname )
  5. fabfiles. sed("/etc/hosts" , "^(127\.0\.1\.1\s+)[-a-z0-9]+", "\\ 1 %s %s " % (fqdn, hostname), use_sudo = True)
  6. sudo("reboot")
  7. time. sleep(20)
| |

Many hosting providers provide access to a control panel. It has a DNS settings section. This is where DNS records are created.

Requirements

To configure the hostname you need to have:

  • Virtual server.
  • Domain name.

1: Collecting information using WHOIS

First you need to redirect your domain name server to your provider's name servers. You can do this through the domain registrar's website. If you don't remember where you registered your name, you can find it using WHOIS. This is a protocol that displays site identification information (IP address and registration information).

To do this, enter into the command line:

whois example.com

WHOIS will display all the data associated with the site, including your domain registrar.

2: Changing the domain server

Log into your domain registrar's control panel and look for the Domain Name Server type field.

Point it to the provider's name servers and fill in the three Domain Name Server fields. After that, save your changes and exit.

To verify that new nameservers have been registered, use WHOIS again; As a result, you will see updated information:

Domain Name: EXAMPLE.COM
Registrar: ENOM, INC.
Whois Server: whois.enom.com
Referral URL: http://www.enom.com
Name Server: NS1.HOSTING-PROVIDER.COM
Name Server: NS2.HOSTING-PROVIDER.COM
Name Server: NS3.HOSTING-PROVIDER.COM
Status: ok

Although the nameservers are visible in the WHOIS output, it may take an hour or two for the changes to be reflected on the site.

3: Domain Setup

Now open your hosting provider's control panel.

Look for a section like “Add Domain” and enter your domain.

Note: The domain must not contain the www prefix.

You will be taken to a page where you can enter all your site details. To create a new hostname, you only need to fill out the A record. If you are using an IPv6 address, you must enter it in the AAAA record.

Record A

Enter the IP address of the server that will use this domain and hostname. The hostname comes before the domain:

test.example.com

Create a new record A, where HOSTNAME is test. In the WILL DIRECT TO field, enter the IP address of the server you want to associate this name with.

HOSTNAME: test
WILL DIRECT TO: 1111.1.111.1
TTL (SECONDS): 3600

You can also connect an IP address to a domain name without a prefix:

http://example.com

To do this, create a new hostname with the "@" symbol in the HOSTNAME field. The entry should look like this:

HOSTNAME: @
WILL DIRECT TO: 1111.1.111.1
TTL (SECONDS): 3600

Click the Create Entry button.

AAAA record

Enter the IPv6 address of the server that will use this domain and hostname. The hostname comes before the domain. You can also connect your IP to a base domain name without a prefix. To do this, create a new hostname with the @ symbol in the HOSTNAME field. The entry should look like this:

HOSTNAME: @
WILL DIRECT TO: 1111:1DB1::d:1111
TTL (SECONDS): 3600

Click the Create Entry button.

CNAME record

The CNAME record acts as an alias to the A record, directing the subdomain to the A record. If the A record's IP address changes, the CNAME will follow the new address. To add the www prefix to a URL, select the CNAME record and fill out two fields.

The screen should look like this:

HOSTNAME: www
IS AN ALIAS OF: @
TTL (SECONDS): 43200

You can also create a template CNAME record that will direct all subdomains to a specified A record (for example, if a visitor accidentally types wwww instead of www). To do this, put an asterisk in the HOSTNAME field.

HOSTNAME: *
IS AN ALIAS OF: @
TTL (SECONDS): 43200

If you need to configure a mail server, use MX records.

Posts by MX

The MX record consists of the following fields:

  • HOSTNAME (defines which hosts the entry should apply to),
  • MAIL PROVIDERS MAIL SERVER (indicates the mail server),
  • PRIORITY (indicates the order of connection to mail servers).

In most cases, the HOSTNAME field contains the @ symbol to apply the entry to the underlying domain. These entries always end with a period character. A standard MX record looks like this:

mail1.example.com

3: Final steps

After filling out all the required fields, it will take some time to update the information. After this, the name server information will be automatically filled in. The new settings will be supported in a few hours.

After some time, you will be able to confirm that the new hostname has been registered. To do this, use the command.

Hostname is a symbolic name assigned to a network device that can be used to access that device in a variety of ways. Typically, a hostname is a domain name, which is much easier for a person to read, remember and pronounce than the same numeric IP address, which also identifies a network device.

While an IP address is required to identify a device, assigning a hostname is optional.

There are several ways to assign a hostname to a device. Fully Qualified Domain Names (FQDNs) are managed and assigned to Internet hosts through the global Domain Name System (DNS) and are unique throughout the Internet. However, this can be done in the same way on local networks by local DNS servers, or the hostname can be determined using the "hosts" file on a separate computer.

In any case, devices on a network can technically only connect using an IP address. This means that in order to establish a connection with a device by its name, it is necessary to be able to resolve this name to an IP address. In addition, more than one domain name can be assigned to the same IP (one IP - several domain names). In this regard, resolving a domain name to IP is quite simple and the minimum necessary operation to implement a network connection. On the other hand, resolving an IP address to the list of domain names assigned to it is generally neither trivial nor necessary. Therefore, this is not technically implemented in Internet protocols. With one exception, the DNS system has a special PTR (pointer) record. It is this entry that allows the IP address to be resolved back to a hostname, and it is this name that is meant by a "hostname", and is what distinguishes it from the more general "domain name".