Get name of current user - linux

What is a cross-platform way of getting the username of the current user in R? I am currently using
system('whoami', intern=TRUE)
However this assumes that the user has shell access, and that the whoami program is available. Is there a more native to get this information in R?

I would do this :
Sys.getenv("USERNAME") ## works under windows
or better more robust:
Sys.info()[["user"]]
But under unix-like system the result is sometimes different of system('whoami', intern=TRUE) :
whoami outputs the username that the user is working under, whereas
$USER outputs the username that was used to login.
For example, if the user logged in as John and su into root, whoami displays root and echo
$USER displays John. This is because the su command does not invoke a
login shell by default.

Related

How to check user inside bash script and run as another user

I always want to run a script as user1, but the code flow always runs it as root. So, I want to check the current user inside the script. If it is root, I want to run the rest of the script as user1.
I want to do something similar to this. But only difference is I need to run the script as a particular user (say user1). So, when I find that the current user is not user1, I do not want to exit but want to run rest of the shell script as user1.
How can I achieve this?
Thanks #User123. But I was facing some issues with that solution as there are many functions and variables in the script. Not sure what the problem exactly is. But, this worked for me now:
if [ $USER == <undesired user> ]; then
echo Current user is $USER. Running the script as <desired user>
su -c 'sh <script.sh>' <desired user>
else
echo Current user is $USER
...
<rest of the script>
...
fi

'su' by using 'script' in Docker returns different results compared to the standard environment

I need to request certain commands via su including password in one line.
I found a solution and it is working in a standard environment (Ubuntu) (more about solution here):
{ sleep 1; echo password; } | script -qc 'su -l user -c id' /dev/null | tail -n +2
But I am faced with the problem that this solution is not suitable in a Docker container environment
Script terminates the command without waiting for echo and as a result i get:
su: Authentication failure
Any help is much appreciated.
Passing the password for su via stdin is problematic for various reasons: the biggest one is probably that your password will end up in the history.
You could instead:
Call the entire script as the specific user and thus enter the password manually
Use sudo with the appropriate NOPASSWD sudoers configuration
In your case you are using docker, so you could just set the USER in your Dockerfile

Run a script from root and it calls another script that was in the sudo oracle. how to do that without asking the password of the oracle

I'm logging into a linux machine with root and after login i have used su - oracle to connect my database. Now I've 2 shell scripts one at root home and one at home/oracle. In the home/oracle I've wrote a script for taking the backup of the database. The script available in the root is nohup ssh oracle#onprem-svcdb /home/oracle/test.sh while running the script its asking the password of the oracle, I don't need it to be like this while running the scripts It doesn't need to ask the password and it needs to run the script in oracle. What I need to do for that??? Can anyone help for this
If I understand corrently, you are getting a password prompt on using a script which connects to your database and executes something. If you dont need a password prompt , you would need to generate public and private keys for ssh for the logged in user , in your linux machine and get it configured in the database. Please have a look at the below link
http://docs.oracle.com/cd/E19253-01/816-4557/sshuser-33/index.html
You can try the below
Let this be you env variables.
---------VARIABLES --------------
export APP_USER=something
export APP_PASS=somepass
export APP_SID=sid
Here is the script with a execute permission.
--------------SCRIPT TO RUN SQL----------
#!/usr/ksh
sqlplus << END_OF_SQL
$APP_USER/$APP_PASS#APP_SID
select * from dual;
END_OF_SQL
exit $?
----------END SCRIPT----------
Source : https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:142212348066
you could try expect tool:
You will start expect script below, that will start your main sql-script in return and will send oracle password if prompted:
content of /tmp/expect.exp script:
#!/usr/bin/expect -f
# set Variables
set password '***'
set timeout -1
# what to execute
spawn /usr/bin/su oracle -c "/tmp/main_script.sh"
match_max 100000
# Look for passwod prompt
while {1 > 0 } {
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
}
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
than your main script will be stored in (or root home, whatever dir you need):
/tmp/main_script.sh content:
su - oracle
drop table; create table; other SQL commands
One disadvantage is - plain text password, stored inside the script
How to install: http://www.cyberciti.biz/tips/execute-commands-on-multiple-hosts-using-expect-tool-part-iii.html
Or you could try to modify visudo , like :
user1 ALL=(user2) NOPASSWD: /bin/bash
where : user1 will be granted "su to user2" without password prompt. You can replace also /bin/bash by ALL and then you could launch any command as user2

Difference between EUID and UID?

EUID is not the same as UID. At what context are these both are used in the script?
I tried to get the values by echo "UID is $UID and EUID is $EUID", but only space came as output. My machine runs Ubuntu 12.04 LTS. Seen at some sites that this is usually used to check whether it is root user and all but not able to get proper difference.
They're different when a program is running set-uid. Effective UID is the user you changed to, UID is the original user.
UID is the ID of the user that executed the program.
EUID (Effective UID) is the user ID the process is executing. Usually both are equal, unless using a program with SetUID to for example increase your privileges. A common case where UID and EUID are different would be executing sudo.
EUID and UID variables only work on bash, not in dash (in Debian based distros as Ubuntu sh is usually a symlink to dash).
If you are running the script interactively you might not have bash configured as your default shell, run bash before trying.
If you are running it from console:
bash script.sh
If you are running it using its path (for example ./script.sh) ensure the first line of the script is:
#!/bin/bash
And not:
#!/bin/sh
For a more generic way to do it -that works on any shell- check: https://askubuntu.com/questions/15853/how-can-a-script-check-if-its-being-run-as-root
In that post the command id is mentioned, where:
id -u # is the EUID
id -u -r # is the UID

can we write a simple bash script to automatically login as root user

I usually have to login in 20 to 50 times daily as a super user, typing the long password again and again..
i have just created a simple bash script
#!/bin/bash
sudo -s
echo password
./test
output root#localhost:
password
when i execute it, it works like charm... but it shows my password on the screen.....
do some one have any other best solution...... for this small problem.......
i hope this is not all the solution in security standard...... can we have any other solution with out exposing my password.....
You can pipe the echo'd password into a command. Try something like this:
echo myPassword | sudo -S
You can see come more info on this here.
Question is, do you REALLY want your password in a shell script file? (just emphasizing that its a terrible idea)
Is there a reason that you can't sudo su - to just become the root user instead of prepending all of your commands with sudo blah?
just change ownership of the script to root & set SUID-Bit in user the permissions
chmod u=rws g+x o+x script123
the script will run as root for every user
You can configure sudo to require a password only every so many minutes. The default is 5 minutes. Read the sudoers man page and scroll down to this:
timestamp_timeout
Number of minutes that can elapse before sudo will ask
for a passwd again. The timeout may include a
fractional component if minute granularity is
insufficient, for example 2.5. The default is 5. Set
this to 0 to always prompt for a password. If set to a
value less than 0 the user's timestamp will never
expire. This can be used to allow users to create or
delete their own timestamps via sudo -v and sudo -k
respectively.
simple solution is to use key base authentication
Use ssh-copy-id instead following from this tutorial which is secure

Resources