3

In /etc/hosts, I added the following line:

github.com git.hub

In chrome browser, when I enter the address git.hub it can't redirect to github.com, instead it does a search.

In Linux terminal (e.g bash), when I enter cmd ping git.hub, it says git.hub: Name or service not known.

In my /etc/nsswitch.conf, the hosts config is like this:

hosts:          files mdns4_minimal [NOTFOUND=return] dns myhostname

I suppose it means check the /etc/hosts first, then dns.

So, why git.hub can't become an alias for github.com after my configuration.

And, is there anyway to achieve that, without actually change the DNS record.

1

2 Answers 2

13

The /etc/hosts file does not alias a hostname to another hostname the way you're trying to do. It associates a hostname to an IP address, period. The IP address appears in the first (left-most) field of the line. The hostname appears in the second field of the line.

You can include a third field, fourth field, etc., on the line, each with an alternative hostname, but those names will be associated with the first field's IP address, not any of the hostnames in the other fields.

This can give you a short list of names that all reach the same destination, but the destination is an IP address. If the site you want to reach is like Github, its IP address will change from time to time. When it changes, all of the names on the line will fail.

I used to do this kind of aliasing in my /etc/hosts file, but it became too disruptive because sites on the Internet would change their IP address too often.

2
  • 8
    What's more, even if you could keep your /etc/hosts up to date (eg with a script that checked the DNS entries regularly), the alias likely still wouldn't work, since modern HTTP requires the client to send the hostname - so if your browser sends a hostname of git.hub to www.github.com, the server won't recognise it and may give the wrong result. Commented Oct 9, 2024 at 15:49
  • 4
    @psmears and even if the site would like to respond, it would likely not have a valid certificate for that (non-existing) domain name. Commented Oct 9, 2024 at 23:06
3

If you just want a short name that you can use in a browser, Firefox will let you set a "keyword" for a bookmark:

Firefox Edit Bookmark dialog

Then if you enter the keyword in the Firefox address bar, it will pick the bookmark as the first option to visit:

entering keyword in Firefox address bar

This won't affect other tools like ping, however.

9
  • Just did a test, after add the keyword, git.hub works, but sub path like git.hub/lua/lua won't. Commented Oct 9, 2024 at 8:00
  • 1
    Yes, this strictly only for the bookmarked URL Commented Oct 9, 2024 at 8:06
  • 4
    @Eric git.hub/whatever will never work anyway because the SSL certificate name won’t match, so you will get a browser error that the site is not trusted… Commented Oct 9, 2024 at 12:21
  • 3
    @Eric not necessarily. The browser will send the requested hostname to the server, and the server could simply refuse to serve any page at all for an unknown hostname. Commented Oct 9, 2024 at 14:23
  • 5
    If the server has virtual hosts, as many do, it won't recognize the git.hub hostname and won't know which virtual host configuration to use. Commented Oct 9, 2024 at 14:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.