How to Customize Your Cloud Command Line Prompt with Colors (EC2, VPS, and More)

Cover Image for How to Customize Your Cloud Command Line Prompt with Colors (EC2, VPS, and More)
DevOps5 min read

Customizing your command line prompt on a cloud instance, such as an AWS EC2 instance, can make your terminal experience more visually appealing and informative. By adding colors and useful information to your prompt, you can enhance productivity and make it easier to navigate your cloud environment. This guide will walk you through the steps to set up a custom color prompt on a Linux-based cloud instance using the Bash shell.

Prerequisites

  • Access to a cloud instance (e.g., AWS EC2, Google Cloud, or Azure) running a Linux distribution (e.g., Ubuntu, Amazon Linux).
  • Basic familiarity with SSH and terminal commands.
  • A user with sufficient permissions to edit shell configuration files (e.g., ~/.bashrc).

Step 1: Connect to Your Cloud Instance

  1. SSH into your instance: Use a terminal or SSH client to connect to your cloud instance. For example, with an AWS EC2 instance:

    ssh -i <your-key.pem> ec2-user@<your-instance-public-ip>
    

    Replace <your-key.pem> with your private key file and <your-instance-public-ip> with the instance's public IP address.

  2. Verify your shell: Ensure you're using Bash as your shell, as this guide focuses on customizing the Bash prompt. Run:

    echo $SHELL
    

    If the output shows /bin/bash, you're good to go. If not, you can switch to Bash by running:

    bash
    

Step 2: Understanding the Bash Prompt (PS1)

The Bash prompt is controlled by the PS1 environment variable, which defines the format and content of the command line prompt. You can customize PS1 to include colors, usernames, hostnames, current directories, and more.

To see your current prompt configuration, run:

echo $PS1

This might output something like \u@\h:\w\$, where:

  • \u: Username
  • \h: Hostname
  • \w: Current working directory
  • \$: Displays $ for regular users or # for root

Step 3: Adding Colors to the Prompt

Colors in the Bash prompt are added using ANSI escape codes. Here's a basic example of a colored prompt:

  • Red: \[\e[31m\]
  • Green: \[\e[32m\]
  • Blue: \[\e[34m\]
  • Reset: \[\e[0m\] (resets color to default)

Example: Creating a Colored Prompt

Let’s create a prompt that shows the username in green, the hostname in blue, and the current directory in red, followed by a $ sign.

  1. Edit the .bashrc file: Open the ~/.bashrc file in a text editor like nano or vim:

    nano ~/.bashrc
    
  2. Add the custom prompt: Scroll to the bottom of the file and add the following line:

    export PS1='\[\e[32m\]\u\[\e[0m\]@\[\e[34m\]\h\[\e[0m\]:\[\e[31m\]\w\[\e[0m\]\$ '
    

    Breakdown of the prompt:

    • \[\e[32m\]\u: Username in green
    • @: Literal @ symbol
    • \[\e[34m\]\h: Hostname in blue
    • :: Literal colon
    • \[\e[31m\]\w: Current directory in red
    • \[\e[0m\]: Reset color
    • \$: Prompt symbol ($ or #)
  3. Save and apply changes: Save the file (in nano, press Ctrl+O, Enter, then Ctrl+X to exit). Apply the changes by running:

    source ~/.bashrc
    

    Your prompt should now look something like:

    ec2-user@ip-172-31-12-34:~/myfolder$
    

    with ec2-user in green, ip-172-31-12-34 in blue, and ~/myfolder in red.

Step 4: Advanced Customization

You can further enhance your prompt by adding more information, such as:

  • Git branch: If you use Git, display the current branch.
  • Time: Show the current time.
  • Exit status: Indicate whether the last command succeeded.

Example: Prompt with Git Branch and Time

To include the current Git branch and time, you’ll need to define a function to parse the Git branch and modify the PS1 variable.

  1. Edit .bashrc again:

    nano ~/.bashrc
    
  2. Add a Git branch function: Add the following code to parse the current Git branch:

    parse_git_branch() {
        git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
    }
    
  3. Update the PS1 variable: Add this line to create a prompt with the time, username, hostname, directory, and Git branch:

    export PS1='\[\e[33m\]\t \[\e[32m\]\u@\[\e[34m\]\h:\[\e[31m\]\w\[\e[35m\]$(parse_git_branch)\[\e[0m\]\$ '
    

    Breakdown:

    • \[\e[33m\]\t: Time in yellow
    • \[\e[32m\]\u: Username in green
    • \[\e[34m\]\h: Hostname in blue
    • \[\e[31m\]\w: Directory in red
    • \[\e[35m\]$(parse_git_branch): Git branch in purple
    • \[\e[0m\]: Reset color
  4. Save and reload: Save the file and run:

    source ~/.bashrc
    

    If you’re in a Git repository, your prompt might look like:

    14:30:22 ec2-user@ip-172-31-12-34:~/myrepo (main)$
    

Step 5: Persisting Changes Across Sessions

The changes made to ~/.bashrc are persistent for your user account. However, if you create a new cloud instance or switch users, you’ll need to reapply these changes. To make the prompt available for all users, you can edit /etc/bash.bashrc (requires root permissions):

sudo nano /etc/bash.bashrc

Add the PS1 configuration there, but be cautious as this affects all users.

Step 6: Testing and Troubleshooting

  • Test the prompt: Run bash to start a new shell and verify the prompt appears as expected. If colors don’t display, ensure your terminal supports colors by checking:

    echo $TERM
    

    It should output something like xterm-256color. If not, set it with:

    export TERM=xterm-256color
    
  • Common issues:

    • No colors: Ensure the ANSI escape codes are correct and your terminal supports them.
    • Prompt not updating: Verify that source ~/.bashrc was run or restart your terminal session.
    • Git branch not showing: Ensure Git is installed (sudo yum install git or sudo apt install git) and you’re in a Git repository.