sindresorhus / pure

Pretty, minimal and fast ZSH prompt
MIT License
13.22k stars 977 forks source link

Show user@host when running in a container #564

Closed TimSpence closed 4 years ago

TimSpence commented 4 years ago

Issue

When running a shell inside a docker or LXC container, the pure prompt does not display user@hostname as when logged in to a remote computer via SSH.

Fix

After executing a command on the wrong LXC container yesterday, I found a few reliable methods for determining whether the shell is executing inside a container.

  1. In LXC, the environment contains container=lxc.
  2. In docker and sometimes in LXC, the cgroup for the init process indicates "docker" or "lxc".

After the fix, the pure prompt retains the default behavior which hides user@host when physically logged in to a computer.

Testing

LXC:

❮ lxc ubuntu dotfile-tester

ubuntu@dotfile-tester ~
❯

Ubuntu 20.04 (bare metal):

❮ ssh nuc
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-45-generic x86_64)
...
Last login: Thu Sep 17 18:30:41 2020 from 192.168.1.178

tim@nuc ~
❯

OS X Catlina (physical computer, attached with keyboard:

❯ hostname
mac-mini

~
❯

Docker container:

❯ ls
Dockerfile          LICENSE

root@50867f260801 ~
❯
MattiNieminen commented 3 years ago

I personally run a lot of things locally in a container and the container share my hosts hostname and username. Therefore it's not really meaningful for me to show this information. Couple of options:

1) Add an option for disabling this feature. 2) Check that if the username@host is identical between the host and the container, don't show it.

sindresorhus commented 3 years ago

Check that if the username@host is identical between the host and the container, don't show it.

👍