How to change hosts file in ubuntu. Linux hosts file

How to change hosts file in ubuntu. Linux hosts file

03.08.2023

If you need to configure a network card, then select the IP address, fully qualified domain name (FQDN), as well as possible aliases that will be specified in the /etc/hosts file. The syntax is as follows:

IP_address myhost.example.org aliases

If you don't want your computer to be visible on the Internet (i.e., have a registered domain and a valid range of assigned IP addresses—most users don't), simply check that the IP address is within the range of private network IP addresses. Valid ranges:

Range of private network addresses Normal prefix 10.0.0.1 - 10.255.255.254 8 172.x.0.1 - 172.x.255.254 16 192.168.y.1 - 192.168.y.254 24

x can be any number in the range 16 - 31. y can be any number in the range 0 - 255.

A valid private IP address can be 192.168.1.1. A valid FQDN for this IP would be lfs.example.org.

Even if the network card is not used, the FQDN may still be required. It is required by some programs in order for them to work properly.

Create a /etc/hosts file like this:

Cat > /etc/hosts< "EOF" # Begin /etc/hosts (network card version) 127.0.0.1 localhost <192.168.1.1> # End /etc/hosts (network card version) EOF

Values<192.168.1.1>And should be changed to suit specific user or other requirements (if the IP address is assigned by the network/system administrator and the machine is connecting to an existing network). Optional alias names can be omitted.

If you do not need to configure the network card, create the /etc/hosts file as follows:

Cat > /etc/hosts< "EOF" # Begin /etc/hosts (no network card version) 127.0.0.1 localhost # End /etc/hosts (no network card version) EOF

Previous section:

The Domain Name System (DNS) is used to determine which IP belongs to a given domain on the Internet. When any program needs to access a site by its domain name, the operating system sends a request to the DNS server to find out which IP to forward packets to. But this doesn't always happen. For example, when we access the localhost domain, the request is always sent to our local computer.

The reason for this is the hosts file. If you've used Windows before, you've probably already heard about this file. There, it was most often used to quickly block access to a resource. But its application is much wider. In this article we will look at how to configure the hosts file in Linux, as well as what capabilities it provides us.

Before we move on to the hosts file itself, we need to understand how to look up an IP address for a domain name in Linux. I said that the operating system immediately sends a request to the DNS server, but this is not entirely true. There is a specific search order according to which it is performed. This order is set in the /etc/nsswitch.conf configuration file

cat /etc/nsswitch.conf

Here we are interested in the hosts line. It lists, in order of priority, the services that are used to find an IP address for a domain name. The files item means using the /etc/hosts file, and dns means the Internet domain name service. If files is located before hosts, this means that first the system will try to find the domain in /etc/hosts, and only then using DNS. By default, this is the case.

Setting up the hosts file in Linux

The file we need is located in the /etc/ directory. To open it, you can use any text editor, both on the command line and in the graphical interface, but you need to open it with superuser rights. For example, using vim:

sudo vi /etc/hosts

sudo gedit /etc/hosts

The file syntax is quite simple. It contains several lines with domain names and IP addresses that need to be used for them. Each of them looks like this:

ip_address domain alias

Usually the first line creates a rule to redirect all requests to the localhost domain to the local IP address - 127.0.0.1:

127.0.0.1 localhost

This file also contains redirections for your computer name and IPv6 addresses by default. You can create your own settings for any desired domain. To do this, add a line to the end of the file ..0.0.1:

127.0.0.1 site

Please note that only the domain is indicated here, without the protocol. There is no need to specify the http or https prefix, otherwise nothing will work. But for the www subdomain you need to create a separate entry or write it as an alias. For example:

127.0.0..site

Now, when requesting a domain, the site will open our local IP. To return access to the original resource, just remove the added line. But you can use not only a local address, but also any other one. This is very convenient if you have just registered a domain and the domain zone has not yet been updated, but you already want to work with a new site. Just add the details to /etc/hosts and work as usual.

conclusions

