Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LOGNAME not changing when I su -

Status
Not open for further replies.

bi

Technical User
Apr 13, 2001
1,552
US
When I su - <username> for some users (from root to the new user), the LOGNAME environment variable doesn't change. I have it set up for my PS1 to put my logname in the prompt so I know who I am. This happens on all systems with the same user. But it doesn't happen with other users. It happens on Solaris 7 and Solaris 9.

Anyone have any idea what I'm doing wrong? I do put the - in the su so the .profile is read.
 
Puzzling.

Are you sure it's LOGNAME that's not being updated and not your PS1? i.e. if you su - user and then echo $LOGNAME does it still say 'root'?

Where do you actually set this PS1 so that it takes effect for the substituted user?

Do these users use standard shells? Annihilannic.
 
Yes. It's logname. I set my PS1 by an environment file that is called from .profile. Both users use ksh.
 
bi,

The LOGNAME variable will not change, but if you use the logname command it will return the username you su'd to. To set PS1, try using `logname` instead of $LOGNAME. That's worked for me in similar situations.


Good Luck. Have a Great Day!! :)
~Sol
Sys Admin
 
bi,

Let me correct myself... LOGNAME should contain the current user, logname returns original login username. I stated it backwards. Anyway, below is the line I use for my PS1 variable. If the new user uses a different shell, the prompt will not be the same though.

PS1=&quot;`hostname`{${LOGNAME}}> &quot;

screen shows...
host1{user1}> _

If user uses a different shell, the prompt could be a default prompt or a prompt the user has defined in the login initialization file for his default shell.

Good Luck. Have a Great Day!! :)
~Sol
Sys Admin
 
Since LOGNAME is set by either login or su itself the only reason I can think of is that it's being clobbered somewhere in the /etc/profile, .profile or your environment setting file.

What operating system are you using? Annihilannic.
 
Not sure if this will help, but in .profile PS1 on one of our boxes (Solaris), the format is: `'$LOGNAME'`

Cheers.
 
Sorry, ignore that, I was miscounting my apostrophes (painful experience) ;-)
 
try: man the-shell-you-use
you will get a list of vars setted by login
and NOT resetted by su
LOGNAME is one of them.

soladm: i don't like the orgie:
PS1=&quot;`hostname`{${LOGNAME}}> &quot;
try:
PS1=`hostname`&quot;{$LOGNAME} > &quot; -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Thanks, all, for answering. Here is what I'm running in my environment file to change the PS1:

export HOST=`/usr/bin/uname -n`
export LOGNAME=`/usr/bin/logname`
export PS1='$LOGNAME@HOST: PWD >'

This works great for some users and not others. When I login as root and su - oracle, however, the PS1 stays with the name of root. When I do an echo $LOGNAME, it still says root. When I run the command it still says root. id gives me oracle.

If I su - <ordinary user>, it works. echo $LOGNAME gives me <ordinary user>, logname gives me root, and id gives me <ordinary user>, and my PS1 looks great.

Both these users use ksh, so it must be something in the .profile of oracle. I'll look into that.

 
bi,

Here is your problem.

export LOGNAME=`/usr/bin/logname`

LOGNAME is an environment variable and is automatically set at login or when you use su. Do NOT reset using the logname command. You get different results from each. Simply use the lines you used to set HOST and PS1 without using the &quot;export LOGNAME=`/usr/bin/logname`&quot; line.

Like so,

export HOST=`/usr/bin/uname -n`
export PS1='$LOGNAME@HOST: PWD >'

This should do it for you.
Good Luck!


To address the comment from jamisar for clarification:

I wanted the prompt to have the hostname followed by the username enclosed in braces and then the greater than symbol, like this: hostname{username}>

That is why there are two sets of braces. The innermost set of braces is used to enclose the variable name. Just my way to represent a variable. Your way definitely works as well, just a difference in programming style.


Thanks.
Have a Great Day!! :)
~Sol
Sys Admin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top