Hot answers tagged

168

The functionality you are looking for is implemented in glibc. You can define a custom hosts file by setting the HOSTALIASES environment variable. The names in this file will be picked up by gethostbyname (see documentation). Example (tested on Ubuntu 13.10): $ echo 'g www.google.com' >> ~/.hosts $ export HOSTALIASES=~/.hosts $ wget g -O /dev/null ...


58

Problematical effects include slow hostname resolution (unless the OS somehow converts the linear list into a faster-to-search structure?) and the potential for surprising interaction with shell tab completion well before any meaningful file size is reached. For example! If one places 500,000 host entries in /etc/hosts # perl -E 'for (1..500000) { say "127....


57

The file /etc/hosts started in the old days of DARPA as the resolution file for all the hosts connected to the internet (before DNS existed). It has the maximum priority, meaning this file is preferred ahead of any other name system.1 However, as a single file, it doesn't scale well: the size of the file becomes too big very soon. That is why the DNS system ...


34

I found this thread that discusses doing something along these lines. The thread is pretty adamant about not having multiple lines line the /etc/hosts file. excerpt - Re: /etc/hosts: Two lines with the same IP address? No, it will not. The resolvers stop at the first resolution. Having something like: 127.0.0.1 localhost.localdomain localhost ...


31

Private mountspaces created with the unshare command can be used to provide a private /etc/hosts file to a shell process and any subsequent child processes started from that shell. # Start by creating your custom /etc/hosts file [user] cd ~ [user] cat >my_hosts <<EOF 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 127....


29

The magic is opening the /etc/hosts file and reading it: strace -e trace=file wget -O /dev/null http://www.google.com http://www.facebook.com http://unix.stackexchange.com 2>&1 | grep hosts open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 4 open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 5 open("/etc/hosts", O_RDONLY|O_CLOEXEC) = 4 The getaddrinfo(3) function, ...


25

This is dictated by the NSS (Name Service Switch) configuration i.e. /etc/nsswitch.conf file's hosts directive. For example, on my system: hosts: files mdns4_minimal [NOTFOUND=return] dns Here, files refers to the /etc/hosts file, and dns refers to the DNS system. And as you can imagine whichever comes first wins. Also, see man 5 nsswitch.conf to get ...


21

I don't think it has a size limit in terms of number of lines. Before DNS (which was brought into use in 1985), it was this file that served as the only means for doing host name lookups, so I presume that this means that the file should be able to have thousands or at least hundreds of entries to be able to support the most well-connected pre-1985 Internet ...


21

Modify the nixos config with: networking.extraHosts = '' 127.0.0.2 other-localhost 10.0.0.1 server '';


20

I tried this out and it seems to work as expected: echo "1.2.3.4 facebook.com" >> /etc/hosts Then I ran: $ getent ahosts facebook.com 1.2.3.4 STREAM facebook.com 1.2.3.4 DGRAM 1.2.3.4 RA


19

As written in the manual page, the /etc/networks file is to describe symbolic names for networks. With network, it is meant the network address with tailing .0 at the end. Only simple Class A, B or C networks are supported. In your example the google-dns entry is wrong. It's not a A,B or C network. It's an ip-address-hostname-relationship therefore it ...


17

