PDA

View Full Version : [ubuntu] .profile not being evaluated at login



kstuart101
May 18th, 2009, 06:50 PM
Here's a strange one,

I've just installed Ubuntu 9.04 from scratch as a desktop system, I've configured my .profile and realised that it's not being evaluated at login, I've checked /etc/passwd and my home directory and shell are correct, the .profile file has the right attributes and owner, and if I open a terminal and run 'bash -l' it's evaluated correctly, my ~/.bashrc works just fine.

So what's up with that? Any ideas, I'm pulling my hair out with this one?

Brandon Williams
May 19th, 2009, 08:58 AM
How are you logging in? Via gdm, kdm, xdm, or the console?

If you're logging in with gdm, then it should be working. /etc/gdm/Xsession checks for .profile and sources it directly.

If you're logging in with xdm, then it might not work as expected. /etc/X11/Xsession, which I think is the version of Xsession used by xdm, does not source .profile.

If it's kdm, I've got no idea, but hopefully someone running kdm will comment.

If it's a console login, do you have either .bash_profile or .bash_login? According to the man page, bash will source whichever file it finds first out of .bash_profile, .bash_login, or .profile, in that order.

Also, it might be helpful to know what you are using as an indicator that the file was not sourced at login. What do the content of your .profile look like and what are you expecting to see in the environment when you open a shell?

kstuart101
May 19th, 2009, 09:46 AM
Hi Brandon,

There are a couple of gdm processes running so I guess It's gdm, I took a look at /etc/gdm/Xsession and copy pasted line 108 into a terminal:

test -f "$HOME/.profile" && . "$HOME/.profile"

and that works so I'm assuming it is being evaluated ok in gdm but don't know how to test that beyond what I've just done.

It is during a console login (when I run terminal) that I'm aware it's not working, strangely it works if a run 'bash -l' which would indicate the mechanism is working ok, just not being triggered, also fine if at the console if I 'source ~/.profile'.

I've tried renaming the ~/.profile to .bash_profile and .bash_login, also tried having all three files but without success, ~/.bashrc works fine.

I initially noticed it wasn't working when none of my aliases were being set, also if I simply echo a message from the top of the ~/.profile nothing gets printed, no environment variables being set, custom prompt, etc...

I've only just started using Ubuntu linux as a desktop after getting tired of Vista so am pretty lost here, it's a little frustrating to fall at the first hurdle.

Brandon Williams
May 19th, 2009, 10:42 AM
As you've noted, .profile is only sourced when you run bash as a login shell. .bashrc is sourced for any interactive shell. Things that you want in your environment for every interactive shell should go in .bashrc. This will typically include alias settings, prompt overrides, and environment variables. If there are environment variables being exported in your .profile, then they can stay there, because they will have a global impact, but any variables that are not exported should be in your .bashrc. The default .profile will take responsibility for source your .bashrc file so that you get everything from .bashrc in a login shell, just like you would in a non-login shell.

If you don't want to move everything over to .bashrc for some reason, or if you're having trouble figuring out what should be in .bashrc and what should stay in .profile, then you can always configure your terminal emulator to run as a login shell. If it's gnome-terminal, then go to the 'Profile Preferences' dialog, click on the 'Title and Command' tab, and check the box next to 'Run command as a login shell'

kstuart101
May 19th, 2009, 11:09 AM
Ah, that would be it then, I misunderstood what I read, I though .bashrc was evaluated for non-interactive shells and .bashrc + .profile were evaluated for interactive shells, but if I understand right, .profile, or .bash_profile is only evaluated when bash is set as the user's login shell, that would explain why it works perfectly on my headless Ubuntu server since I ssh into it and therefore it runs as a login shell.

Presumably that's so one time setup can be performed at login, however .bash_login/.bash_logout would appear to be better suited for that so I'm still a little confused, maybe it's for compatibility with other shells, but then what's the point of .bash_profile? guess I've some more reading to do.

Also good to have an idea of how GDM starts up.

Thanks very much for the help, I'll refactor those bits :D.

kpkeerthi
May 19th, 2009, 01:00 PM
When you login, bash will look for .bash_profile first. If it is not found it will look for .bash_login. If it is not found it will look for .profile.

You can still have .profile and source it from .bash_profile.

MestreLion
February 19th, 2011, 04:31 AM
When you login, bash will look for .bash_profile first. If it is not found it will look for .bash_login. If it is not found it will look for .profile.

But remember Gnome login does not use bash. So .bash_profile is not called upon a normal, graphical, Gnome session Ubuntu login (at least not in 10.04 onwards, but prolly even before too).

A script called by Gnome after login, /etc/gdm/Xsession, manually sources ~/.profile. And neither use bash, as /etc/gdm/Xsession has a #!/bin/sh directive, which uses dash, not bash.

To sum it up, here is what i do:

- For stuff i want to run just once every session, after login, i use ~/.profile, as it is sourced by both GDM and console logins. But im careful not to add any bash-specific syntax (bashisms) on it, since bash is not run when i use a Gnome login. My $PATH additions go there.

- For other stuff, meant to be run on everytime i open a terminal, i use ~/.bashrc, as it is called directly by bash. But be aware that it is NOT sourced at login when using Gnome, so $PATH aditions are not valid for invoking GUI apps directy (via ALT+F2). Its works only within in a terminal (including gnome-terminal). I use mostly for aliases.

Cheers!