Hosts file in ubuntu. How to edit the hosts file in Ubuntu

Good afternoon, dear readers. I am publishing the second part. In the current part the main emphasis is on network implementation in Linux(how to set up a network in Linux, how to diagnose a network in Linux and maintain the network subsystem in Linux).

Configuring TCP/IP in Linux to work on an Ethernet network

To work with TCP/IP network protocols in Linux, it is enough to have only loopback interface, but if it is necessary to connect hosts with each other, naturally, it is necessary to have a network interface, data transmission channels (for example, twisted pair), possibly some kind of network equipment. Also, it is necessary to have installed ones (, etc.), usually supplied to. It is also necessary to have a network (for example /etc/hosts) and network support.

Network settings

Let's start understanding Linux network mechanisms with manual network configuration, that is, with the case when IP address network interface static. So, when setting up a network, you need to consider and configure the following parameters:

IP address- as already mentioned in the first part of the article - this is the unique address of the machine, in the format of four decimal numbers separated by dots. Usually, when working in local network, selected from private ranges, for example: 192.168.0.1

Subnet mask- also, 4 decimal numbers that determine which part of the address relates to the network/subnet address, and which part to the host address. A subnet mask is a number that is added (in binary form) with an IP address to determine which subnet the address belongs to. For example, the address 192.168.0.2 with a mask of 255.255.255.0 belongs to the subnet 192.168.0.

Subnet address- determined by the subnet mask. However, there are no subnets for loopback interfaces.

Broadcast address- the address used to send broadcast packets that will be received by all hosts on the subnet. Typically, it is equal to the subnet address with a host value of 255, that is, for the subnet 192.168.0 the broadcast will be 192.168.0.255, similarly, for the subnet 192.168 the broadcast will be 192.168.255.255. There is no broadcast address for loopback interfaces.

Gateway IP address- this is the address of the machine that is the default gateway for communication with the outside world. There can be several gateways if the computer is connected to several networks at the same time. The gateway address is not used on isolated networks (not connected to the WAN) because these networks have nowhere to send packets outside the network, the same applies to loopback interfaces.

Name server IP address (DNS server)- address of the server that converts host names into IP addresses. Usually provided by the provider.

Network settings files in Linux (configuration files)

To understand how the network works in Linux, I would definitely recommend reading the article "". In general, all Linux work is based on , which is born when the OS boots and produces its descendants, which in turn do all the necessary work, be it launching bash or a daemon. Yes, and the entire Linux boot is based on, which spells out the entire sequence of launching small utilities with various parameters that are sequentially started/stopped when the system starts/stops. The Linux network subsystem starts in the same way.

Each Linux distribution has a slightly different network initialization mechanism, but I think the general picture will be clear after reading. If you look at the start scripts of the network subsystem of any Linux distribution, how to configure the network configuration using configuration files will become more or less clear, for example, in Debian (let’s take this distribution as a basis), a script is responsible for initializing the network /etc/init.d/networking, having looked at which:

Net-server:~#cat /etc/init.d/networking #!/bin/sh -e ### BEGIN INIT INFO # Provides: networking # Required-Start: mountkernfs $local_fs # Required-Stop: $local_fs # Should -Start: ifupdown # Should-Stop: ifupdown # Default-Start: S # Default-Stop: 0 6 # Short-Description: Raise network interfaces. ### END INIT INFO PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin" [ -x /sbin/ifup ] || exit 0 . /lib/lsb/init-functions process_options() ( [ -e /etc/network/options ] || return 0 log_warning_msg "/etc/network/options still exists and it will be IGNORED! Read README.Debian of netbase." ) check_network_file_systems() ( [ -e /proc/mounts ] || return 0 if [ -e /etc/iscsi/iscsi.initramfs ]; then log_warning_msg "not deconfiguring network interfaces: iSCSI root is mounted." exit 0 fi exec 9<&0 < /proc/mounts while read DEV MTPT FSTYPE REST; do case $DEV in /dev/nbd*|/dev/nd*|/dev/etherd/e*) log_warning_msg "not deconfiguring network interfaces: network devices still mounted." exit 0 ;; esac case $FSTYPE in nfs|nfs4|smbfs|ncp|ncpfs|cifs|coda|ocfs2|gfs|pvfs|pvfs2|fuse.httpfs|fuse.curlftpfs) log_warning_msg "not deconfiguring network interfaces: network file systems still mounted." exit 0 ;; esac done exec 0<&9 9<&- } check_network_swap() { [ -e /proc/swaps ] || return 0 exec 9<&0 < /proc/swaps while read DEV MTPT FSTYPE REST; do case $DEV in /dev/nbd*|/dev/nd*|/dev/etherd/e*) log_warning_msg "not deconfiguring network interfaces: network swap still mounted." exit 0 ;; esac done exec 0<&9 9<&- } case "$1" in start) process_options log_action_begin_msg "Configuring network interfaces" if ifup -a; then log_action_end_msg $? else log_action_end_msg $? fi ;; stop) check_network_file_systems check_network_swap log_action_begin_msg "Deconfiguring network interfaces" if ifdown -a --exclude=lo; then log_action_end_msg $? else log_action_end_msg $? fi ;; force-reload|restart) process_options log_warning_msg "Running $0 $1 is deprecated because it may not enable again some interfaces" log_action_begin_msg "Reconfiguring network interfaces" ifdown -a --exclude=lo || true if ifup -a --exclude=lo; then log_action_end_msg $? else log_action_end_msg $? fi ;; *) echo "Usage: /etc/init.d/networking {start|stop}" exit 1 ;; esac exit 0

