bash: fail if script is not being run by root - linux

I have a bash script that installs some software. I want to fail as soon as possible if it is not being run by root. How can I do that?

#!/bin/bash
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
Source: http://www.cyberciti.biz/tips/shell-root-user-check-script.html

After digging around on this, the consensus seems to be that there is no need to use id -u in bash, as the EUID (effective user id) variable will be set. As opposed to UID, the EUID will be 0 when the user is root or using sudo. Apparently, this is around 100 times faster than running id -u:
#!/bin/bash
if (( EUID != 0 )); then
echo "You must be root to do this." 1>&2
exit 1
fi
Source: https://askubuntu.com/questions/30148/how-can-i-determine-whether-a-shellscript-runs-as-root-or-not

Related

if with exit 0 & 1 and showing process

I want to solve a certain task with a bash script which I pass to a parameter, but unfortunately I don't get anywhere.
This is what it is about.
If a user exists, the exit status 0 should be returned and displayed. If this user does not exist then 1. This works fine so far. Now it is so that if the user should exist and has running processes, only the processes should be displayed. Otherwise only the exit status 0 if the user exists and has no running processes.
#!/bin/bash
user=$1
exists=$(grep -c $user /etc/passwd)
if [ "$exists" -ne 0 ]; then
echo $?
else
echo $?
fi
I added an additional elif, but that did not work. How can I customize the script to show me the running processes for an existing user if they should exist or if no running processes exist but the user exists only returns and displays the status 0 or 1?
Thanks a lot!
The id command can tell if user exists, without need to tap the passwd file, and works regardless of what resource type is providing the users database to the system.
#!/bin/sh
if id --user "$1" > /dev/null 2>&1;
then
ps --user "$1" --format pid=,comm= || :
else
false
fi
Or a one-liner:
sh -c 'id -u "$1">/dev/null 2>&1&&{ ps -u "$1" -o pid=,comm=||:;}' sh username
grep returns the exit status you are looking for, so you probably just want:
#!/bin/bash
user=$1
if grep -q "$user" /etc/passwd; then
echo 0;
exit 0;
else
echo 1;
exit 1;
fi
But that's quite redundant and could be more easily written:
grep -q "$user" /etc/passwd
rv=$?
echo "$rv"
exit "$rv"
However, the requirement to print the return status is odd, and if you simply drop that, your script can just be the grep.
Note that this completely disregards that fact that grepping /etc/password for the user name is not an adequate test to determine if the user exists, but that does not seem to be the heart of this question.

For each parameters, script checks if there exists a user of such username. If exists script prints all processes run by this user

