Running a script as root in the script


 
Thread Tools Search this Thread
Top Forums Programming Running a script as root in the script
# 8  
Old 11-16-2017
i just want the user to run this simple script that i made to create an ftp user account with a home directpry and i thought a normal user cannot add directorys and make a user chmod chown etc etc -

Code:
#!/bin/bash

dir=/mnt/sftp
group=sftp_users

    echo "Enter UserName:"
    read user

    if id $user ; then
        echo "$user already exists as you can see above, please re-run the script"
        exit
        else
        echo "$user not in system, ok to continue"
    fi

    echo "Enter Password:"
    read passwd
    echo "$user:$passwd" >> /ftp_details/accounts.csv
    echo "is this a normal user (press 1) ?"
    read choice
    
    case $choice in
        1)
            useradd -g $group -d $dir/$user -s /sbin/nologin $user
            mkdir -p $dir/$user/data
            chown root $dir/$user
            chmod 755 $dir/$user
            chown $user $dir/$user/data
            chmod 755 $dir/$user/data
            touch $dir/$user/data/WARNING_everything_in_here_will_get_removed_in_14_days_time.txt
            ;;
        *)
            echo "invalid selection, please re-run the script"
            exit
            ;;
    esac

    echo $user:$passwd | chpasswd

This User Gave Thanks to robertkwild For This Post:
# 9  
Old 11-16-2017
Well I can see that you have thought about what you want to achieve. Whilst we could organise to grant access to the specific powerful commands, the problem would then be that they could run them anytime, so i think you probably have it about right to only run the whole script as root. I presume you plan to expand it from a single choice later.

I notice that you don't have an audit trail in here to say who did what and when, just in case it goes wrong or needs to be shown. Whilst within the script if started as root through sudo, you can get the calling user as $SUDO_USER so you can write that in your message.



I hope that this helps, but I think have everything you need. Does it make sense or have I/we left you confused?



Robin
# 10  
Old 11-16-2017
Hi Robin yes im confused, what do you mean sudo_user

do you mean for every command i have in my script put sudo infront of it

then i thought instead of putting sudo infront of every command just make the user type su and job done ie from there it will run all the commands as root
# 11  
Old 11-16-2017
Quote:
Originally Posted by robertkwild
Hi Robin yes im confused, what do you mean sudo_user

do you mean for every command i have in my script put sudo infront of it

then i thought instead of putting sudo infront of every command just make the user type su and job done ie from there it will run all the commands as root
I thought I explained this in post #4 in this thread when I said:
Quote:
Nothing in your script after invoking su will be run with root privileges. The su utility, if given a proper password, will start a shell and nothing in the rest of your script will be run until that shell exits.
Once a user types the root password in response to invoking the command su (without operands), they can then type any commands into the shell that su starts for them and it will run those commands with all of the privileges of someone who logged in as root. When they exit that super-user shell, your script will then continue running with the same privileges as the user who invoked that utility had when they invoked your script. No commands in your script after the shell started by su exits will run with root privileges unless it was root who invoked your script to being with.

The here-document trick I also showed you in that post can be used to feed commands into that super-user shell. The text in that here-document is just read and executed by the shell that su starts; it is not that su is running commands in your script.

The logical easy way to do this (if a user who is going to run your script knows the root password and wants to run your script with root privileges) is for them to run su and then while in the shell that su starts have them run your script and do whatever else they need to do as root before exiting that super-user shell.
# 12  
Old 11-17-2017
Gluing everything in this thread together, we have (my insertions in green):-
Code:
#!/bin/bash

if [ $(id -u) -ne 0 ]
then
   exec sudo $0 "$@"             # Will overwrite this process so script does not continue as self
fi                               # Any arguments are passed on as supplied

# A superuser will carry on through here

LOGFILE=/var/lib/creation.log    # .... or whatever suits you

dir=/mnt/sftp
group=sftp_users	

echo "Enter UserName:"
read user

if id $user ; then
        echo "$user already exists as you can see above, please re-run the script"
        exit
else
        echo "$user not in system, ok to continue"
fi

echo "Enter Password:"
read passwd
echo "$user:$passwd" >> /ftp_details/accounts.csv
echo "is this a normal user (press 1) ?"
read choice
    
case $choice in
        1)
            echo "$(date) : User $SUDO_USER creating $user" >> $LOGFILE
            useradd -g $group -d $dir/$user -s /sbin/nologin $user
            mkdir -p $dir/$user/data
            chown root $dir/$user
            chmod 755 $dir/$user
            chown $user $dir/$user/data
            chmod 755 $dir/$user/data
            touch $dir/$user/data/WARNING_everything_in_here_will_get_removed_in_14_days_time.txt
            ;;
        *)
            echo "invalid selection, please re-run the script"
            exit
            ;;
esac

echo $user:$passwd | chpasswd

You would then need to add a rule using visudo to allow your selected user(s)/group(s) to run this script. You will need to be a super-user to run visudo
Add the lines like these:-
Code:
#Individual users
robert1			ALL = NOPASSWD: /path/to/this_script
trusted1		ALL = PASSWD: /path/to/this_script

#Group members are trusted
%trustedgroup		ALL = PASSWD: /path/to/this_script

The account robert1 will just pass into the script, but trusted1 and members of the group trustedgroup will have to enter their own password to continue. This means they don't need to know the all-powerful account password. If they do, then there is no way to control them.

Using sudo means that you can grant them privileges they need for just when they are doing what you want and nothing more, i.e. you trust them to run this script, but not to become the super-user because they might remove /etc/passwd by mistake.


How far does this get you now?


Am I just more confusing? Apologies if I am.
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Root running a script calling to scp using user "xyz" is not authenticating!

Close duplicate thread. (0 Replies)
Discussion started by: denissi
0 Replies

2. Shell Programming and Scripting

Running a script as root but with different users inside

Hi All, my script.sh has the below lines, and i need to run the script as root or wam. please tell me if this will work #!/bin/bash sudo -t wam /usr/local/wam/stopwam -r ------- this needs run as wam user /usr/local/web/stopweb -a --- this needs to run as... (18 Replies)
Discussion started by: nanz143
18 Replies

3. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

4. Shell Programming and Scripting

Script for running root based C++ code

Hi all, I have to run C++ file using root programming, using following commands: $root -l root .L TwoTrees.C++ root TwoTrees t root t.Loop() root.q I wonder if I can write script to do the following. Thanks Pooja (12 Replies)
Discussion started by: nrjrasaxena
12 Replies

5. Shell Programming and Scripting

Need to run a bash script that logs on as a non-root user and runs script as root

So I have a script that runs as a non-root user, lets say the username is 'xymon' . This script needs to log on to a remote system as a non-root user also and call up a bash script that runs another bash script as root. in short: user xymon on system A needs to run a file as root user and have... (2 Replies)
Discussion started by: damang111
2 Replies

6. Shell Programming and Scripting

Issue running script as root

1) Environment:Red Hat Linux, bash shell Script to be run owned by user :myUser Home environment of myUser: pathto/home 2) ESP agent with root access will run JobXXX.sh su - myUser -c "/pathto/home/bin/script.sh" where script.sh has some echo statements and an exit statement in the end... (4 Replies)
Discussion started by: cj09
4 Replies

7. Cybersecurity

Running script through SSH as root

Hi all, I have a situation where I have a shell script that I need to run remotely on multiple *nix machines via SSH. Unfortunately, some of the commands in it require root access. I know that best practices for ssh entail configuring it so that the root account cannot log in, you need to... (4 Replies)
Discussion started by: irinotecan
4 Replies

8. Shell Programming and Scripting

As root , running script as different user with su - problem

Dear All I am running into a situation where I am running a script as another user lets say oracle using su command as below, and the script fails because the .profile of oracle is not executed so the environment variables are not set. cat /etc/passwd | grep oracle... (4 Replies)
Discussion started by: dbsupp
4 Replies

9. Shell Programming and Scripting

Running a command or script as root

I'm writing an application (Progress language) that needs to: 1) load the contents of a cron table into the Progress application; 2) display this information in a human manner and allow a select group of people to update it (these people are logged in as themselves, not as root); 3) save... (3 Replies)
Discussion started by: rm-r
3 Replies

10. UNIX for Dummies Questions & Answers

Run non-root script as root with non-root environment

All, I want to run a non-root script as the root user with non-root environment variables with crontab. The non-root user would have environment variables for database access such as Oracle or Sybase. The root user does not have the Oracle or Sybase enviroment variables. I thought you could do... (2 Replies)
Discussion started by: bubba112557
2 Replies
Login or Register to Ask a Question