You can find several functions that check for the presence of mounted network file systems ( check_network_file_systems(), check_network_swap()), as well as checking the existence of some still unclear config /etc/network/options ( function process_options()), and at the very bottom, the design case "$1" in and in accordance with the entered parameter (start/stop/force-reload|restart or any other) performs certain actions. Of these same " certain actions", using the start argument as an example, you can see that the function is launched first process_options, then the phrase is sent to the log Configuring network interfaces, and the command is run ifup -a. If you look at man ifup , you can see that this command reads the config from a file /etc/network/interfaces and according to the key -a launches all interfaces that have the parameter auto.

The ifup and ifdown commands may be used to configure (or, respectively, deconfigure) network interfaces based on interface definitions in the file /etc/network/interfaces.

-a, --all
If given to ifup, affect all interfaces marked auto. Interfaces are brought up in the order in which they are defined in /etc/network/interfaces. If given to ifdown, affect all defined interfaces. Interfaces are brought down in the order in which they are currently listed in the state file. Only interfaces defined in /etc/network/interfaces will be brought down.

ip-server:~# cat /etc/network/interfaces # This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface allow-hotplug eth0 iface eth0 inet dhcp allow-hotplug eth2 iface eth2 inet static address 192.168.1.1 netmask 255.255.255.0 gateway 192.168.1.254 broadcast 192.168.1.255

In this config the lines allow-hotplug And auto- these are synonyms and interfaces will be raised on command ifup -a. This, in fact, is the entire chain of operation of the network subsystem. Similarly, in other distributions: in RedHat and SUSE the network is launched by a script /etc/init.d/network. Having examined it, you can similarly find where the network configuration lies.

/etc/hosts

This file stores a list IP addresses And hostnames corresponding to them (addresses).The file format is no different from the master file:

Ip-server:~# cat /etc/hosts # ip host.in.domain host 127.0.0.1 localhost 127.0.1.1 ip-server.domain.local ip-server 192.168.1.1 ip-server.domain.local ip-server

Historically, this file was used instead of the DNS service. Currently, the file can also be used instead of the DNS service, but only on the condition that the number of machines in your network is measured in units, and not in tens or hundreds, because in this case, you will have to monitor the correctness of this file on each machine.

/etc/hostname

This file contains NetBIOS hostname:

Ip-server:~# cat /etc/hostname ip-server

This file stores the names and addresses of local and other networks. Example:

Ip-server:~# cat /etc/networks default 0.0.0.0 loopback 127.0.0.0 link-local 169.254.0.0 home-network 192.168.1.0

When using this file, networks can be managed by name. For example, do not add a route route add 192.168.1.12 , A route add.

/etc/nsswitch.conf

File defines hostname lookup order/network, the following lines are responsible for this setting:

For hosts: hosts: files dns For networks: networks: files

Parameter files specifies to use the specified files (/etc/hosts And /etc/networks respectively), parameter dns specifies to use the service dns.

/etc/host.conf

The file specifies name resolution parameters for the resolver

Ip-server:~# cat /etc/host.conf multi on

This file tells the resolv library to return all valid host addresses that appear in the /etc/hosts file, and not just the first one.

/etc/resolv.conf

This file defines the parameters of the mechanism for converting network names to IP addresses. In simple terms, defines DNS settings. Example:

Ip-server:~# cat /etc/resolv.conf nameserver 10.0.0.4 nameserver 10.0.0.1 search domain.local

First 2 lines indicate DNS servers. The third line specifies the search domains. If, when resolving a name, the name is not an FQDN name, then this domain will be substituted as an “end”. For example, when executing the ping host command, the pinged address is converted to host.domain.local. The remaining parameters can be read in man resolv.conf. Very often, Linux uses dynamic generation of this file, using the so-called. programs /sbin/resolvconf. This program is an intermediary between services that dynamically provide name servers (for example DHCP client) and services that use name server data. To use a dynamically generated file /etc/resolv.conf, you need to make this file a symbolic link to /etc/resolvconf/run/resolv.conf. In some distributions the path may be different; this will definitely be written in man resolveconf.

Network configuration

After reviewing the main configuration files, you can look at . The command has already been mentioned above ifup, ifdown, but these tools are not entirely universal; for example, RH distributions do not have these commands by default. In addition, new distributions have introduced a new high-level network management tool - which belongs to the iproute package. I will dedicate it to him (the iproute package). And in the current post I will not consider it. The commands described below belong to .