#!/bin/bash
if [ $# -ne 1 ]
then
echo "Script Should Have Atleast 1 Parameter"
exit
fi
USER=$1
echo $USER
if [ $? -eq 0 ]
then
echo " Yes the User Exists"
else
echo "No , The User Doesnt Exists"
fi
My problem here is whatever the input i give, it shows yes the user exists.
And can anyone suggest me a command to print out all the process runs by this user
Should i use awk , or grep
Well, first of all, you are not checking if user exists, but ultimately if command echo $USER didn't fail. You will need to implement proper user check.
Good way how to check if arbitrary user exists would be something like this, using command id.
if id "${1}" &> /dev/null; then
echo 'user found'
else
echo 'user not found'
fi
Next you want to list all processes belonging to this user. You will need ps. There are many ways how ps can format output. Be sure to check manual page of ps. Here is example which lists command and pid of all processes belonging to user in "$1".
ps -eo pid,comm --user $(id -u "${1}")
At last, running check using id and only then using ps for process list is inefficient at least. In your use-case, you can simply run ps and check if it was successful. If not, you can show warning that user was not found.
#!/bin/bash
if ! ps -u "$1" 2> /dev/null; then
echo 'user not exists!'
exit 1
fi

BASH: Check if user is root even when using fakeroot [duplicate]

This question already has answers here:
How to check if running as root in a bash script
(21 answers)
Closed 24 days ago.
How can I check whether a user is root or not within a BASH script?
I know I can use
[[ $UID -eq 0 ]] || echo "Not root"
or
[[ $EUID -eq 0 ]] || echo "Not root"
but if the script was invoked via fakeroot, UID and EUID are both 0 (of course, as fakeroot fakes root privileges).
But is there any way to check whether the user is root? Without trying to do something only root can do (i.e. creating a file in /)?
Fakeroot sets custom LD_LIBRARY_PATH that contains paths to libfakeroot. For example:
/usr/lib/x86_64-linux-gnu/libfakeroot:/usr/lib64/libfakeroot:/usr/lib32/libfakeroot
You can use this to detect if application is running inside the fakeroot iterating by paths and looking for libfakeroot.
Sample code:
IS_FAKEROOT=false
for path in ${LD_LIBRARY_PATH//:/ }; do
if [[ "$path" == *libfakeroot ]]; then
IS_FAKEROOT=true
break
fi
done
echo "$IS_FAKEROOT"
Here, The below script will find the user is root or not.
#!/bin/bash
touch /checkroot 2>/dev/null
uid=`stat -c "%u" /checkroot 2>/dev/null`
if [ "$uid" = "0" ]
then
echo "Root user"
else
echo "Not a root user"
fi
rm /checkroot 2>/dev/null
In the above example, I will try to create a file in the root directory, if I am not a root user and I don't have a permission it will give error, I was redirect that error to the /dev/null.
If the user have the permission, the file will be created. Then using the stat to get the user id for that file, and store that into the variable uid.
Using that variable in the if condition I will check.
If the temporary file will be created the rm command will remove that file.
But make sure the file is not already exist in the root directory.

How to check in dash for root

The standard-solution for bash, see:
https://askubuntu.com/questions/15853/how-can-a-script-check-if-its-being-run-as-root
which is:
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
does not work in dash, which is slowly becoming the standard-shell under Linux. How can the above be ported to dash?
Use id:
if [ "$(id -u)" -eq 0 ]; then
echo "I am root!"
fi
Or
#!/bin/sh
if [ "$(id -u)" -ne 0 ]; then
echo "This script must be run as root"
exit 1
fi
What I usually do is check for the capabilities I actually require, so that the script will also work correctly for a user who likes to run via an alternate privileged account (*BSD used to have toor for superuser with csh, which of course nobody in their right mind would want these days, but anyway).
test -w /usr/share/bin ||
{ echo "$0: /usr/share/bin not writable -- aborting" >&2; exit 1 }
Use the "id -u" command to get your current effective user id:
#!/bin/dash
MYUID=`id -u`
if [ "$MYUID" -eq 0 ]
then
echo "You are root"
else
echo "You are the non-root user with uid $MYUID"
fi

Shell script continues to run even after exit command

My shell script is as shown below:
#!/bin/bash
# Make sure only root can run our script
[ $EUID -ne 0 ] && (echo "This script must be run as root" 1>&2) || (exit 1)
# other script continues here...
When I run above script with non-root user, it prints message "This script..." but it doe not exit there, it continues with the remaining script. What am I doing wrong?
Note: I don't want to use if condition.
You're running echo and exit in subshells. The exit call will only leave that subshell, which is a bit pointless.
Try with:
#! /bin/sh
if [ $EUID -ne 0 ] ; then
echo "This script must be run as root" 1>&2
exit 1
fi
echo hello
If for some reason you don't want an if condition, just use:
#! /bin/sh
[ $EUID -ne 0 ] && echo "This script must be run as root" 1>&2 && exit 1
echo hello
Note: no () and fixed boolean condition. Warning: if echo fails, that test will also fail to exit. The if version is safer (and more readable, easier to maintain IMO).
I think you need && rather than||, since you want to echo and exit (not echo or exit).
In addition (exit 1) will run a sub-shell that exits rather than exiting your current shell.
The following script shows what you need:
#!/bin/bash
[ $1 -ne 0 ] && (echo "This script must be run as root." 1>&2) && exit 1
echo Continuing...
Running this with ./myscript 0 gives you:
Continuing...
while ./myscript 1 gives you:
This script must be run as root.
I believe that's what you were looking for.
I would write that as:
(( $EUID != 0 )) && { echo "This script must be run as root" 1>&2; exit 1; }
Using { } for grouping, which executes in the current shell. Note that the spaces around the braces and the ending semi-colon are required.

Resources