0

Running an Amazon EC2 instance with Ubuntu 22.04. The elastic ip address is 52.120.94.72.

I want the bash prompt to read like this (no space)

della@52.120.94.72:~/working_directory$

Based on some advice on the net, I edited the top of the /etc/hosts file like this

127.0.0.1 localhost 52.120.94.72

and even added the following line to .bashrc

export PS1='\[\033[0;32m\]\u@\h:\[\033[36m\]\W\[\033[0m\] \$ '

But it is not working, and currently, the bash prompt looks like

della@52:~ $

which is ridiculous. How to make it appear like I want. Also, is the $HOSTNAME environment variable important in this context? Particularly, if I run some web server in this instance, I can make calls just using the ip address, right? No need for the hostname.

Della
  • 175
  • 1
  • 1
  • 5

1 Answers1

3

Letting your prompt actually derive that IP-address is, as far as I know, rather more difficult. Bash does not provide a ready-made variable to use for that according to the list with special characters here.

I think the easiest is to simply hardcode the IP-address you want to see in your prompt definition:

 export PS1='\u@52.120.94.72:\w\$'

where \u, \$ and \w are some of those special characters that get expanded by your bash shell according to the definitions from here :

  • \u
    The username of the current user.
  • \w The value of the PWD shell variable ($PWD), with $HOME abbreviated with a tilde ~ (uses the $PROMPT_DIRTRIM variable).
  • \$
    If the effective uid is 0, #, otherwise $.

With the additional escapes in the example you quoted:

 export PS1='\[\033[0;32m\]\u@52.120.94.72:\[\033[36m\]\w\[\033[0m\]\$'

you get a prompt formatted with ANSI escape sequences/codes that instruct the terminal program you're using to apply specific formatting and colouring.

See https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences for a detailed overview.


You could of course rely on external helper to derive your IP, such as query the instance meta data URI :

MY_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4)
export PS1='\u@$MY_IP:\w\$' 
diya
  • 1,771
  • 3
  • 14
  • This definitely put me in the right direction - a bit more dynamically, you could use `export ELASTIC_IP=$(curl ipinfo.io/ip)`, then to set it you use `PS1='\u@$ELASTIC_IP:\w\$'`. If you put this in your .bashrc or profile with the export, you also have access to that value throughout your session - `echo $ELASTIC_IP` – Julian Feb 04 '23 at 19:00