[tutorial] set user@hostname:dir to guest terminal session

I maintain several servers, and recently added a DietPi based server to the line-up.
I have a terminal session open for each server while doing maintenance stuff / monitoring. I noticed that all servers where ‘labeled’ as the REMOTE user@host:dir, whereas the DietPi server was labeled as LOCAL user@host:dir

Making things a little bit confusing when switching tabs in the terminal to find the correct host.

So here is the instruction on how to change this on the DietPi host:

  1. open a terminal session to your DietPi and login with the user you normally log into with
  2. nano ~/.bashrc
  3. add the following to the bottom of that file:
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac
  1. save and close the file

You should instantly see the tab-label change in your terminal session, otherwise a reconnect would certainly do the trick :slight_smile:

Hope this helps.

BTW: I think this should be a nice want-to-have out of the box as this is what most linux distributions do (the ones I have anyway).

2 Likes

thx for sharing

@MichaIng FYI

Not sure whether I understand correctly. As of /etc/bash.bashrc, the default PS1 (command prompt) on Debian and DietPi is

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

This is not touched. What you do in ~/.bashrc looks like your should have the same info doubled now. Also I’m not sure what you mean by “REMOTE” vs “LOCAL”. Can you paste a before/after command prompt?

Okay, so here we go with screenshots before and after. With REMOTE I mean other systems (VPS / DietPI), with LOCAL I mean the desktp I am using myself.

So in the image I have 3 servers (1,2,3) open in tabs and 1 (4) is my local machine / terminal session.
My local desktop is ruud-PEAQ, my username is ruud > ruud@ruud-PEAQ:~

As you can see before the change in .bashrc, tab 3 displays the same as tab 4
After the change, tab 3 holds the correct user / hostname (for the dietpi server session

Before:

After:

Can you also show the command prompt itself before and after, as well as:

echo "$PS1"

EDIT: Ah found it: https://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html
So you prepend the info to the terminal console header within brackets. This is not shown on the command prompt.

Hence more interesting would be what the above command shows on the other servers where this change is not required. We use the default /etc/bash.bashrc from Debian, hence on all Debian systems this should be the same. Only other idea I have is that the OpenSSH server does some magic to adjust the terminal header which Dropbear does not.

on DietPI with changed bashrc it outputs:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

on DietPi without changed bashrc it outputs
${debian_chroot:+($debian_chroot)}\u@\h:\w\$

on Ubuntu server it outputs:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$

When logging in directly on the console on my VPS, so not via OpenSSH the output is:
${debian_chroot:+($debian_chroot)}\u@\h:\w\$

Where DietPi is on Dropbear and Ubuntu on OpenSSH.

So I think that it is indeed dropbear who is missing the ‘magic’ of prepending that OpenSSH has

for testing, you could revert your changes done and switch to OpenSSH

Hi, sorry, not possible as this system is the only one I currently have and is in production (off-site). So I only have ssh access and switching would break that.

I will however (eventually) buy an additional one for backup / testing, but I decided that I first do a donation as I see and appreciate how much time you all spend in maintaining DietPi and managing / servicing the community :slight_smile:

understood

not correct, using dietpi-software will keep current session active as long as not closed manually. :wink:

many thanks. Much appreciate your support / contribution.

I found it:

cat /etc/skel/.bashrc

This is the default .bashrc for new users, which contains the code block along with some other colouring and stuff. The root users however gets a minimal variant from /usr/share/base-files/dot.bashrc.

Means if you login with another user, like dietpi, the adjusted terminal header should be there already, and the adjustment is hence only needed for root. And an alternative would be (if /root/.bashrc is still the original one):

cp /etc/skel/.bashrc /root/.bashrc

Good find @MichaIng, just checked this and for ‘regular’ recreated users this is indeed the case.
But not for the dietpi user account.
So both root and dietpi have the minimal variant :slight_smile:

So instruction is still valid and it also works with the cp line you provide (with the note that this will override any other changes in that file)

Ah you are right, I just found that our user creation script copies .bashrc and .profile from the root users home to the new user’s home :smile:. Probably we should skip that, or we should generate own skeleton files (the default one contains a bunch of at best redundant commands), or better enhance the global login script to set the PS1 and some other little enhancements we want, with some nice ls colouring and aliases added, like:

eval "$(dircolors)"
alias ls='ls -A --color=auto'
alias l='ls -lh'
alias ll='l -L'

:slightly_smiling_face:.