0

I want to customise my terminal on an Amazon linux device. I have colours using $(tput setaf 33);, user name with /u but the IP address not there.

#blue=$(tput setaf 33);
PS1="\[${blue}]\u$(tput sgr0)"
PS1="${blue}[\u@\w $(tput sgr0)"
PS1+=""
export PS1;

1 Answer 1

2

Well... you would need to know what IP address you actually want. Is it a public IP address? Is it a private subnet address? Do you only have 1 IP address? A few things you would need to figure out.

But, if we just assume that we want to have whatever ip address your VM thinks it's address is then:

Add the following to your .bashrc

PS1="\[$(tput setaf 33)\][\u@$(ip -4 -o addr show scope global $(ip -4 route | grep default | head -n1 | awk '{print$5}') | awk '{gsub(/\/.*/, "",$4); print $4}' | head -n 1) \w ]\[$(tput sgr0)\] \[$(tput setaf 34)\]\\$\[$(tput sgr0)\] "

To throw an IP address into your prompt.

Now, because that's not really human friendly, let's break it down:

Make the prompt blue (honestly I can't remember why I always wrap with \[ \] I just remember someone telling me I needed to do that, so I guess see what works for you:

\[$(tput setaf 33)\]

This bit you already had figured out:

[\u@ 

This insanity to find the default IP address, it's a bit hard to break down without just reading through the ip man pages but basically we are searching for the interface marked as default then grabbing the IP address and stripping all the extra stuff out with awk/grep/head:

$(ip -4 -o addr show scope global $(ip -4 route | grep default | head -n1 | awk '{print$5}') | awk '{gsub(/\/.*/, "",$4); print $4}' | head -n 1)

I tend to want to know my ${PWD}:

\w ]

Reset our colour scheme just to be stafe (I've found weird stuff happens when you keep layering colours without resting:

\[$(tput sgr0)\] 

Set some other colour:

\[$(tput setaf 34)\]

Set the $ or # prompt which is handy if you want it to be obvious if you are logged in as root:

\\$

Then reset colour again:

\[$(tput sgr0)\]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.