Optimizing Linux Command Prompt Display: System-wide PS1 Configuration for Long Hostnames


4 views

When working with enterprise Linux environments, we often encounter excessively long FQDNs like companyname-ux-staging-web1.companyname.com. This creates cluttered command prompts:

[root@mycompany-ux-staging-web1 ~]#

Such prompts waste terminal real estate and reduce readability. While changing the actual hostname might break applications, we have better solutions.

The most efficient system-wide solution involves modifying /etc/bashrc (RHEL/CentOS) or /etc/bash.bashrc (Debian/Ubuntu). This affects all users while respecting individual customizations:

# /etc/bashrc modification
if [ "$PS1" ]; then
  # Extract first part of hostname before first hyphen
  SHORT_HOST=$(hostname -s | cut -d'-' -f1)
  PS1='[\u@$SHORT_HOST \W]\$ '
fi

For more modular management across multiple servers, create /etc/profile.d/custom-prompt.sh:

#!/bin/bash
# Custom prompt configuration
if [ -n "$BASH_VERSION" ]; then
    # Use color and show only domain suffix
    PS1='$$\e[1;32m$$\u$$\e[0m$$@$$\e[1;34m$$\H$$\e[0m$$:\W\$ '
    
    # Alternative: Show only first two hostname components
    # PS1='[\u@$(hostname | cut -d'.' -f1-2) \W]\$ '
fi

Several methods exist to shorten the displayed hostname:

# Method 1: Remove domain entirely
PS1='[\u@${HOSTNAME%%.*} \W]\$ '

# Method 2: Custom truncation
PS1='[\u@$(echo $HOSTNAME | sed -e "s/companyname-//" -e "s/.companyname.com//") \W]\$ '

# Method 3: Show environment only
PS1='[\u@${HOSTNAME#*-} \W]\$ '

For managing 10+ machines, use Ansible:

---
- hosts: all_linux_servers
  become: yes
  tasks:
    - name: Ensure custom prompt directory exists
      file:
        path: /etc/profile.d
        state: directory
        
    - name: Deploy global prompt configuration
      copy:
        content: |
          #!/bin/bash
          PS1='$$\e[1;32m$$\u$$\e[0m$$@$$\e[1;33m$$${HOSTNAME%%-*}$$\e[0m$$:\W\$ '
        dest: /etc/profile.d/custom-prompt.sh
        mode: 0644

The solution maintains user-specific ~/.bashrc settings by:

  • Checking for existing PS1 definitions before applying changes
  • Using PROMPT_COMMAND for dynamic adjustments
  • Adding comments explaining the global configuration
# In /etc/bashrc
if [[ -z "${USER_PS1_SET+x}" && -n "$PS1" ]]; then
    # Apply global PS1 only if not already set by user
    export PS1
    export USER_PS1_SET=1
fi

When managing enterprise Linux systems with lengthy FQDNs like companyname-ux-staging-web1.companyname.com, the default bash prompt becomes unwieldy. The standard [user@hostname directory] format consumes valuable terminal real estate without providing proportional value.

For uniform prompt customization across all users and machines, consider these implementation strategies:

# Option 1: /etc/profile.d customization
# Create /etc/profile.d/custom_prompt.sh with:
export PS1='[\u@\h-\W]\$ '

# Option 2: Global bashrc configuration
# Edit /etc/bashrc or /etc/bash.bashrc:
if [ "$PS1" ]; then
  PS1='[\u@\H-\W]\$ '
fi

For a balanced solution that maintains host identification while reducing clutter:

# /etc/profile.d/shortprompt.sh
# Get first component of hostname
SHORT_HOST=$(hostname -s)
export PS1='[\u@${SHORT_HOST} \W]\$ '

Use Ansible for multi-machine deployment:

# prompt_config.yml
- hosts: all
  tasks:
    - name: Create prompt config
      copy:
        dest: /etc/profile.d/shortprompt.sh
        content: |
          #!/bin/sh
          SHORT_HOST=$(hostname -s)
          export PS1='[\u@${SHORT_HOST} \W]\$ '
        mode: '0644'

After implementation, verify with:

source /etc/profile
echo $PS1

For existing sessions, users will need to reconnect or manually source the configuration.

For power users, consider adding color and git branch status:

# Enhanced prompt example
export PS1='$$\033[01;32m$$\u@\h$$\033[00m$$:$$\033[01;34m$$\W$$\033[00m$$$(__git_ps1 "(%s)") \$ '