How do I force the user to become root
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I have written a bash script for use on my ubuntu box. Now I would like to prevent running this script under my own user and only able to run it as root (sudo).
Is there a possibility to force this. Can I somehow let my script ask for root permissions if I run it under my own username?
bash permissions
migrated from stackoverflow.com Jan 6 '12 at 14:56
This question came from our site for professional and enthusiast programmers.
add a comment |Â
up vote
6
down vote
favorite
I have written a bash script for use on my ubuntu box. Now I would like to prevent running this script under my own user and only able to run it as root (sudo).
Is there a possibility to force this. Can I somehow let my script ask for root permissions if I run it under my own username?
bash permissions
migrated from stackoverflow.com Jan 6 '12 at 14:56
This question came from our site for professional and enthusiast programmers.
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have written a bash script for use on my ubuntu box. Now I would like to prevent running this script under my own user and only able to run it as root (sudo).
Is there a possibility to force this. Can I somehow let my script ask for root permissions if I run it under my own username?
bash permissions
I have written a bash script for use on my ubuntu box. Now I would like to prevent running this script under my own user and only able to run it as root (sudo).
Is there a possibility to force this. Can I somehow let my script ask for root permissions if I run it under my own username?
bash permissions
bash permissions
asked Sep 22 '10 at 10:55
Peter Smit
4792927
4792927
migrated from stackoverflow.com Jan 6 '12 at 14:56
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com Jan 6 '12 at 14:56
This question came from our site for professional and enthusiast programmers.
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
8
down vote
accepted
I have a standard function I use in bash
for this very purpose:
# Check if we're root and re-execute if we're not.
rootcheck ()
if [ $(id -u) != "0" ]
then
sudo "$0" "$@" # Modified as suggested below.
exit $?
fi
There's probably more elegant ways (I wrote this AGES ago!), but this works fine for me.
To use this function consistently in a script, you should pass it the arguments received by the script originally.
Usage: rootcheck "$@"
(rootcheck
alone will not pass any arguments to the rootcheck function)
1
Possiblyexec sudo "$0" "$@"
to avoid problems with spaces in arguments, and avoid have the first execution hanging around?
â Douglas Leeder
Sep 23 '10 at 9:33
Yeah, that would be a decided improvement. I'll update the answer to reflect it.
â JUST MY correct OPINION
Sep 23 '10 at 10:11
Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it withrootcheck "$@"
). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).
â Gordon Davisson
Sep 23 '10 at 19:18
I do it first thing in the script, so yeah. Early is right.
â JUST MY correct OPINION
Sep 24 '10 at 0:11
It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something likeif [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi
and then modify the sudo command toexec sudo "$0" --no-sanity-checks "$@"
â Gordon Davisson
Sep 25 '10 at 17:28
add a comment |Â
up vote
4
down vote
i am using this single line version, easy to copy paste on top of scripts
[ `whoami` = root ] || sudo "$0" "$@"; exit $?;
1
This would work, but it creates a dependency onsudo
. It's probably better form to just check$EUID
and quit if not root.
â HalosGhost
Sep 2 '14 at 21:15
add a comment |Â
up vote
2
down vote
sudo chown root yourcode
sudo chmod 500 yourcode
Voila!
add a comment |Â
up vote
1
down vote
You can change the permissions, so that only root can execute it.
Or you could use whoami
command and in case that it isn't root force sudo.
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
I have a standard function I use in bash
for this very purpose:
# Check if we're root and re-execute if we're not.
rootcheck ()
if [ $(id -u) != "0" ]
then
sudo "$0" "$@" # Modified as suggested below.
exit $?
fi
There's probably more elegant ways (I wrote this AGES ago!), but this works fine for me.
To use this function consistently in a script, you should pass it the arguments received by the script originally.
Usage: rootcheck "$@"
(rootcheck
alone will not pass any arguments to the rootcheck function)
1
Possiblyexec sudo "$0" "$@"
to avoid problems with spaces in arguments, and avoid have the first execution hanging around?
â Douglas Leeder
Sep 23 '10 at 9:33
Yeah, that would be a decided improvement. I'll update the answer to reflect it.
â JUST MY correct OPINION
Sep 23 '10 at 10:11
Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it withrootcheck "$@"
). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).
â Gordon Davisson
Sep 23 '10 at 19:18
I do it first thing in the script, so yeah. Early is right.
â JUST MY correct OPINION
Sep 24 '10 at 0:11
It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something likeif [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi
and then modify the sudo command toexec sudo "$0" --no-sanity-checks "$@"
â Gordon Davisson
Sep 25 '10 at 17:28
add a comment |Â
up vote
8
down vote
accepted
I have a standard function I use in bash
for this very purpose:
# Check if we're root and re-execute if we're not.
rootcheck ()
if [ $(id -u) != "0" ]
then
sudo "$0" "$@" # Modified as suggested below.
exit $?
fi
There's probably more elegant ways (I wrote this AGES ago!), but this works fine for me.
To use this function consistently in a script, you should pass it the arguments received by the script originally.
Usage: rootcheck "$@"
(rootcheck
alone will not pass any arguments to the rootcheck function)
1
Possiblyexec sudo "$0" "$@"
to avoid problems with spaces in arguments, and avoid have the first execution hanging around?
â Douglas Leeder
Sep 23 '10 at 9:33
Yeah, that would be a decided improvement. I'll update the answer to reflect it.
â JUST MY correct OPINION
Sep 23 '10 at 10:11
Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it withrootcheck "$@"
). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).
â Gordon Davisson
Sep 23 '10 at 19:18
I do it first thing in the script, so yeah. Early is right.
â JUST MY correct OPINION
Sep 24 '10 at 0:11
It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something likeif [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi
and then modify the sudo command toexec sudo "$0" --no-sanity-checks "$@"
â Gordon Davisson
Sep 25 '10 at 17:28
add a comment |Â
up vote
8
down vote
accepted
up vote
8
down vote
accepted
I have a standard function I use in bash
for this very purpose:
# Check if we're root and re-execute if we're not.
rootcheck ()
if [ $(id -u) != "0" ]
then
sudo "$0" "$@" # Modified as suggested below.
exit $?
fi
There's probably more elegant ways (I wrote this AGES ago!), but this works fine for me.
To use this function consistently in a script, you should pass it the arguments received by the script originally.
Usage: rootcheck "$@"
(rootcheck
alone will not pass any arguments to the rootcheck function)
I have a standard function I use in bash
for this very purpose:
# Check if we're root and re-execute if we're not.
rootcheck ()
if [ $(id -u) != "0" ]
then
sudo "$0" "$@" # Modified as suggested below.
exit $?
fi
There's probably more elegant ways (I wrote this AGES ago!), but this works fine for me.
To use this function consistently in a script, you should pass it the arguments received by the script originally.
Usage: rootcheck "$@"
(rootcheck
alone will not pass any arguments to the rootcheck function)
edited 11 mins ago
yosefrow
1768
1768
answered Sep 22 '10 at 11:02
JUST MY correct OPINION
1962
1962
1
Possiblyexec sudo "$0" "$@"
to avoid problems with spaces in arguments, and avoid have the first execution hanging around?
â Douglas Leeder
Sep 23 '10 at 9:33
Yeah, that would be a decided improvement. I'll update the answer to reflect it.
â JUST MY correct OPINION
Sep 23 '10 at 10:11
Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it withrootcheck "$@"
). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).
â Gordon Davisson
Sep 23 '10 at 19:18
I do it first thing in the script, so yeah. Early is right.
â JUST MY correct OPINION
Sep 24 '10 at 0:11
It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something likeif [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi
and then modify the sudo command toexec sudo "$0" --no-sanity-checks "$@"
â Gordon Davisson
Sep 25 '10 at 17:28
add a comment |Â
1
Possiblyexec sudo "$0" "$@"
to avoid problems with spaces in arguments, and avoid have the first execution hanging around?
â Douglas Leeder
Sep 23 '10 at 9:33
Yeah, that would be a decided improvement. I'll update the answer to reflect it.
â JUST MY correct OPINION
Sep 23 '10 at 10:11
Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it withrootcheck "$@"
). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).
â Gordon Davisson
Sep 23 '10 at 19:18
I do it first thing in the script, so yeah. Early is right.
â JUST MY correct OPINION
Sep 24 '10 at 0:11
It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something likeif [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi
and then modify the sudo command toexec sudo "$0" --no-sanity-checks "$@"
â Gordon Davisson
Sep 25 '10 at 17:28
1
1
Possibly
exec sudo "$0" "$@"
to avoid problems with spaces in arguments, and avoid have the first execution hanging around?â Douglas Leeder
Sep 23 '10 at 9:33
Possibly
exec sudo "$0" "$@"
to avoid problems with spaces in arguments, and avoid have the first execution hanging around?â Douglas Leeder
Sep 23 '10 at 9:33
Yeah, that would be a decided improvement. I'll update the answer to reflect it.
â JUST MY correct OPINION
Sep 23 '10 at 10:11
Yeah, that would be a decided improvement. I'll update the answer to reflect it.
â JUST MY correct OPINION
Sep 23 '10 at 10:11
Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it with
rootcheck "$@"
). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).â Gordon Davisson
Sep 23 '10 at 19:18
Note that if it's run as a function like this, you need to pass in the script's parameters (i.e. run it with
rootcheck "$@"
). Or you can just put the if block inline rather than in a function. Also, this should be done very early in the script, as anything before it will be run twice (once normally, once as root).â Gordon Davisson
Sep 23 '10 at 19:18
I do it first thing in the script, so yeah. Early is right.
â JUST MY correct OPINION
Sep 24 '10 at 0:11
I do it first thing in the script, so yeah. Early is right.
â JUST MY correct OPINION
Sep 24 '10 at 0:11
It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something like
if [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi
and then modify the sudo command to exec sudo "$0" --no-sanity-checks "$@"
â Gordon Davisson
Sep 25 '10 at 17:28
It's sometimes good to run sanity checks (e.g. does the file in question exist?) before sudo'ing -- I tend to do this by starting the script with something like
if [[ $1 == "--no-sanity-checks" ]]; then shift; else run sanity checks; fi
and then modify the sudo command to exec sudo "$0" --no-sanity-checks "$@"
â Gordon Davisson
Sep 25 '10 at 17:28
add a comment |Â
up vote
4
down vote
i am using this single line version, easy to copy paste on top of scripts
[ `whoami` = root ] || sudo "$0" "$@"; exit $?;
1
This would work, but it creates a dependency onsudo
. It's probably better form to just check$EUID
and quit if not root.
â HalosGhost
Sep 2 '14 at 21:15
add a comment |Â
up vote
4
down vote
i am using this single line version, easy to copy paste on top of scripts
[ `whoami` = root ] || sudo "$0" "$@"; exit $?;
1
This would work, but it creates a dependency onsudo
. It's probably better form to just check$EUID
and quit if not root.
â HalosGhost
Sep 2 '14 at 21:15
add a comment |Â
up vote
4
down vote
up vote
4
down vote
i am using this single line version, easy to copy paste on top of scripts
[ `whoami` = root ] || sudo "$0" "$@"; exit $?;
i am using this single line version, easy to copy paste on top of scripts
[ `whoami` = root ] || sudo "$0" "$@"; exit $?;
answered Sep 2 '14 at 20:31
fsh
411
411
1
This would work, but it creates a dependency onsudo
. It's probably better form to just check$EUID
and quit if not root.
â HalosGhost
Sep 2 '14 at 21:15
add a comment |Â
1
This would work, but it creates a dependency onsudo
. It's probably better form to just check$EUID
and quit if not root.
â HalosGhost
Sep 2 '14 at 21:15
1
1
This would work, but it creates a dependency on
sudo
. It's probably better form to just check $EUID
and quit if not root.â HalosGhost
Sep 2 '14 at 21:15
This would work, but it creates a dependency on
sudo
. It's probably better form to just check $EUID
and quit if not root.â HalosGhost
Sep 2 '14 at 21:15
add a comment |Â
up vote
2
down vote
sudo chown root yourcode
sudo chmod 500 yourcode
Voila!
add a comment |Â
up vote
2
down vote
sudo chown root yourcode
sudo chmod 500 yourcode
Voila!
add a comment |Â
up vote
2
down vote
up vote
2
down vote
sudo chown root yourcode
sudo chmod 500 yourcode
Voila!
sudo chown root yourcode
sudo chmod 500 yourcode
Voila!
answered Sep 22 '10 at 11:00
mkoistinen
1234
1234
add a comment |Â
add a comment |Â
up vote
1
down vote
You can change the permissions, so that only root can execute it.
Or you could use whoami
command and in case that it isn't root force sudo.
add a comment |Â
up vote
1
down vote
You can change the permissions, so that only root can execute it.
Or you could use whoami
command and in case that it isn't root force sudo.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can change the permissions, so that only root can execute it.
Or you could use whoami
command and in case that it isn't root force sudo.
You can change the permissions, so that only root can execute it.
Or you could use whoami
command and in case that it isn't root force sudo.
answered Sep 22 '10 at 11:00
Nuz
1111
1111
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f28454%2fhow-do-i-force-the-user-to-become-root%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password