3

I have set of some (third-party) shell scripts, which are supposed to connect to remote host and start some daemons (hadoop, if that matters). These scripts rely heavily on hostname in order to configure pid folders, data directories etc.

What I need to do now is to create some test/development environment on single machine, using Arch, which means:

  • configure /etc/hosts to provide some aliases to 127.0.0.1, like data1, data2, data3 (that's the trivial part)
  • configure passwordless SSH access to localhost (which is also trivial)
  • somehow set the hostname for current SSH session, like if I connect using 'ssh data1' - then hostname should be "data1" - and this is what I can't figure out.

so I need to solve 2 problems:

  • how do I pass some environment variable or something like that to remote host, so it will be visible to all scripts there
  • how do I set hostname on remote host for that session only

and all this happens on the same computer.

1

While you can't change the hostname for a single process (well, it might be possible with namespaces), you can change the HOSTNAME environment variable.

As for forwarding environmnet variables from client to server, see the AcceptEnv and PermitUserEnvironment optins for sshd and SendEnv for ssh (see man pages sshd_config(5) and ssh_config(5) for details).

1
2

The hostname of a system is not something that you can programmatically set as just a regular use, nor is there an environment variable that will effectively change this.

You may see a variable such as $HOSTNAME but this is often just for convenience sake and doesn't change your systems hostname in any meaningful way.

I think if I were you I'd pick a environment variable of my own choosing and set it at login time as part of the user's environment perhaps, and then use that variable downstream within the scripts, instead of trying to co-op the actual hostname of the system.

The hostname command

You can change a system's name in a loose type of way by issuing the command:

$ sudo hostname <somename>

However this approach can be tricky given that the hostname is often stored in a variety of locations, some of which are only read once during boot-up and never again.

Given this I'd find it a difficult path to go down, and it will be wrought with a lot of hacking and slashing to override the system's hostname in an easy way.

I'll leave you this guide which shows some of the things that will likely trip you up when attempting to do this. It's titled: How to Change the Hostname of a Linux System.

3
  • 1
    It depends how it gets queried by what he is using. If they run hostname, you can do the trick of PATH tricks and a custom hostname in a user-based bin directory. Combine that with per-key environment variables, and he could pull it off. – kurtm Oct 17 '13 at 4:52
  • @kurtm - yes I didn't want to imply that it was impossible, just that it can be difficult given different tools will query for the hostname in different ways. It's highly dependent on the distro too. – slm Oct 17 '13 at 4:54
  • On Linux, you can set the host name in a namespace. With very recent kernels (≥3.8), this no longer requires root privileges. – Gilles 'SO- stop being evil' Oct 17 '13 at 21:29
0

SSH is usually set up to pass some environment variables that are considered safe and system-independent, namely locale-related environment variables: LC_*. So on many installations, you can pass data via the environment by setting a variable like LC_DESIRED_HOSTNAME. Which environment variables the server lets you pass is determined by the AcceptEnv setting in sshd_config, and which environment variables the client attempts to pass is determined by the SendEnv setting in ~/.ssh/config.

You can set the host name for all processes until the next reboot by calling the hostname command with an argument as root.

On Linux, if you run unshare -u someprogram, then someprogram and its subprocesses will run in a separate namespace which has its own host name. The host name inside the namespace starts out identical to the rest of the system but it can be changed independently. This needs to be done as root unless your kernel version is 3.8 or above.

unshare -u -- sh -c 'hostname "$LC_HOSTNAME"; service hadoop start'

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.