The main difference between the two is that: You will lose your Public IP when you Stop and Start the instance, while the EIP remains linked to the instance even after the Stop/Start operation (or until you don't explicitly detach it from the instance) Concerning the costs, there's no recurring fee you will pay for the EIP usage, while you keep it ...


16

Rather then make this block using /etc/hosts I'd suggest using a browser addon/plugin such as this one named: BlockSite for Firefox or StayFocusd for Chrome. BlockSite    StayFocusd                      But I want to really use /etc/hosts file ...


16

HOSTALIASES feature is provided by the resolver funtion gethostbyname() in glibc. In this function an alias look up result is passed as-is to subsequent libnss module calls specified by hosts: in /etc/nsswitch.conf, therefore if there's no module which can handle it, gethostbyname() will end up with failure. Note that in most programs numerical address ...


15

You should never parse /etc/passwd directly. You might be on a system with remote users, in which case they won't be in /etc/passwd. The /etc/passwd file might be somewhere else. Etc. If you need direct access to the user database, use getent. $ getent passwd phemmer phemmer:*:1000:4:phemmer:/home/phemmer:/bin/zsh $ getent passwd phemmer | awk -F: '{ ...


15

/etc/hostname contains name of the machine, as known to applications that run locally. /etc/hosts and DNS associate names with IP addresses. myname may be mapped to whichever IP address the machine can access itself, but mapping it to 127.0.0.1 is unæsthetic. Not /etc/hosts, but /bin/hostname serves another function with -f. Because /etc/hosts can override ...


14

According to RFC 4291, Section 2.7: Multicast Addresses, the first 8 bits (FF in FF0X) at the beginning of identifies the address as a muticast address. The flags, the next 4 bits, (the first 0 in FF0X) in the question indicate: ... a permanently-assigned ("well-known") multicast address, assigned by the Internet Assigned Numbers Authority (IANA)....


14

No. The hosts file doesn't affect any routing. It only affects name lookups. Since 216.58.212.68 is an IP address, the system won't look it up in the hosts table. Read here for more info on the hosts file: http://manpages.ubuntu.com/manpages/trusty/man5/hosts.5.html If you want to block connections to an IP address from your system, there are a couple of ...


13

How can I determine the size limit of /etc/hosts? It is a regular file, so the limit would correspond to the underlying filesystem's limits (which would itself be capped by the number of disks behind it), minus the space used by any other files in the same (probably root (/)) filesystem: ext2/3: 2 TiB ext4: 16 TiB (with the default 4KiB block size) xfs: ...


11

/etc/hostname and /etc/hosts are simple, but also not so simple... How to quickly setup Debian /etc/hostname and /etc/hosts 1) Edit /etc/hostname. There are 2 valid ways to set this up. Either make it: machine, i.e. a simple, one word name for this processor. For example, 'joe' might be your self-chosen machine name. Note this will require an alias ...


11

Since @William still got it wrong (!) here we go: The format of lines in /etc/hosts is address first and name(s) second 0.0.0.0 node1 0.0.0.0 node2 192.168.1.1 myroutermaybe 8.8.8.8 googledns # in case DNS doesn't work for DNS??? 127.0.0.1 localhost or where several names map to the same address 0.0.0.0 node1 node2 node3 stitch626 ADDED, thanks to ...


11

The file is called /etc/hosts.deny, not host.deny Not all services use tcp-wrappers. sshd, for example, doesn't by default. Neither does apache. You can use iptables to block all packets from 117.25.128/24, e.g.: iptables -I INPUT -s 117.25.128.0/24 -j DROP Even better, you can use fail2ban to monitor a log file (such as apache's access.log and/or error....


11

Size limits only apply when allocating static buffers. gethostbyname(3), which parses the entries in /etc/hosts, does not allocate static buffers - and never has. The original 1983 release of BSD 4.3 algorithm shows an open-file, while parse line, close-file pattern: sethostent(0); while (p = gethostent()) { if (strcmp(p->h_name, name) == 0) ...


9

That's because the /etc/hosts is simply a file on your Debian server that it utilizes for its own name resolution. It doesn't use the file to provide any DNS services. Since you don't want to set up BIND can I recommend that you look at dnsmasq instead? It's lightweight and can act as a DNS and DHCP server, simply by making use of your hosts file.


9

To answer just your last question: /etc/hosts doesn't apply again immediately because firefox is caching the last hostname it got for google.com; if you want it to always fetch it again, you'll have to set network.dnsCacheExpiration to 0 in about:config. More info (though a bit outdated) here. Sorry if this is offtopic. As a sidenote, many programs don't ...


9

Name resolution, amongst other things, is managed by /etc/nsswitch.conf. Here is an excerpt: passwd: files sss shadow: files sss group: files sss hosts: files dns myhostname (...) Note the hosts line. It says: "When resolving a hostname, first read /etc/hosts file to lookup the hostname, if not found then run a DNS query, if not found ...


8

I faced the same need, so I tried libnss-userhosts, but it fails at multithreaded applications. Therefore I have written libnss-homehosts. It's very new and tested only by me. You may give a chance for it! It supports some options in /etc/host.conf, multiple alias names, and reverse resolving (address to name).


8

The host command doesn't check the hosts file. From the manpage: host is a simple utility for performing DNS lookups. If you want to test lookups while respecting the hosts file, then use ping or getent. $ tail -1 /etc/hosts 127.0.0.1 google.com $ ping -c1 google.com | head -1 PING google.com (127.0.0.1) 56(84) bytes of data. $ getent ahosts google.com ...


8

This fully valid and correctly. Also you can write names in one string with space delimiter: 127.0.1.1 debian mini mini.local The both variants is fully valid.


8

The hosts file format consists of lines, each of which contains the IP address, followed by whitespace, then one or more hostnames, also separated by whitespace. Nothing changes when the address is an IPv6 address instead of a legacy IPv4 address. The format is the same. For example: 2001:db8:1::1 example.com www.example.com 2001:db8:2::1 mail.example.com ...


Only top voted, non community-wiki answers of a minimum length are eligible