I need to map a word "foo" to a domain name "foobar.com" So it should be able to run ping foo or curl it.

alias foo='foobar.com'

giving below issue.

curl -k foo
curl: (6) Could not resolve host: foo

this is run on kubernetes and domain name IP address is dynamic.

Basically I want to map K8s service name to a internal domain name which IP dynamic.

  • From man bash: Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. – user4556274 May 29 '18 at 15:10

What you are trying to do is a server alias here.

Option A (dirty ugly)

You need to edit the /etc/hosts file for this


${IP} foo foo.com

then you will be able to

curl foo -H 'Host: foo.com'

The host header is necessary as the vhost alias is foo.com

Option B (clean and nice)

Create a server alias, depending on your web server it may be different config.

On my apache httpd config I use:

/etc/httpd/sites-available - contains the config for each vhost

/etc/httpd/sites-enabled - contains the symlinks to the sites-available config files, this way I can activate/deactivate vhosts without modifying or renaming the files at sites-available, I just need to unlink to bring down a vhost

and I have modified the conf


IncludeOptional sites-enabled/*.conf`

so let's say I had

/etc/httpd/sites-available/foo.com.conf and a symlinked version at /etc/httpd/sites-enabled/foo.com.conf

then do

cp /etc/httpd/sites-available/foo.com.conf /etc/httpd/sites-available/foo.conf

modify foo.conf to match this:

ServerName foo
ServerAlias foo

and then ln -s /etc/httpd/sites-available/foo.conf /etc/httpd/sites-enabled/foo.conf

finally restart apache:

systemctl restart httpd

Note all these actions may require root or sudo (power user)

Assuming your system has a DNS server, nothing else would be needed; otherwise the /etc/hosts file will need to be modified too, but when you curl you won't need to pass a host header as the vhost alias will be foo.

i.e. in this case curl http://foo will work fine, while on option A you need a host header too.

| improve this answer | |
  • Thanks. but this is run on kubernetes and domain name IP address is dynamic. Basically I want to map K8s service name to a internal domain name which IP dynamic. – Ruwan Ranganath May 29 '18 at 15:26
  • use a server alias then, should be the easiest. I would avoid using bash/cli aliases in this case, the result could be very unexpected, otherwise the hosts file is your best bet from my POV – Kramer May 29 '18 at 15:27
  • 2
    and I think this is exactly what you need too: kubernetes.io/docs/concepts/services-networking/dns-pod-service :) – Kramer May 29 '18 at 15:30

For your simple use case, you can use a variable:

$ foo="foobar.com"
$ curl -k "${foo}"

Do all of your use cases center on terminal commands?

| improve this answer | |

You'll need (the possible footgun) that are "global" aliases to do a mid-line substitution. These are provided by ZSH.

$ exec zsh
% alias -g foo=foobar.com
% print foo

However note that any bare use of foo will get substituted so if foo could also be an argument that must not be substituted then bad things could happen (hence, footgun—a command could break, or the wrong args could cause something else to happen, or…)

% print 'foo'

so that's extra work you'll need to do with your brain to always remember when foo needs to be quoted versus when it does not, depending on how common foo is.

| improve this answer | |
  • I would recommend to keep concepts separated, a bash alias vs a vhost alias :) – Kramer May 29 '18 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.