Executing bash file with sudo for the second time, leads to permission denied, for some commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing bash file with sudo for the second time, leads to permission denied, for some commands
# 1  
Old 04-03-2019
Executing bash file with sudo for the second time, leads to permission denied, for commands

I have a script that checks if the script has been ran with sudo.
If the script is not ran as sudo, the current script is being executed with exec sudo bash.
You are asked for a password, you type in the password, success. Everything is perfect - the commands inside the script are ran as sudo.
However, the second time you run the script, you are getting cat: /etc/sudoers: Permission denied

What is this happening and how can this be fixed?


Output of the Script:

Code:
vaidas@vaidas-SATELLITE-L855:~$ cd Desktop
vaidas@vaidas-SATELLITE-L855:~/Desktop$ bash shellscript.sh 
sudo: a password is required
[sudo] password for vaidas: 
----------YAS HAVE SUDO----------
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults    env_reset
Defaults    mail_badpass
vaidas@vaidas-SATELLITE-L855:~/Desktop$ bash shellscript.sh 
----------YAS HAVE SUDO----------
head: cannot open '/etc/sudoers' for reading: Permission denied
vaidas@vaidas-SATELLITE-L855:~/Desktop$

The Script:
Code:
#!/bin/bash

if sudo -n true;
then
  echo "----------YAS HAVE SUDO----------";
  head /etc/sudoers;
  
else
  exec sudo bash "$0" "$@";
fi


Last edited by boqsc; 04-03-2019 at 04:32 PM..
# 2  
Old 04-03-2019
That you can run sudo -n true doesn't always mean you're root. Sometimes it just means you ran sudo recently enough it isn't asking for your password again. Check if UID is 0 too.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

ISSUE : bash: cd: /rep/prok: Permission denied

Hi All, i have two accounts with acl permissions. I need to provide read access to both users. for one user s109552 "cd" is not working. drwxrws---+ 8 cvsadmin reto 1024 Aug 23 2011 reto su5sr117# su - s109553 $ bash bash-3.2$ cd /rep/reto bash-3.2$ pwd <-------- it... (2 Replies)
Discussion started by: Naveen.6025
2 Replies

2. Shell Programming and Scripting

Executing multiple commands in a file at same time

Hi Am having file.ksh as below wc -l file1.txt wc -l file2.txt wc -l file3.txt wc -l file4.txt i want all the commands in this file to execute in same time please help Thanks in advance (1 Reply)
Discussion started by: ragu.selvaraj
1 Replies

3. UNIX for Dummies Questions & Answers

Permission denied for bash

my file permissions are -rwxrwxrwx for runInstall file and while giving " sudo bash runInstall.sh " then i am getting error " permission denied" now i don't see any reasonable answer for that please suggest what should i do? :eek: got to start DBConnection practice with oracle database and... (4 Replies)
Discussion started by: toshanshu
4 Replies

4. Shell Programming and Scripting

OSX, bash, cat with <<MARKER executing commands

I have a script that writes another script with cat >/usr/local/bin/myscript.sh <<EOF #!/bin/sh VAR=`run a command here` EOF Problem is, after this is run, I get: $ cat /usr/local/bin/myscript.sh #!/bin/sh VAR=result of command How do I stop that from happening with Macs... (2 Replies)
Discussion started by: jnojr
2 Replies

5. UNIX for Dummies Questions & Answers

bash: permission denied...(after it allows me?)

I'm sure this question comes up a lot, but I'm particularly confused in this situation. I can run my program once, just fine. However, if I try to run it again without recompiling, bash refuses to give me permission. I have a small idea why, but I don't think think I'm right. Here's what I'm... (1 Reply)
Discussion started by: statichazard
1 Replies

6. UNIX for Dummies Questions & Answers

Bash: permission denied

Hello, I have a problem creating files in a certain folder. I installed apache and mysql today, using this guide: http://computerfaq.be/ubuntu-apache-mysql-php-server-installeren/ (can't post links yet :( ) Instead of /home/www/html I used /home/www/ for the document root. Now I... (12 Replies)
Discussion started by: emveedee
12 Replies

7. UNIX for Dummies Questions & Answers

changing password with sudo user " permission denied"

HI All, I am using solaris i created a user adam and updated his permissions in vi sudoers file as follows adam ALL=(ALL) NOPASSWORD: ALL ........... when i create user by logging as sudo user . $ sudo useradd -d /home/kalyan -m -s /bin/sh kalyan sudo: not found ... (6 Replies)
Discussion started by: kalyankalyan
6 Replies

8. Shell Programming and Scripting

Executing expect script giving message as bad interpreter: Permission denied

Hi Gurus, I am new to scripting and needs your help in expect script used for telnet. I wrote a simple script as #!/usr/bin/expect-5.43 -f spawn telnet localhost 2233 expect "password:" send "secret\r" send "i data.cnbc.com\r" send "exit\r" expect eof When I am trying to execute... (2 Replies)
Discussion started by: niks_yv
2 Replies

9. Shell Programming and Scripting

Permission denied while executing rsh

Hi All, I have two servers, say server1 and server2. I have put the entries of both servers in /.rhosts file of both servers. So, when I tried to run following from server1- rsh server2 "ls" Permission denied was printed on console. Can anyone help me out? Regards, akash mahakode (4 Replies)
Discussion started by: akash_mahakode
4 Replies

10. UNIX for Dummies Questions & Answers

Why ' Permission denied' when executing a command?

Hi All, When I execute a command on Unix and it says 'Permission denied'. What could be the reason and how can it be resolved? Many thanks, David (2 Replies)
Discussion started by: david_wang
2 Replies
Login or Register to Ask a Question