Is there a way to provide a specific hostname to an application that differs from the machine hostname on Linux?

3

1

Is there a way to provide a specific hostname to an application that differs from the machine hostname on Linux? Basically, I have a piece of software that expects the local machine to have a certain hostname that differs from my machine hostname. Rather than change the machine hostname globally, I'd like a mechanism where I can either run the program in an environment with the specified hostname or run the program directly with the specified hostname.

wyer33

Posted 2015-10-12T22:54:53.420

Reputation: 135

How is the hostname used in the program? For DNS purposes? – heavyd – 2015-10-12T22:59:38.947

Answers

3

It's possible with root privileges. Use unshare --uts to create a new UTS1 namespace, set the desired hostname in it, then su back to your account and run the app. For example:

ongun@foo$ sudo -s
~ create a new namespace ~
root@foo# unshare --uts /bin/sh
~ this terminal now has its own namespace – change the hostname ~
root@foo# hostname quux
~ switch back ~
root@quux# sudo -s -u ongun
ongun@quux$ /usr/bin/game

Namespaces are limited to their creator process by default, so the new hostname will be visible only to programs you run inside the "unshare"d window.

Note: Don't forget to actually add the new hostname to /etc/hosts as 127.0.0.1, as many programs expect / depend on it to be resolvable.


1 Don't ask what "UTS" means. Best I know is it's a leftover from ancient Unix.

user1686

Posted 2015-10-12T22:54:53.420

Reputation: 283 655

If the program only needs to resolve the hostname, then you would only need to modify /etc/hosts, no need to create a new namespace. This is slightly more secure as you only need root privilege once when editing /etc/hosts rather than during every application startup. – Lie Ryan – 2016-01-25T13:32:00.987

0

If your application does not directly read output of hostname or /etc/hostname file, you can always create an alias for your machine like below.

#
# /etc/hosts: static lookup table for host names
#

#<ip-address>   <hostname.domain.org>   <hostname>
127.0.0.1       localhost.localdomain   localhost MYHOSTNAME   OTHER_ALIAS
::1             localhost.localdomain   localhost MYHOSTNAMEv6 OTHER_ALIASv6

or you can try creating a chroot with a different host name like here

Ongun

Posted 2015-10-12T22:54:53.420

Reputation: 51

0

I presume you do not have the source code of the program in question. Reading a machine's hostname is done thru a sys call, gethostname(), which it would be hard to circumvent without access to the program source code.

You should then use a virtual solution to run the program. Containers, dockers, VMs have the sufficient degree of isolation to allow you to rename them to the required value, without compromising your host machine. A container is very lightweight solution which will drain very little of your host resources.

MariusMatutiae

Posted 2015-10-12T22:54:53.420

Reputation: 41 321