In this short article, we looked at how DNS is configured through the Linux hosts file. As you can see, with its help you can block access to unwanted resources, for example, which programs should not have access to, and also use it in your work as a webmaster.

about the author

Founder and site administrator, I am passionate about open source software and the Linux operating system. I currently use Ubuntu as my main OS. In addition to Linux, I am interested in everything related to information technology and modern science.

I want to tell you about a somewhat non-standard way with which you can open sites in a browser using short names. For example, instead of typing “yandex.ru” in your browser, you can simply type one letter “y”. The file /etc/hosts will help us with this.

File /etc/hosts

The /etc/hosts file specifies correspondence between the IP address and the host name (hostname, domain), as well as their aliases (also called synonyms, aliases). When you access an address on the network, for example, http://yandex.ru, to access the site, the system must first convert the site address into its corresponding IP address; for this, the system requests the IP address from the DNS server. If the /etc/hosts file contains a correspondence between yandex.ru and an IP address, then the system will use this exact IP address. And since we can also specify an alias in the /etc/hosts file, through which a specific IP address will be accessible, we can specify, for example, the alias “y” for yandex.ru. Accordingly, when you request “y” in your browser, the system will read the IP address in the /etc/hosts file and open the yandex.ru website. That is, /etc/hosts is a kind of local DNS server.

How to find out the IP address of a site

Now we need to determine the IP addresses of the sites that we want to add to /etc/hosts. It is worth noting that not all sites have a separate dedicated IP address and it is impossible to access them only by IP address. But almost all large sites and portals have their own dedicated IP address. An easy way to find out the IP address of a site is to use the ping command:

Ping site-address.ru

For example, let's do:

ping yandex.ru

The ping process will be displayed on the screen (to interrupt, press Ctrl+C). So, we will get the output of the ping command like this:

PING yandex.ru (87.250.250.11) 56(84) bytes of data. 64 bytes from yandex.ru (87.250.250.11): icmp_seq=1 ttl=57 time=3.57 ms ...

Here you can see that yandex.ru corresponds to the IP address 87.250.250.11. We do the same for other sites.

Editing the /etc/hosts file

You need to edit the /etc/hosts file carefully so as not to spoil anything, since this is a system file and it is important not to violate its syntax. Open the file for editing, to do this run on the command line:

Sudo gedit /etc/hosts

The contents of the file look something like this:

127.0.0.1 localhost::1 mycomp localhost6.localdomain6 localhost6 127.0.1.1 mycomp # The following lines are desirable for IPv6 capable hosts::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ...

The syntax for the /etc/hosts file is very simple:

IP address hostname [aliases, ...]

Let's add new lines to the file for different sites:

74.125.232.19 google.ru g 94.100.191.203 mail.ru m 194.186.36.211 rbc.ru r 87.250.250.11 yandex.ru y

Save and close the file. We have written correspondences between IP addresses, site names and their aliases. Now go to the browser and simply enter “g” in the address bar, the website google.ru should open. To open mail.ru, enter simply “m”, for rbc.ru - simply “r”, yandex.ru - “y”.

NAME
hosts - static IP address conversion table
car names

SYNTAX
/etc/hosts

DESCRIPTION
This page contains a description of the file format
/etc/hosts. This file contains text describing
correspondence between IP addresses and machine names (one address per
line). For each machine in one line there should be
the following information is present:

IP_address canonical_alias name

The fields of this record are separated from each other by spaces and/or
tabs. Text starting with the "#" character through to the end
line is considered a comment and is ignored. Machine names
can contain any printable character except the delimiter
margins, newline, or comment character.
Aliases represent modified, alternative,
shortened or generic forms of machine names (for example,
localhost). The format of the machine name table is described in RFC 952.

The Berkeley Internet Domain Name (BIND) server contains
Internet name server for UNIX machines. It replaces the file
/etc/hosts or machine search engine and frees the machine
from the need to completely fill out the file in detail
/etc/hosts.

Despite the fact that the functions of this table have been performed for a long time
DNS is still used for

