1

I have been doing a lot of work using Linux containers, and I keep forgetting which of the terminals on my screen is running inside which container, particularly when I leave them open overnight.

How can I change the shell prompt, or the terminal title based on the container that the shell runs in?

This is a general question, but it'd be great if it worked for systemd-nspawn and kde on Arch.

Johannes Ernst
  • 1,037
  • 4
  • 16
  • 26
  • Can you see any kind of container ID/name in the `env` and `set` output? – Kondybas Jun 23 '15 at 18:22
  • @Kondybas: no. I don't think there is supposed to be any, `systemd-nspawn` even cleans up the environment before instantiating the container. – Johannes Ernst Jun 23 '15 at 18:29
  • And how do you tell one from another? – Kondybas Jun 23 '15 at 18:49
  • @Kondybas: from inside the container, you can't, just like you can't tell which host you ssh'd into if they were installed from the same configuration before you started customizing (hostname, ...) – Johannes Ernst Jun 23 '15 at 19:51
  • So the most straightforward way is to set environment variable just after instantiation and then to use it as ID. – Kondybas Jun 23 '15 at 20:03

1 Answers1

1

Make sure your containers use distinct UTS namespaces then set a unique hostname in each one. For instance, things like docker set the hostname value to the container hash ID as a default in the UTS namespace associated with the main container process.

Thus with a PS1 pattern using the hostname variable you will know in which container you are when running a bash prompt.

Xavier Lucas
  • 12,815
  • 2
  • 44
  • 50