So, to be sure that the command works on any Linux distribution, you need to use two main old commands. This , and arp. The first team (responsible for setting up network interfaces(ip, mask, gateway), second () - routing setup, third (arp) - arp table management. I would like to note that executing these commands without disabling the standard SystemV startup script of the network subsystem will make changes only until the first reboot/restart of the network service, because if you think about it, you can understand that the script /etc/init.d/networking the next time it starts, it will re-read the above configs and apply the old settings. Accordingly, the way out for permanently setting the settings is either to enter the ifconfig command with the appropriate parameters in , or to manually correct the corresponding configs of the network interfaces.

Also, if the command is executed ifconfig with missing parameters(for example, only an IP address), then the rest are added automatically (for example, a broadcast address is added by default with a host address ending in 255 and the default subnet mask is 255.255.255.0).

Routing for existing interfaces in modern kernels it is always raised automatically by the kernel. Or rather, direct routes to the network according to the IP settings and the subnet into which the raised interface looks are formed automatically, by the kernel. The gateway field for such entries indicates the address of the output interface or *. In older versions of the kernel (I can’t tell you the kernel number from which routes began to rise automatically), it was necessary to add the route manually.

If there is a need to organize your routes, then you need to use . With this command you can add and remove routes, but again, this will only help until you restart /etc/init.d/networking (or another script responsible for the network in your distribution). In order for routes to be added automatically, you must, in the same way as with the ifconfig command, add commands for adding routes to rc.local, or manually correct the corresponding network interface configs (for example, in Deb - /etc/network/options).

By what rules routes to networks are formed, I'm in

Linux network diagnostics

There are a large number of network diagnostic tools in Linux, often very similar to utilities from Microsoft. I will look at 3 main network diagnostic utilities, without which it will be difficult to identify problems.

I think that this utility is familiar to almost everyone. This utility works by sending so-called ICMP packets to the remote server, which will be specified in the command parameters, the server returns the sent commands, and pingcounts the time required for the sent packet to reach the server and return. For example:

# ping ya.ru PING ya.ru (87.250.251.3) 56(84) bytes of data. 64 bytes from www.yandex.ru (87.250.251.3): icmp_seq=1 ttl=57 time=42.7 ms 64 bytes from www.yandex.ru (87.250.251.3): icmp_seq=2 ttl=57 time=43.2 ms 64 bytes from www.yandex.ru (87.250.251.3): icmp_seq=3 ttl=57 time=42.5 ms 64 bytes from www.yandex.ru (87.250.251.3): icmp_seq=4 ttl=57 time=42.5 ms 64 bytes from www .yandex.ru (87.250.251.3): icmp_seq=5 ttl=57 time=41.9 ms ^C --- ya.ru ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4012ms rtt min/ avg/max/mdev = 41.922/42.588/43.255/0.500 ms

As can be seen from the above example, ping gives us a bunch of useful information. First of all, we found out that we can establish a connection with the host ya.ru(sometimes they say that “the ya.ru host is available to us”). Secondly, we see that DNS is working correctly, because the “pinged” name was correctly converted to an IP address (PING ya.ru (87.250.251.3)). Further, in field icmp_seq= numbering of sent packets is indicated. Each sent packet is sequentially assigned a number, and if there are “dips” in this numbering, this will tell us that the connection with the “pinged” is unstable, and may also mean that the server to which the packets are sent is overloaded. By value time= we see, how long did the package travel to 87.250.251.3 and back. You can stop the ping utility by pressing Ctrl+C.

Also, ping utility It is interesting because it can allow you to see exactly where the problems occurred. Let's say ping utility displays a message network not reachable (network not available), or other similar message. This most likely indicates that your system is configured incorrectly. In this case, you can send packets to the provider’s IP address to understand where the problem occurs (between the local PC or “further”). If you are connected to the Internet through a router, you can send packets via its IP. Accordingly, if the problem appears already at this stage, this indicates incorrect configuration of the local system, or damage to the cable; if the router is recalled, but the provider’s server is not, then the problem is in the provider’s communication channel, etc. Finally, if the name to IP conversion fails, then you can check the IP connection; if the responses come correctly, then you can guess that the problem is in the DNS.

It should be noted that this utility is not always a reliable diagnostic tool. The remote server can block responses to ICMP requests.

traceroute

In simple terms, the command is called route tracing. As the name suggests, this utility will show which route the packets took to reach the host. traceroute utility somewhat similar to ping, but displays more interesting information. Example:

# traceroute ya.ru traceroute to ya.ru (213.180.204.3), 30 hops max, 60 byte packets 1 243-083-free.kubtelecom.ru (213.132.83.243) 6.408 ms 6.306 ms 6.193 ms 2 065-064-free .kubtelecom.ru (213.132.64.65) 2.761 ms 5.787 ms 5.777 ms 3 lgw.kubtelecom.ru (213.132.75.54) 5.713 ms 5.701 ms 5.636 ms 4 KubTelecom-lgw.Krasnodar.gldn.net (194.186.6.177) 81.430 ms 81.581 ms 81.687 ms 5 cat26.Moscow.gldn.net (194.186.10.118) 47.789 ms 47.888 ms 48.011 ms 6 213.33.201.230 (213.33.201.230) 43.322 ms 41.783 ms 4 1.106 ms 7 carmine-red-vlan602.yandex.net (87.250. 242.206) 41.199 ms 42.578 ms 42.610 ms 8 www.yandex.ru (213.180.204.3) 43.185 ms 42.126 ms 42.679 ms

As you can see, you can trace the route from the provider’s router 243-083-free.kubtelecom.ru (213.132.83.243) (South of Russia) to the end host at www.yandex.ru (213.180.204.3) in Moscow.

dig

This utility sends queries to DNS servers and returns information about the specified domain. Example:

# dig @ns.kuban.ru roboti.ru ;<<>> DiG 9.3.6-P1<<>> @ns.kuban.ru roboti.ru ; (1 server found) ;; global options: printcmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64412 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 0 ;; QUESTION SECTION: ;roboti.ru. IN A ;; ANSWER SECTION: roboti.ru. 448 IN A 72.52.4.90 ;; AUTHORITY SECTION: roboti.ru. 345448 IN NS ns1.sedoparking.com. roboti.ru. 345448 IN NS ns2.sedoparking.com. ;; Query time: 102 msec ;; SERVER: 62.183.1.244#53(62.183.1.244) ;; WHEN: Thu Feb 17 19:44:59 2011 ;; MSG SIZE rcvd: 94

dig command sent a request DNS server - ns.kuban.ru (@ns.kuban.ru- this parameter is not necessary to specify, in this case the source of information about DNS will be the server from your system settings) about the domain name roboti.ru. As a result, I received a response, in which we can see in the section ANSWER SECTION information about domain IP addresses, in the section AUTHORITY SECTION information about the so-called authoritative DNS servers. The third line from the bottom tells us which server provided the response.

Other diagnostic utilities

ping, dig and other diagnostic utilities with parameters can be found in the post.

Connecting a new network card

Connecting and launching a new network card comes down to a few steps:

1. Physical connection of the card

3. View the output of whether the system has detected a new network card:

Let's see the conclusion BEFORE connecting a new card:

Server:~# dmesg | grep eth [ 4.720550] e1000: eth0: e1000_probe: Intel(R) PRO/1000 Network Connection [ 5.130191] e1000: eth1: e1000_probe: Intel(R) PRO/1000 Network Connection [ 15.285527] e1000: eth2: e1000_watchdog: N IC Link is Up 1000 Mbps Full Duplex, Flow Control: RX [ 15.681056] e1000: eth0: e1000_watchdog: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX

The output shows that the system has 2 network cards eth1 and eth2. We connect the third one and look at the output:

Server:~# dmesg | grep eth [ 4.720513] e1000: eth0: e1000_probe: Intel(R) PRO/1000 Network Connection [ 5.132029] e1000: eth1: e1000_probe: Intel(R) PRO/1000 Network Connection [ 5.534684] e1000: eth2: e1000_probe: Intel( R ) PRO/1000 Network Connection [ 39.274875] udev: renamed network interface eth2 to eth3 [ 39.287661] udev: renamed network interface eth1_rename_ren to eth2 [ 45.670744] e1000: eth2: e1000_watchdog: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX [ 46.237232] e1000: eth0: e1000_watchdog: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX [ 96.977468] e1000: eth3: e1000_watchdog: NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX

IN dmesg we see that a new network has appeared - eth3, which is actually eth2, but renamed by the udev device manager to eth3, and eth2 is actually a renamed eth1 (we will talk about udev in a separate post). The appearance of our new network in dmesg tells us that the network card supported core and correct decided. All that's left is to set up the new interface in /etc/network/interfaces(Debian) because this map was not initialized by the startup script /etc/init.d/network. ifconfig sees this map:

Server:~# ifconfig eth3 eth3 Link encap:Ethernet HWaddr 08:00:27:5f:34:ad inet6 addr: fe80::a00:27ff:fe5f:34ad/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric: 1 RX packets:311847 errors:0 dropped:0 overruns:0 frame:0 TX packets:126 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:104670651 (99.8 MiB) TX bytes: 16184 (15.8 KiB)

but again - it does not configure. How to configure the network card was discussed above.

Summary

I think that's all for today. When I started writing this article, I thought that I would fit into one post, but it turned out to be huge. Therefore, it was decided to split the article into two. In total, I tried to present, not a step-by-step procedure for setting up a network, but to outline the principle and explain the understanding of how the network starts and works in Linux. I really hope that I succeeded. I will be glad to see your comments and additions. Over time, I will add to the article.

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:

I said that I would write about how to set up virtual hosts in Ubuntu and change the directory for hosting sites. 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, lasts 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.

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.

Working in other OSes, you may already know where the hosts file is located, but when you switch to Ubuntu Linux, you may be a little lost where this file is located and how to edit it.

What is a hosts file? If you are interested in where it is located in Ubuntu and how to edit it, read the material below.

HOSTS is a text file that contains a database of domain names and is used to translate them into host network addresses. The request to this file plays a very important role and has a very high priority before accessing DNS servers. Unlike DNS, which is updated from 30 minutes to 2-3 days, the contents of the file are controlled by the computer administrator and can be updated at any time if necessary.

Working in other operating systems, you may already know where the hosts file is located, but if you switch to any other OS, for example Ubuntu Linux, you will search for a long time without prompting to find out where this file is located and how to edit it.

  • C:\WINDOWS\system32\drivers\etc - on Windows
  • /etc/hosts - On Ubuntu

To open the hosts file for editing in Ubuntu, open a terminal (CTRL+ALT+T) and run the command:

Sudo gedit /etc/hosts

after which this file will open in a standard editor convenient for editing, the standard contents of the file are:

127.0.0.1 localhost 127.0.1.1 your-pc # 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

If you want to register the domain and IP you need, you can write immediately after the second line at the very beginning of the file, having written everything you need, click the save button in the Gedit editor and you’re done, no need to restart the computer.

File hosts has the following format:

(IP - IP address of the node) (Host name - the name of the node in the domain). (Domain - domain name) (Alias ​​- defines the additional name of the node)

You can get more detailed information on working with the hosts file by running the command in the terminal man hosts.

Example entry:

193.109.247.234 linuxsoid.com 88.212.202.38 li.ru

It’s that simple, make changes, save and you’re done. If you have any questions, ask in the comments to the material.

mob_info