Initial boot of the system.
Many systems have a small table of names
machines containing information about names and addresses
important machines on the local network. This is useful in that
in case DNS does not work, for example when
loading the system.

NIS Sites running NIS use a table of names
machines as a source of information for the database
NIS machine data. Although NIS may
work with DNS, many NIS sites use
table of machine names with rows for all machines
local network as a safety net.

Isolated nodes
Small sites not connected to the global network
use a machine name table instead of DNS. If
local information changes rarely, and the network does not
connected to the Internet, then DNS is hardly necessary.

EXAMPLE
127.0.0.1 localhost
192.168.1.10 foo.mydomain.org foo
192.168.1.13 bar.mydomain.org bar
216.234.231.5 master.debian.org master
205.230.163.103 www.opensource.org

HISTORICAL REFERENCE
Before the introduction of DNS, the machine name table was the only
a tool for converting machine names to addresses in
the growing Internet. In fact, this file
was created on the basis of the official vehicle database,
maintained in the Network Information Control Center
(NIC), and also based on local patches containing
unofficial aliases and/or information about unknown persons
cars. NIC no longer supports hosts.txt files,
however, at the time of writing this page (circa 2000)
there were old hosts.txt files on the WWW. They were
found; the dates of their placement are 92, 94 and 95.

I said that I would write about how to set up virtual hosts in Ubuntu and change the directory for hosting websites. Well, I’m writing.

There is a lot written on the Internet about virtual hosts and how to set them up. But in some places the information is outdated, in others it is only half working. As a result, the procedure, which takes at most five minutes, stretches out for several hours. I had that one. Therefore, in order not to waste a lot of time again the next time I need it, I am writing this article.

Setting up the root directory for sites

By default, Apache searches for Internet pages in /var/www/html. But this may be inconvenient for the user. Therefore, if desired, the root directory can be changed. For example, for my sites I will use the folder public_html in the home directory. Also, I'll immediately create a folder for the test site testsite.loc and directly the folder in which the site files will be located www. That is, you need to create the following directory structure: /public_html/testsite.loc/www/. You can do this through a file manager, or through a terminal:

Mkdir -p public_html/testsite.loc/www

Let's create a simple test page right away index.html

Setting up virtual hosts

Hello!!

And save it to the www folder.

Now let's edit the virtual host file 000-default.conf to tell Apache where we are now hosting sites:

Sudo gedit /etc/apache2/sites-available/000-default.conf

In the opened file, find the line that begins with DocumentRoot and change the path to a new one: /home/user/public_html/. Where user— your username.

Save the file and close the editor.

Let's make changes to the settings file apache2.conf:

Sudo gedit /etc/apache2/apache2.conf

Add the following block to the opened file:

Options Indexes FollowSymLinks AllowOverride None Require all granted

Again, don't forget to replace user to your username. Save the file.

Create a configuration file for the new host. To do this, copy the standard file 000-default.conf and rename it to testsite.conf:

Sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/testsite.conf

Now let's edit it:

Sudo gedit /etc/apache2/sites-available/testsite.conf

For convenience, you can remove all commented lines (that start with #)

Add “ServerName testsite.loc”

Add “ServerAlias ​​www.testsite.loc”

In DocumentRoot we specify the path to the directory with the site files.

Save the changes.

And turn on the site:

Sudo a2ensite testsite.conf

Restart Apache:

Service apache2 reload

Hosts file

To redirect browser requests to our server. Let's edit the hosts file:

Sudo gedit /etc/hosts

In the first line, separated by a space, next to localhost, add the domains of our site testsite.loc And www.testsite.loc

Save changes

We can check the result of our work. We type the address of our website in the browser - testsite.loc.

Folder permissions

Apache server runs as group and user www-data. In the home directory, the owner of the files is the current user. Therefore, to avoid problems, you need to change the rights on the files and folders inside public_html and on it itself. To do this, enter in the terminal.

© 2024 hecc.ru - Computer technology news