HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
cancel
Showing results for 
Search instead for 
Did you mean: 

test if root user

 
digger86
Occasional Advisor

test if root user

I have a profile (bash in Linux) that I use to test if you are root and if the user is your name turns red. I like to use this for the admins that su to root. How would I get this to work with /usr/bin/sh? See attachment to view what I would do in Linux.

Thanks

7 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: test if root user

Hi:

You might use 'tput' to perform some simple terminal output format changes:

http://www.docs.hp.com/en/B2355-60130/tput.1.html

Remember, that if you 'su' to root as 'su - root' then root's login profile -- '${HOME}/.profile' is run. However if the transition is done with 'su root' the '.profile' is not read.

Regards!

...JRF...
digger86
Occasional Advisor

Re: test if root user

Thanks for your response.

I want to avoid using root /.profile, I would like to keep the users /.profile. Is this possible?
James R. Ferguson
Acclaimed Contributor

Re: test if root user

Hi (again):

> I want to avoid using root /.profile, I would like to keep the users /.profile. Is this possible?

Then you must 'su root' instead of 'su - root'. But then, nothing executes to signal to you that the environment has changed. See the manpages for 'su' for more information.

By the way, don't ever change 'root's default shell, as defined in '/etc/passwd' to be anything other than '/sbin/sh'. To do so may lead to a system that won't startup.

Regards!

...JRF...
Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: test if root user

Hi (again):

One standard in HP-UX is that root's '.profile' uses a PS1 prompt that begins with a "#" whereas non-root '.profile's use a "$" character as the first character. The presence of a prompt with a "#" is assumed to be that of a 'root' session.

Regards!

...JRF...
digger86
Occasional Advisor

Re: test if root user

Thx again :)

I would like the color change to indicate the change to root instead.

On the comment about root's shell. A user should not #su root without the "-" then because you may not be using the shell that is definded in /etc/passwd which could lead to system instability you suggests? If that the case I will change the default shell to /sbin/sh. Thanks.
Bill Hassell
Honored Contributor

Re: test if root user

> ...not use root's .profile...

That is not a good idea. root's .profile should be written to constrain the environment. Becoming root with some crazy user profile is asking for trouble.

> ...change to /sbin/sh...

This is the only shell root should have. While it is identical to /use/bin/sh (the POSIX shell), /sbin/sh is the only available shell in single user mode.

> ...test for root...

If you simply su to root, nothing will change because $PS1 has not bee reevaluated. You need to go through a common profile for each change in a login, typically /etc/profile. That's where the user id can be tested. NOTE: be sure to test for UID 0 and not LOGNAME or whoami. Although bad sysadmin pract5ice, there may be multiple root users with different names. Use $(id -u) and test for 0 like this:

[ $(id -u) ] && CODE="$(tput bold)" || CODE=""

Now as for red, real terminals don't have colors. There imitators that have enhancements but it a consistent feature in terminal emulators such as hyperterminal, SecureCRT, Reflection, PuTTY, xterm, hpterm, Attachmate, (and the list goes on).

The better choice (unless you can guarantee the terminal being used) is to use bold or inverse or underline, something the majority of terminals understand. This also requires that TERM is correctly defined (never hardcoded) using ttytype:

eval $(ttytype -s)
[ $(id -u) ] && USER="$(tput bold)root$(tput sgr0) #" || USER="$(id -un) $"
export PS1='$USER'

man terminfo
man tput


Bill Hassell, sysadmin
Jim Walls
Trusted Contributor

Re: test if root user

/usr/bin/sh is not identical to /sbin/sh (except in the functional sense).

eg.
[root@wuwst /root ] ls -l /usr/bin/sh
-r-xr-xr-x 2 bin bin 286720 Nov 15 2007 /usr/bin/sh
[root@wuwst /root ] ls -l /sbin/sh
-r-xr-xr-x 1 bin bin 610304 Nov 15 2007 /sbin/sh

The most important reason for using only /sbin/sh for root is that this shell is statically linked and has all its suporting routines included in the module. This guarantees that the shell will function even if the /usr file system is unavailable.