DevHeads.net

root .bash_profile?

Hi folks. Just wondering how I can implement an automatic .bash_profile for root. I have to load my user .bash_profile every time I get into root, and I would like a better solution. There is no /home/ for root, so I’m a bit confused if this is even allowed.

Any insight appreciated.

Cheers, Bee

Comments

Re: root .bash_profile?

By Stephen John Smoogen at 05/13/2019 - 09:11

While moving /root to /home/root is done in someplaces, it only works if
/home is not on a different partition. If you put /home on a different
partition you will find all kinds of weird behavior happening on start up.

Re: root .bash_profile?

By Bee.Lists at 05/13/2019 - 13:28

No, this isn’t a case of multi partitions, clusters, or anything silly. I just want a set of aliases loaded for su. /root/.bash_profile isn’t loading, and there isn’t any obvious choice as to where the loaded .bash* were loading from.

Cheers, Bee

Re: root .bash_profile?

By Chris Adams at 05/13/2019 - 13:31

Once upon a time, Bee.Lists <bee. ... at gmail dot com> said:
.bash_profile will not be read when you just run "su", because
.bash_profile is read in a login shell, and "su" does not create a login
shell.

.bashrc will be read (and is really where aliases belong anyway), or you
can "su -" to create a login shell.

Re: root .bash_profile?

By Bee.Lists at 05/13/2019 - 13:40

OK that’s exactly what I just was questioning. The documentation wasn’t clear on the ‘man bash’ (INVOCATION) notes.

So I entered my inclusion of my aliases file (it’s my own) inside .bashrc.

Thank you

Cheers, Bee

Re: root .bash_profile?

By John Austin at 05/13/2019 - 08:49

On Mon, 2019-05-13 at 08:28 -0400, Bee.Lists wrote:
For the occasions when it is desirable NOT to cd /root

I have an alias in /root/.bashrc
alias sbp='source ~/.bash_profile'

ja@naxos ~ 2$ pwd
/home/ja
ja@naxos ~ 3$ su
Password:
root@naxos ja 1001$ pwd
/home/ja
root@naxos ja 1002$ sbp
[root@naxos:/home/ja]$ pwd
/home/ja

/root/.bash_profile
changes the colour of the prompt (among other things)
PS1="\[\033[1;31m\][\u@\h:\w]$\[\033[0m\] "

There must be much better ways of doing this!

Re: root .bash_profile?

By Bee.Lists at 05/13/2019 - 13:21

OK, I haven’t tested for that loadup yet, and the .bashrc is indeed there. I thought .bashrc was loaded first, then .bash_profile. This is for normal user.

Just tested it again, and /root/.bash_profile is not loading. Tried this in /root/.bashrc:

source /root/.bash_profile

That created a loop, because that reverse instruction is inside /root/.bash_profile

So for some reason, entering su isn’t loading its own .bash_profile nor keeping my user’s .bash_profile (expected).

Any ideas?

Cheers, Bee

Re: root .bash_profile?

By nux at 05/13/2019 - 08:38

Hi,

The $home of root is /root, just copy it there.

Re: root .bash_profile?

By Bee.Lists at 05/13/2019 - 13:06

Ah thank you. Having forgotten this, I already had all my aliases and instructions in there. For some reason they aren’t loading. If I do this, then everything loads:

source /root/.bash_profile

So there’s an indication this isn’t loading upon entry into su. Is this normal?

Cheers, Bee

Re: root .bash_profile?

By Christian, Mark at 05/13/2019 - 13:17

On Mon, 2019-05-13 at 13:06 -0400, Bee.Lists wrote:
$ man bash, search on INVOCATION

Re: root .bash_profile?

By Bee.Lists at 05/13/2019 - 13:39

$ man bash (INVOCATION)

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file
exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is
readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

But the reference to .bash_profile has some unclear restrictions or boundaries:

~/.bash_profile
The personal initialization file, executed for login shells

First, the ~ which might not apply to root. Second, it’s a “personal” init file, which also might not pertain to root. Going from user to root (su) might not initiate a login shell. I’m not clear on this.

But, .bash_profile is not loading.

I have my aliases in another file called /root/.bash_aliases, which is a duplicate of my /home/myuser/.bash_aliases which is NOW sourced in my /root/.bashrc so it now works.

So ya, got it to work, but knowing the cascade of inclusions is important. root is as important to me as my normal user.

Cheers, Bee

Re: root .bash_profile?

By Jonathan Billings at 05/13/2019 - 15:38

On Mon, May 13, 2019 at 01:39:27PM -0400, Bee.Lists wrote:
You might also benefit from reading the man page for 'su', which will
explain why running 'su -' is different from running 'su'.

Re: root .bash_profile?

By Pete Biggs at 05/13/2019 - 14:46

Why do you think that? '~' is just shell shorthand for user's home
directory.

root is just as much a user as anyone else, albeit one with special
privileges because they are UID 0.

Are you logging in? (i.e. typing the username and password at a login
prompt.) If not, then it's not a login shell.

