Lets assume i am normal user, the i will switch to root:
user ~ $ su - root
Password:
root ~ #
So once i logged in as root, i want to run following command automatically:
source .bash_profile
How can i have that above command run automatically please?
According to the bash man page, .bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.
In your case, you don't need to source .bash_profile like this.
You just need to put source .bash_profile in your root's .bashrc file
if [ -f ~/.bash_profile ]; then
source ~/.bash_profile
fi
Read me for better understanding of .bash_profile and .bashrc
Update
Example:
[root#sgeorge-ld ~]# cat .bashrc | tail -1
echo "Testing .bashrc for a stack query"
[root#sgeorge-ld ~]# exit
logout
[sgeorge#sgeorge-ld ~]$ su - root
Password:
Testing .bashrc for a stack query
[root#sgeorge-ld ~]#
First of all, when you switch to root user, you will be still your regular user's home directory. Which .bash_profile you want to execute? /Users/myuser/.bash_profile or root's /var/root/.bash_profile?
Regardless of what you would like to execute, you can edit /var/root/.bashrc (if you don't have it, create one) and add your command there.
Related
I have created a bash_profile in centos
#vi /home/hadoop/.bash_profile
as follow:
# .bash_profile
# User specific environment and startup programs
export j=1
However, when I do
[root#hadoop1 ~]# su hadoop
[hadoop#hadoop1 root]$ echo $j
[hadoop#hadoop1 root]$
I suppose my .bash_prfile shoud run for each su login?
The .bash_profile won't load unless you use one of the -, -l, or --login options or login directly with that user. Checkout this link is from the Unix & Linux stack exchange. You can also run man su in the shell to see all the available options.
I'm very confused about how my shell is reading bash_profile.
In root, my ~/.bash_profile looks like so
# .bash_profile
# Get the aliases and functions
if [-f ~/.bashrc ]; then
.~/.bashrc
fi
PATH=$PATH:$HOME/bin:$HOME/sbin:$HOME/usr/sbin:$HOME/usr/bin:/usr/sbin
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export PATH=$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
unset USERNAME
There is no ~/.profile file.
In a user called maruhan, my ~/.bash_profile looks like so
# .bash_profile
# Get the aliases and functions
if [-f ~/.bashrc ]; then
.~/.bashrc
fi
PATH=$PATH:$HOME/bin:$HOME/sbin:$HOME/usr/sbin:$HOME/usr/bin:/usr/sbin
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/maruhan/Desktop/issac:/usr/local/lib
ASDF=$ASDF:/home
export PATH=$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
export ASDF=$ASDF
unset USERNAME
And my ~/.profile looks like so
LD_LIBRARY_PATH=/home/maruhan/Desktop/issac:/usr/local/lib:$LD_LIBRARY_PATH
ASDF=/home:$ASDF
export ASDF=$ASDF
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
You can clearly see that ASDF is not defined in root's bash_profile.
However when I call export, I get this in root.
declare -x ASDF=":/home"
but nothing about LD_LIBRARY_PATH.
Strangely in maruhan, running export shows both ASDF and LD_LIBRARY_PATH.
Also, nothing about ASDF or LD_LIBRARY_PATH exist in /etc/environment. I also don't have a /etc/bash_profile file.
Running echo $0 gives me bash for both root and maruhan.
How come LD_LIBRARY_PATH disappeared in root while ASDF is there?
The rules are a bit complicated. According to bash's man page:
INVOCATION
A login shell is one whose first character of argument zero is a -, or one
started with the --login option.
An interactive shell is one started without non-option arguments (unless -s is
specified) and without the -c option whose standard input and error are both
connected to terminals (as determined by isatty(3)), or one started with the -i
option. PS1 is set and $- includes i if bash is interactive, allowing a shell
script or a startup file to test this state.
... ...
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.
... ...
When an interactive shell that is not a login shell is started, bash reads and
executes commands from ~/.bashrc, if that file exists. This may be inhibited by
using the --norc option. The --rcfile file option will force bash to read and
execute commands from file instead of ~/.bashrc.
... ...
Note that on some systems bash may be customized so that it would also execute a system wide rc file (e.g. /etc/bash.bashrc) before sourcing ~/.bashrc for an interactive shell that's not a login shell.
Shells started by a login mechanism (usually with a username/password prompt, like console login, telnet, ssh, ...) are usually login shells. For a login shell, $0 is usually -bash.
[local] % ssh user#host <-- The user is trying to login
Password: P#ssw0rd
[remote] % echo $0
-bash <-- This is a login shell
[remote] % bash <-- This is not a login (no username/password)
[remote] % echo $0
bash <-- Not a login shell
[remote] %
To make life easier I would put all rc things in ~/.bashrc and source ~/.bashrc in ~/.bash_profile. For example:
% cat ~/.bash_profile
[[ -f ~/.bashrc ]] && source ~/.bashrc
% cat ~/.bashrc
# return immediately if not in an interactive shell
[[ $- != *i* ]] && return 0
export FOO=bar
PATH=$PATH:/my/path
%
If I enter sudo su - Rorschach and login as Rorscach. I would like a command to be executed automatically.
Such as echo "Hello Rorschach" or cd ~
Where do I put these scripts in order for them to be performed upon login of that user?
I am using an ubuntu 14.04 command line and echo $0 outputs -bash
~/.bash_profile (recommended) or ~/.bashrc (will work, but this file should rather contain functions and aliases, not commands).
As far as message after login is concerned you can write at /etc/motd file which I think is the recommended file.
/etc/motd is not a script but a text file which contents are shown before the first prompt of a login session.
motd: message of the day
as I read the bash shell should call /home/myUser/.login each time myUser logs in.
That is where I want to set and export some variables:
Within /home/myUser/.login:
. /etc/login
... and some user specific variables later.
echo .login done.
Within /etc/login some common variable settings like:
export JBOSS_HOME=/home/ec2-user/sw/wildfly-8.2.0.Final
echo /etc/login done.
None of the both echos is displayed.
But
. /home/myUser/.login
works.
drwx--x--x 6 myUser myUser 4096 Jan 15 18:42 myUser
Because in useradd -m ... the -m is ignored,
and useradd -p hallo ... does not hash the password (but passwd --stdin does),
I wonder if I am the one doing the mistakes ...
welcome to SO!
The bash shell reads from several files during an interactive login session. .login is not one of them.
From the INVOCATION section of the bash man page
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.
Hope this answers your question.
From man bash (RHEL 6 and RHEL 7):
~/.bash_profile
The personal initialization file, executed for login shells
~/.bashrc
The individual per-interactive-shell startup file
I'm Mac user.
I want to set PYTHONPATH env for root. so
$ sudo su -
# vi ~/.profile
and add to file 'export PYTHONPATH=/mypythonlib'
then
# env
I can see this line
PYTHONPATH=/Users/simpnet2/projects/meiji/src/hershey
but..
when I use sudo command, cannot find that
$ sudo env
.. there's no PYTHONPATH
My program has to run with sudo command and needs PYTHONPATH.
If you use sh try /etc/profile, bash try /etc/bashrc and if you use zsh try /etc/zshenv.
You can make PYTHONPATH visible to sudo be editing your sudoers file. Notice you should ONLY do this through visudo as explained here.
You should try sudo -i which will simulate logging in as root and source the ~root/.profile.
As of 10.8.5, putting my environment statements in the .profile path in the home of the root user (/var/root) worked. after quitting bash and coming back to the root user prompt with 'su -', I could see my new path, etc. with the 'env' command and my MacPorts installationw orking correctly.
MacBook-Pro:~ root# cat /var/root/.profile
export MANPATH=/opt/local/share/man:$MANPATH
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
MacBook-Pro:~ root# which port
/opt/local/bin/port
Well, in other Linux system, it is also right that 'sudo' does not use local environment variable. But you can declare the temporary environment variable along with 'sudo' command.
For example, in your case, you can add 'PYTHONPATH=/mypythonlib' in your command 'sudo env', and the final command is:
sudo PYTHONPATH=/mypythonlib env
You can also read this article: Using sudo. You can see how 'sudo' keep or ignore user-defined environment variables.
In the case of logging in as a normal user and invoking "su - root" I found that Mac OS 10.8.5's bash was ignoring .profile and .bash_profile; I was unable to change root's $PATH by editing those files. What did work was editing /etc/paths. After exiting the root shell and entering again with "su - root" the new path was present.