.bash_profile doest run on su user - bash

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.

Related

run inline shell script as root

I have a user, who's a passwordless sudoer. I need to execute shell script file with him, and make him execute one block as sudo. E.g:
su root <<"AS_ROOT"
# do something with my linux
AS_ROOT
But nothing works. I tried:
su - root <<...
sudo -s -- <<...
It barks back at me. I'm on ubuntu 16.04 lts.
Thank you.
As Cyrus points out, su is a different utility to which sudo's configuration doesn't apply.
It sounds like you're looking for something like this:
sudo -s <<'AS_ROOT'
echo "Hi from $USER."
AS_ROOT
This should output Hi from root.
Note that -s is needed to tell sudo to create a shell in order to interpret the commands passed via stdin (the here-doc). That shell is the current user's default shell, as reflected in environment variable $SHELL.

Alternative to export variable in non interactive shell

I have a requirement wherein a script needs to be executed which requires a few environment variables to be set. Typically the following is required:
1) I login to target node, say T1 using 'user1' and set my environment variable, say TEMPDIR=~/tmp; Do a source ~/.bashrc and logoff.
2) Now I'm required to do a non interactive shell script execution. This is
Login to a admin server and run the command:
ssh user1#T1 "sudo -u user2 /path/filescript"
The file script that I execute requires the TEMPDIR to be set.
The above setup doesn't work, as the bash shell is not sourced during non interactive shell (if my understanding is correct). To overcome this, I have tried the below suggestions available in this site and other, but without any luck:
ssh user1#T1 "source ~/.bashrc; sudo -u user2 /path/filescript"
ssh user1#T1 "sudo -u user2 env -i /path/filescript"
Please help. Thanks.
Note: I'm trying to get this done without any intervention from root (I mean using root to set / unset any variables.
I'm not sure step 1 is necessary in any way:
ssh user1#T1 sudo -u user2 env -i TEMPDIR=/home/user2/tmp /path/filescript"

Execute command if su to a certain user

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

Identify the shell running in my system

Can anyone please tell me what shell has installed in my system?
Because when I am logging into my system using my username it is initially showing bash shell but later it is showing korn shell after doing sudo.
Please see below for details.
-bash-3.2$ pwd
/home/w4x2spxt
-bash-3.2$ echo $SHELL
/bin/bash
-bash-3.2$ su - XXXXXXX
Password:
You have new mail.
The Oracle base remains unchanged with value /apps/oracle
abc0300ab123:/a30/home/XXXXXXX >> echo $SHELL
/bin/ksh
SHELL environment variable gives you your login shell.
check the shell path mentioned(last column) against your username or XXXXXXX(for su) in /etc/passwd like this: grep ^XXXXXXX /etc/passwd.
The shell mentioned in that file will be your default shell when you login or su to that user.
To check all installed shells on your system use this: cat /etc/shells
Your user is using /bin/bash as default shell.
root is using /bin/ksh as its default shell.
The default shell is a user-specific setting, so there is nothing picky in having different ones among users. Just check the last column in /etc/passwd and surprise yourself with a variety of values.
Note by the way that when you do su, you log in as root. If you add the dash and say su - you are loading the root profile, so that you have its environment.
The environment variable SHELL always contains the login shell of the user logged in, defined in /etc/passwd.
If the user changes his/her shell after login by e.g. exec bash (bash), the SHELL will still expand to the login shell.
In your case, the user XXXXXXX has the login shell /bin/ksh, do:
grep '<user_name>' /etc/passwd
to match the results.
To find the current shell:
echo $0
Or
ps -p $$
When you do this:
-bash-3.2$ su - XXXXXXX
you are starting whatever shell is assigned to user XXXXXXX. This is usually a good thing for su -, since that runs their shell as a login process so you get their normal shell startup initialization (profile, *shrc, etc). If you run a different shell from the one their account is set up for, you probably miss out on all their customization.
You can see what shell is associated with an account by looking them up in the password database. This is pretty reliable across different types of systems and authentication schemes:
perl -MUser::pwent -le 'print( (getpwnam "XXXXXXX")->shell || "/bin/sh" )'
You can always run a shell explicitly as the other user if you have one in mind that you want:
su XXXXXXX -c "bash --login"
or
sudo -u XXXXXXX bash --login # if you have sudo privs
To see what shell you're currently running, look at $0:
echo $0
To see what shell you get by default as you, look at $SHELL.

MacOSX how to autorun a script after su - root?

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.

Resources