# Aliases in /etc/hosts



## Deleted member 9563 (Oct 29, 2010)

I have a hosts file with a long list of aliases that I use on my local network. It works in Linux so I thought I could do the same in FreeBSD, and according to the way I read the manual it should be so. Apparently I am wrong, because I cannot ping any alias. The networking is functional otherwise. Here is the top of my hosts file for GIN:


```
::1                          localhost localhost.lan.cgs
127.0.0.1               localhost localhost.lan.cgs
192.168.1.119           GIN.lan.cgs GIN
192.168.1.119           GIN.lan.cgs.
192.168.1.102           CDX  CDX
192.168.1.103           TOP  TOP
192.168.1.122           CDY  CDY
```

I suspected it had something to do with FreeBSD's craving for having a FQDN but filling in the bogus "lan.cgs" does not help either.


----------



## Savagedlight (Oct 29, 2010)

What are the permissions of the file?
You'd want it to be owned by root:wheel, and have permissions 644 (ie wr--r--r-)


----------



## Deleted member 9563 (Oct 29, 2010)

```
-rw-r--r--  1 root  wheel  213 Oct 29 00:45 hosts
```
I thought of that, but if it was wrong I don't suppose that networking would work properly.


----------



## SirDice (Oct 29, 2010)

Put the entries with the same IP address on the same line.


----------



## wblock@ (Oct 29, 2010)

OJ said:
			
		

> ```
> 192.168.1.119           GIN.lan.cgs GIN
> 192.168.1.119           GIN.lan.cgs.
> ```



Those should all be on the same line, as noted by SirDice.



> ```
> 192.168.1.102           CDX  CDX
> 192.168.1.103           TOP  TOP
> 192.168.1.122           CDY  CDY
> ```



What are those doubled names supposed to do?


----------



## Deleted member 9563 (Oct 29, 2010)

wblock said:
			
		

> Those should all be on the same line, as noted by SirDice.



OK, but that is the way the OS installer put it. I'll put them on the same line.



> What are those doubled names supposed to do?



I simply did it the way it works in Linux, which seemed sensible because the FreeBSD handbook says:



> [Internet address] [official hostname] [alias1] [alias2] ...


So I have then: the address, the hostname, and the alias. In my case because I use simple three letter host names, it is convenient to use the same as alias.

What _should_ I do?

Here is my new test for /etc/hosts and I also added a "real world" address to see what that would do. However I can still not ping anything in the list.


```
::1                     localhost localhost.lan.cgs
127.0.0.1               localhost localhost.lan.cgs
192.168.1.119           GIN.lan.cgs GIN

 192.168.1.102 CDX
 192.168.1.103 TOP
 192.168.1.122 CDY
 69.90.46.148  holyroodgen.net hg
```


----------



## wblock@ (Oct 30, 2010)

OJ said:
			
		

> I simply did it the way it works in Linux, which seemed sensible because the FreeBSD handbook says:
> 
> 
> > [Internet address] [official hostname] [alias1] [alias2] ...
> ...



Yes, but the alias is a different name for the same host.  For example:

```
192.168.1.1    superman clarkkent reporternerd
```



> What _should_ I do?
> 
> Here is my new test for /etc/hosts and I also added a "real world" address to see what that would do. However I can still not ping anything in the list.



ping can be broken in many ways.  /etc/hosts only affects DNS.  Does ping resolve the hostname in the first line?

```
% ping freebsd.org
PING freebsd.org (69.147.83.40): 56 data bytes
```

Then your hosts settings are working and the ping problem is something else.  Often that's a firewall.


----------



## Deleted member 9563 (Oct 30, 2010)

wblock said:
			
		

> Yes, but the alias is a different name for the same host.  For example:
> 
> ```
> 192.168.1.1    superman clarkkent reporternerd
> ```



OK, so I guess all that is needed to resolve a name using /etc/hosts is to have a number and a name.



> ping can be broken in many ways.  /etc/hosts only affects DNS.  Does ping resolve the hostname in the first line?
> 
> ```
> % ping freebsd.org
> ...



Looking at my testing /etc/hosts file, I can ping holyroodgen.net, but I can't ping "hg". I can ping any name like google.com, and also any number. I just can't ping any of my (intended) aliases.

If then my hosts settings are correct how would I go about diagnosing the problem? These machines are all inside the same router/firewall, although on different switches. The aliases I use work just fine using the same format for /etc/hosts file when deployed on several Linux computers. It is just the two FreeBSD systems which are not working. Is there a firewall in FreeBSD which I should be looking at?


----------



## wblock@ (Oct 30, 2010)

Please capture and show your output from the ping command that doesn't work.


----------



## Deleted member 9563 (Oct 30, 2010)

Here's what happens:


```
[ole@GIN]/home/ole$ ping CDX
ping: cannot resolve CDX: Unknown host
[ole@GIN]/home/ole$ ping hg
ping: cannot resolve hg: Unknown host
```

Edit: Oops, I forgot that CDX just got replaced. Anyway, the others give the same output. Just to show that there is indeed a connection; using numbers I get this:

```
[ole@GIN]/home/ole$ ping 192.168.1.122
PING 192.168.1.122 (192.168.1.122): 56 data bytes
64 bytes from 192.168.1.122: icmp_seq=0 ttl=64 time=0.777 ms

[ole@GIN]/home/ole$ ping 69.90.46.148
PING 69.90.46.148 (69.90.46.148): 56 data bytes
64 bytes from 69.90.46.148: icmp_seq=0 ttl=50 time=34.109 ms
```


----------



## wblock@ (Oct 30, 2010)

Rmove the leading spaces in the last four lines of your example, leaving the IP address at the start of the line.


----------



## Deleted member 9563 (Oct 30, 2010)

Here is what /etc/hosts looks like now:


```
::1                     localhost localhost.lan.cgs
127.0.0.1               localhost localhost.lan.cgs
192.168.1.119           GIN.lan.cgs GIN

192.168.1.101 SCO
192.168.1.103 TOP
192.168.1.122 CDY
69.90.46.148  holyroodgen.net hg
```

*It works!!!* I'm surprised the original one didn't work, but now that I have something that works, I can make adjustments from there. Alls good now and I can paste in the rest of the list. Thanks *wblock*!


----------