Yes. If you are going to be playing around as root, then you really
should know the consequences of what you are doing. When I started
using Unix 30mumble years ago, the perceived wisdom was *always* invoke
su as /bin/su, those where the days when '.' was frequently in a users
path and some nasty user might leave scripts called 'su' lying around
waiting for an admin to occidentally execute them. Second, invoke it
as '/bin/su -', that way you clean out any user variables and only have
the environment you have setup for root.

P.

Re: root .bash_profile?

By Bee.Lists at 05/13/2019 - 16:20

root quite often isn’t recognized as a proper user. ~/.bash_profile isn’t loaded because it’s not a normal login shell when entering `su`.

So I can’t assume it’s just another user.

Isn’t moving from my own user using su, then prompted for password count as a login?

man su doesn’t apply to root with regards to the files loaded up upon login. Consequences of reading generic man pages result in more than one option.

Cheers, Bee

Re: root .bash_profile?

By Pete Biggs at 05/13/2019 - 17:25

On Mon, 2019-05-13 at 16:20 -0400, Bee.Lists wrote:
If you switch to any other user using 'su', then their .bash_profile
isn't loaded (unless you specify that it's a login shell). 'su'
doesn't mean "super user" it means "substitute user" - the default
happens to be user UID 0.

It may not be "just another user", but it *is* a user as much as your
login username is a user. You could assign your own username a UID of
0, and it would have the same privileges as 'root', but it would still
act as your username. NOTE: doing this is NOT recommended, do not do
it, seriously, do NOT do it.

No. It doesn't. The password prompt is for authentication, not logging
in.

Could you explain what you mean by that.

P.

Re: root .bash_profile?

By Bee.Lists at 05/14/2019 - 04:50

su does not load .bash_profile and therefore is a completely different application than with any other user. This one is different, considering .bash_profile is indeed used for logins for other users.

Cheers, Bee

Re: root .bash_profile?

By Pete Biggs at 05/14/2019 - 06:02

On Tue, 2019-05-14 at 04:50 -0400, Bee.Lists wrote:
su is an application for switching from one user to another. It behaves
the same way whether you are switching from user A to user B, or to
user root.

su is NOT the same as logging in with that user ID. If you login as
root at the console, root's .bash_profile would be read.

P.

Re: root .bash_profile?

By Bee.Lists at 05/14/2019 - 07:48

I can count those instances on one hand over the last 40 years. Hence the question of switching from another user. I covered that in the thread.

Cheers, Bee

Re: root .bash_profile?

By John Hodrien at 05/14/2019 - 05:10

You misunderstand. su behaves the same when switching to root as to any other account.

su -

is probably the command you're looking for.

jh

Re: root .bash_profile?

By Bee.Lists at 05/14/2019 - 05:19

OK I think you need to read previous posts on this.

I’m not looking for any other command.

Cheers, Bee

Re: root .bash_profile?

By Pete Biggs at 05/14/2019 - 06:06

On Tue, 2019-05-14 at 05:19 -0400, Bee.Lists wrote:
How are 'su' and 'su -' different commands?

If you really dislike typing the extra "<space> '-'", then setup an
alias so you only have to type "'s' 'u'" to get it to do what you want.

P.

Re: root .bash_profile?

By Bee.Lists at 05/14/2019 - 07:49

If you look, they are not the same. One has a parameter. My fix was to park the source into .bashrc and it now works.

Cheers, Bee

Re: root .bash_profile?

By John R. Dennison at 05/14/2019 - 05:50

On Tue, May 14, 2019 at 05:19:57AM -0400, Bee.Lists wrote:
Please stop top-posting, thank you.

It's the _same command_; all it is is a different invocation method
using an additional argument.

John

Re: root .bash_profile?

By Bee.Lists at 05/14/2019 - 07:45

I addressed this in the thread.

Cheers, Bee

Re: root .bash_profile?

By Jonathan Billings at 05/14/2019 - 08:14

On Tue, May 14, 2019 at 07:45:55AM -0400, Bee.Lists wrote:
And we continue to tell you that you're wrong. su behaves the same
way when switching to any other user as it does for root. Stop
spreading misinformation.

Re: root .bash_profile?

By Valeri Galtsev at 05/14/2019 - 10:13

On 2019-05-14 07:14, Jonathan Billings wrote:
Sorry, Jonathan, that I replying _your_ message, my reply has nothing to
do with it of any of your other posts, but rather with some posts by
some other posters. I really have to say this:

This whole thread - some posts in it that is - reminds me the old truth:
RTFM. Namely, Read The F.. (damn) Manual!

It is really hard to help those who don't care to read the man page. No
matter how many times you repeat that su stands for "substitute user",
and that the command as usually may have various options, and "-" is one
of these options, and what is the difference between invoking command
with or without it.

I would suggest that continuing this thread is counter-productive.

Valeri

PS pasting excerpt from "man su" on my FreeBSD workstation:

SU(1) FreeBSD General Commands Manual
SU(1)

NAME
su - substitute user identity

SYNOPSIS
su [-] [-c class] [-flms] [login [args]]

...

-l Simulate a full login. ...

- (no letter) The same as -l.

Re: root .bash_profile?

By Bee.Lists at 05/14/2019 - 10:23

Then reply to them. Is it that hard?

Second, if you READ the posts, you would find that the man page, is unclear. It was referred to, but you missed that as well.

It’s really hard to post things when people like you two don’t read the frickin posts. No matter how many times you post, most people just want to fluff their feathers instead of read the posts.

I would suggest…reading the posts. Then, read the posts. Or get some fresh air. None of this is difficult.

Cheers, Bee

Re: root .bash_profile?

By John R. Dennison at 05/14/2019 - 11:24

On Tue, May 14, 2019 at 10:23:20AM -0400, Bee.Lists wrote:
No, it's really not. This is reinforced by the fact that you are the sole
person having an issue grasping how this all works and would rather
argue your incorrect point of view (and getting the last word in) than
accept that you might be wrong and others might be right. The people
that have chimed in have a combined experience of _well_ over a hundred
years. Might just be they know a thing or 10?

If you find the man page confusing for whatever reason there is a SEE
ALSO section that points you to the GNU info page for it, including the
exact command line necessary to see the invocation specifics; GNU info
pages are generally far more fleshed out than man pages. You might need
to install the info package but it may be worth it for the additional
information source.

This argumentative nature you are displaying is not going to make people
want to help you further.

I would encourage you to do the same with a bit more open mind and the
understanding that perhaps, just maybe, when you come to a support list
asking for assistance that the assistance you get might actually be,
shocking as it may seem, correct.

John

Re: root .bash_profile?

By Bee.Lists at 05/14/2019 - 10:07

Not big on reading what I put. It’s all there. Regardless how often you say ‘su’ is the same as ‘su fred’, it is not. Stop spreading misinformation.

Cheers, Bee

Re: root .bash_profile?

By Valeri Galtsev at 05/14/2019 - 10:23

On 2019-05-14 09:07, Bee.Lists wrote:
Look, in the following four command lines executed in the shell:

su

su fred

su - fred

su -l fred

- in all four of them:

"su" is a command

"fred" is an argument (wherever it is present)

"-" (surrounded by spaces on both sides) and "-l" are command options

This is standard terminology used in UNIX, Linux, etc for several
decades. And finally, RTFM, please.

And also, can we close this thread, please.

Valeri

Re: root .bash_profile?

By Bee.Lists at 05/14/2019 - 10:26

Just wow. No point in even posting in here anymore.

Cheers, Bee

Re: root .bash_profile?

By Johnny Hughes v... at 05/13/2019 - 17:55

On 13/05/2019 22:25, Pete Biggs wrote:
Shame that "security experts" regularly recommend using another name for
the root account - security through obscurity anyone?

Re: root .bash_profile?

By Pete Biggs at 05/13/2019 - 18:19

Unfortunately anyone can call themselves an "expert".

If your protection against a UID 0 login is to change the username,
then you need to seriously look at (a) your password policy and (b)
letting root login in the first place.

P.

Re: root .bash_profile?

By Valeri Galtsev at 05/13/2019 - 18:01

On 2019-05-13 16:55, J Martin Rushton via CentOS wrote:
Not here, sorry ;-)

Valeri

PS Script kiddies will get UID=0, not username=root, when succeed in
pretty much anything they use to elevate privileges on attacked machine.

Re: root .bash_profile?

By Valeri Galtsev at 05/13/2019 - 17:39

On 2019-05-13 16:25, Pete Biggs wrote:
Whole environment of user that invoked su is "inherited" unless it is
specified on commend line to get login environment of user one does su into.

Thanks Pete! This really needs to be repeated, even though whenever su
is mentioned I repeat that to my users that it means "substitute user",
but now that you mentioned it here it makes this whole thread clear.

Valeri

Re: root .bash_profile?

By Jonathan Billings at 05/13/2019 - 16:37

On Mon, May 13, 2019 at 04:20:17PM -0400, Bee.Lists wrote:
You'd find that if there's another user account on your system (lets
call it "fred"), and you ran 'su fred', it would not load
~fred/.bash_profile either, because it wasn't a login shell. 'root'
isn't special here.

As you read in the 'bash' man page, files that are loaded with a
"login shell" are different from files loaded from a normal shell
execution. 'su' has specific syntax for either running a shell as a
user or running a login shell as a user. Just typing a password at
the prompt doesn't mean it was a login shell.

Typically, a 'login shell' is what you get when you log into a system
through login: or a graphical login. .bash_profile is loaded once
when you log in. It's meant for login stuff. .bashrc is loaded for
every shell you start.

'su' is a tool used to switch users, so you are given the option to
either start a shell as a user or log in as that user, depending on
the situation.

The 'su' man page explains all of this pretty well I think. Becoming
root isn't special, in terms of loading .bash_profile vs. .bashrc.

Re: root .bash_profile?

By Bee.Lists at 05/14/2019 - 04:49

But it is different. Significantly different.

I’d rather just use ‘su’ and have the login sequence trip the loading of my aliases. Moving this to .bashrc has solved that. That’s what I was asking.

Cheers, Bee

Re: root .bash_profile?

By Valeri Galtsev at 05/13/2019 - 12:46

On 2019-05-13 07:38, Nux! wrote:
It is $HOME not $home ;-)

Valeri