Hot answers tagged

22

The only way I could think of is to check one of the SUDO_* environment variables set by sudo: #!/usr/bin/env sh if [ "$(id -u)" -eq 0 ] then if [ -n "$SUDO_USER" ] then printf "This script has to run as root (not sudo)\n" >&2 exit 1 fi printf "OK, script run as root (not sudo)\n"...


19

Another option would be to check if the grandparent process name is "sudo": #!/bin/sh if [ "$(id -u)" -eq 0 ] then if [ $(ps -o comm= -p $(ps -o ppid= -p $$)) = "sudo" ] then echo Running under sudo else echo Running as root and not via sudo fi else echo Not running as root fi


13

The information about which user logged in is available in /proc/self/loginuid. EDIT due to comments: That file does not seem to exist on all systems. I tested and it is available on Centos 6, Fedora 32, Fedora 33 and Ubuntu 20.04, all in standard x86_64 setups. If we login as our user and than use sudo or su to become root, this will not change /proc/self/...


6

Avoid sudo in root bash script? Preamble: Care sharing root account!! Unfortunely, there is no resistant way... Please read carefuly upto last paragraph Once you give root access to someone, they could do anything, including editing your script!! For sample, if user hit sudo su -, then variables SUDO_* doesn't exist anymore... First quick way using pstree So ...


4

I suggest checking out process list strings and see if the user is running the program using sudo contype=`tty | cut -d '/' -f 3` tty="$contype/`tty | cut -d '/' -f 4`" if [ "$(id -u)" -eq 0 ] then res=`ps ax | grep "$tty" | grep "$0" | grep "sudo"` if [ $? == 0 ] then echo "You ...


4

if [ "$(id -u)" -ne 0 ] || [ "$(logname)" != root ]; then echo >&2 "You either don't have superuser privilege or didn't login as root" exit 1 fi Not foolproof as any process with superuser privilege that can run any command can do anything to work around anything, but may be close to what the teacher's expecting and ...


3

Hitting the same issue on Fedora 33. This seems due to having an alias defined for sudo in my environment: $ alias sudo alias sudo='\sudo ' Due to this, somehow bash resolves aliases passed as arguments to sudo alias, as shown in the example below: $ alias foo='echo foo' $ sudo foo foo I would have expected to have this instead: $ sudo foo $ sudo: foo: ...


2

Here's one approach: #!/bin/bash ## make a temp dir tmpDir=$(mktemp -d) ## Download the files to it SauceCodePro="https://github.com/ryanoasis/nerd-fonts/blob/master/patched-fonts/SourceCodePro" curl -L -o "$tmpDir/Sauce Code Pro Nerd Font Regular.ttf" "$SauceCodePro/Regular/complete/Sauce%20Code%20Pro%20Nerd%20Font%20Complete.ttf&...


2

Just to post something completely different: if ! tty -s 2>&0 then echo "Need to run with standard error on a terminal" exit 1 fi if [ x"`stat --printf=%u $(tty)`" != x"0" ] then echo "Must be logged in as root. Can't use sudo or su here." exit 1 fi This works by checking if the terminal ...


1

As mentioned by steeldriver the issue was indeed the extra space between < and (.


1

So, it looks like (in all my attempts) I never simply did xhost + as the original user, which opens up X to allow any other user to put a display on it. I guess that's the problem with having so many suggestions, you can miss out on the obvious one. For the record, I understand that xhost + creates security issues. However, it does demonstrate where and how ...


1

You can prevent a process from forking children by setting the RLIMIT_NPROC limit. For instance, with zsh: #! /bin/zsh - limit -h maxproc 0 exec zoom "$@" Would start zoom and prevent it from forking children by setting a hard limit on the maximum number of processes. Now, I can't tell about zoom specifically, but depending on the application, ...


1

In order to be able to move a file to a folder you need to have write permission to that folder. In order to be able to get to some folder you need to have execute permission on the folders above it, in its path. Write permission on the folder gives you permission to create new object in it (files or folders). Execute permission on the folder gives you ...


1

Possibly, if anyone still needs the answer, I also encountered the same situation and managed to cope with the problem following way: In the section "example entries" of the arch-wiki for sudo command https://wiki.archlinux.org/index.php/Sudo There is a possible variant: Open your sudoers file with sudo visudo After the Defaults section (was ...


Only top voted, non community-wiki answers of a minimum length are eligible