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.

5
  • Can you see any kind of container ID/name in the env and set output?
    – Kondybas
    Jun 23, 2015 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. Jun 23, 2015 at 18:29
  • And how do you tell one from another?
    – Kondybas
    Jun 23, 2015 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, ...) Jun 23, 2015 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, 2015 at 20:03

1 Answer 1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .