share
Unix & LinuxIs it safe to copy user's .bashrc to /root/.bashrc?
[+3] [3] sgon00
[2020-04-18 12:54:14]
[ bash debian environment-variables root ]
[ https://unix.stackexchange.com/questions/580904/is-it-safe-to-copy-users-bashrc-to-root-bashrc ]

I am running Debian. The default /root/.bashrc is kinda empty. So if I do sudo -i to login as root, there are many things are not setup properly. Normally, I just do the following:

$ sudo -i
# mv .bashrc .bashrc.bak
# ln -s /home/user/.bashrc . # or cp /home/user/.bashrc .

Note: the /home/user/.bashrc is the default one created by debian useradd without any custom modification.

So Now, I just want to clarify if this is safe to do. I am just lazy to create another .bashrc file for root.

It's generally a bad idea to work in an interactive root shell, so why set it up? Related: duckduckgo.com/… - Kusalananda
@Kusalananda there are many directory are not viewable by users. I don't want to change their permission behaviors. so to make life easier than guessing sudo ls dir, sudo ls dir/dir/ everytime. It's easier to use sudo -i and ls. that's all. The only reason I use root user is to make ls easier. Cheers. - sgon00
[+1] [2020-04-18 15:26:21] Chris Davies

By doing this you are effectively making the root account only as secure as your own "user" account. This means that any browser-based trojan or other successful attack on your account is implicitly able to gain root access too.

Is this acceptable to you?

If not, don't symlink the files; copy it once you have verified the content is what you expect.


1
[+1] [2020-04-18 16:02:45] Arkadiusz Drabczyk

I don't think it's a good idea generally, not only in Debian, for at least 2 reasons.

First, you should be very very careful and apart from copying user's .bashrc you should also copy all other files that are sourced by it which might referred to as ~/.bash_aliases etc. or you risk some errors and possible undefined behavior. For example, what if, for whatever reasons, you have this in your .bashrc or one of the scripts sourced by it:

. FILE2

echo "$var1"
echo rm -fr "$var1"/*

When root runs its ~/.bashrc and FILE2 is missing then it would start removing the entire filesystem.

Second, you shouldn't work as root for too long because it's very risky. And you should be always immediately able to say if you're root or a regular user in the current interactive subshell. It's most commonly done by changing the final character of prompt to # when running as root [1] so at least PS1 setting should be different between root and non-root .bashrc.

[1] https://unix.stackexchange.com/questions/291729/why-is-the-default-symbol-for-a-user-shell-and-the-default-symbol-for-a-root

2
[+1] [2023-06-15 08:18:22] archygriswald

So effectively you are using only one .bashrc for root and normal users.
Is this safe (for the root account), you ask?
Well, take a look at your ~/.bashrc if there is code inside that might harm root, if so apply it only to normal users by enclosing this code with e.g. if [ "$(id -u)" -ne 0 ]; then ... .
Then do the same the other way 'round: if something is in original /root/.bashrc that must be set for root but not for normal users, copy this code and enclose it with [ "$(id -u)" -eq 0 ].

This way you could have one .bashrc that fits them all.


3