Bart C
Bart C

Reputation: 1547

How to detect which server I am using in .bash_profile and apply settings accordingly

I have a few virtual machines running Fedora 20 and one NAS box from which the HOME directories are mounted on all VMs. It works great as users have to maintain one copy of git settings, VNC settings SSH keys and so on. The problem is that regardless of whichever VM they use they always use the same .bash_profile which references always the same .bashrc. I say it's a problem because I want to apply some settings in there on per user basis but only for one specific VM. To be specific I want to change umask for few users on one specific isolated server.

So I thought there must be a way to conditionally load some settings through .bash_profile depending on the IP/hostname of the server.

I thought, just like .bash_profile references .bashrc I could reference another file with this setting which only exists on the server it will affect. Not the other servers this reference would not be valid. This is not an elegant solution, it would throw errors.

I've tried googling this and the results usually point me to stackoverflow where somebody had a similar question but this time I couldn't find anything.

Does somebody have an idea how to solve this?

Thanks in advance for any help.

Upvotes: 0

Views: 1504

Answers (2)

Bart C
Bart C

Reputation: 1547

This is what I tested and does what I want it to do. Most of my users look into .bashrc while changing their aliases so to make sure it won't be accidentally altered I left in in .bash_profile

TargetVM="vm1.example.lan"

if [ "$TargetVM" = "$HOSTNAME" ]; then
        umask 077
else
        umask 027
fi

Upvotes: 3

that other guy
that other guy

Reputation: 123460

You can put your host specific commands in .bashrc_myhostname and source it if it exists:

[ -f "$HOME/.bashrc_$HOSTNAME" ] && . "$HOME/.bashrc_$HOSTNAME"

or you can hard code commands for certain hosts with an if or case statement:

if [ "$HOSTNAME" = "mytestserver" ]
then
  ulimit -c unlimited
fi

Upvotes: 4

Related Questions