8

I have several machines and the hostnames are really long.. i.e. companyname-ux-staging-web1.companyname.com. So my prompt looks something like

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

I'd like to shorten that up for all users on all machines with the least amount of work. From what I read I have a couple options, but they all have their drawbacks.

I could change the hostname, but that would likely affect applications. Not a great choice.

I could alter also $PS1 at login for all users by editing all .bashrc for existing users, and edit /etc/skel/.bashrc for potential new users. That's a lot of work across >10 machines.

What's my best option or what have I overlooked?

1
  • 1
    you didn't tell us what distro. distro's have gone to great effort to build a combination of standards, conventions, and customization points around shell environments. whatever you wind up doing, make sure you're working with your distro not against it.
    – cagenut
    Jan 4, 2011 at 18:36

8 Answers 8

6

You should probably export PS1. Instead of editing a user's bashrc, you should edit the system bashrc: a user should be able to override a prompt with their choice.

Secondly, to distribute the file use either scp or clusterssh. If you set up a rsa key you don't even need to enter your password more than once for scp:

eval `ssh-agent`
ssh-add
for h in `cat ~/hostlist`; do
    scp ~/newbashrc ${h}:/etc/bashrc
done
eval `ssh-agent -k`
0
9

you can create an executable script in the directory /etc/profile.d/, for example: /etc/profile.d/custom_prompt.sh, with this content:

#!/bin/sh

export PROMPT_COMMAND=’export PS1="[\e[0;35m][\u \t \h \W]\$ [\e[0m]"‘

With this method, you don’t need to modify any file in order to change the bash prompt for all the users.

3
  • 1
    This is the right command as per the sequence of ~/.bashrc and ~/.bash_profile and /etc/profile.d/*.sh works. I was not getting right PS1 set on all servers. In some it works in some it doesn't. But this one solved my problem. Also ensure you give the script 755 permission for other users the make use of it.
    – mannoj
    Oct 18, 2019 at 11:58
  • Works on Ubuntu 22.04. All other ways are overriden by files in user home folder. Jan 30, 2023 at 22:38
  • This does not work for prompts spawned via sudo su username. Feb 22, 2023 at 14:45
3

It shouldn't be any more work than any existing configuration management you're using to deploy new configurations across these ten servers. If this is a persistent problem for your organization, you definitely want to look into a configuration management tool like Puppet/Chef/Cfengine and a deployment tool like MCollective/RunDeck/Capistrano/Fabric.

2
  • +1 I agree with your suggestion for using puppet although the upfront "cost" would be greater than just editing the ten machines by hand if all incredimike wants to do is shorten the hostname. But going forward, the continual update and management time of 10+ servers is greatly reduced with Puppet. I'm using it to manage 200+ servers and am pretty happy with it.
    – Patrick R
    Jan 4, 2011 at 18:41
  • Yes, I also +1 this as I will be setting up puppet in the future to streamline management and help ensure machine state but, as Patrick suggests, the upfront cost is too high to set it up right now. Jan 4, 2011 at 19:22
3

Edit /etc/profile

/etc/profile is run when a user logs in. ~/.bashrc is run for other shells (e.g. opening an xterm)

export PS1="[\e[0;35m][\u \t \h \W]\$ [\e[0m]"

You probably will want to take out the \h

  • \u: Display the current username
  • \h: Display the hostname
  • \W: Print the current working directory
  • \H: Display FQDN hostname
  • \@: Display current time in 12-hour am/pm format
  • \t: the current time in 24-hour HH:MM:SS format
  • \e: an ASCII escape character (033)

https://wiki.archlinux.org/index.php/Color_Bash_Prompt

1

You could edit the system wide bashrc in /etc/bashrc but that is easily overwritten by any user with shell access. You could pretty easily script something to push out a new .bashrc for a user list using scp.

1
  • 1
    I'm not sure it's all that advisable to overwrite the user's settings in this case. If the user wants a longer $PS1, why not? It's a bad precedent to edit anything in a user's home directory.
    – Ben Cotton
    Jan 4, 2011 at 18:59
1

As stated scp is probably the easiest with a simple script. Another option is rysnc. Also, you can just cron to do it with sshkeys.

0

Your second option seems feasible if you can send commands to all the servers at once to update files. For this you could use pssh on linux (http://code.google.com/p/parallel-ssh/) or PuTTY Command Sender (http://www.millardsoftware.com/puttycs) with PuTTY on Windows.

You could build a sed/awk command that could replace the line in the skel .bashrc and /etc/bashrc and users bashrc files. Then send this as one command to all the systems.

The sed command is going to likely be the most work. Unfortunately, I am not as good with sed and awk. Someone else may be able to offer more insight in this area. Hope this information helps!

0

I had to do this for multiple ubuntu servers and for all users, I wanted to have the full FQDN on the prompt instead of just the hostname (the opposite of what you were doing). I found that changing /etc/bash.bashrc is not enough. I also tried fiddling with diffent profile.d files but it was useless because the home .bashrc is always applied last.

What I ended up doing was pushing the below script with ansible to all servers and that did the trick for all users and for all future applications.

    #!/bin/bash

        for userfolders in `ls -d /home/*/`
          do
            for bashfiles in `ls -1 ${userfolders}.bashrc`
            do
               sed -i 's|u@\\h|u@\\H|g' $bashfiles
            done
        done
   sed -i 's|u@\\h|u@\\H|g' /etc/bash.bashrc
   sed -i 's|u@\\h|u@\\H|g' /etc/skel/.bashrc
   sed -i 's|u@\\h|u@\\H|g' /root/.bashrc

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .