Sudo has no access to exported bash function


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Sudo has no access to exported bash function
# 1  
Old 10-22-2019
Sudo has no access to exported bash function

Hello.
I am running leap 15.1
sudo behavior may differ from other distrib.


I have a file with functions definition

Code:
....
function_1 {
echo "Hello world"

}
export -f function_1

This file is sourced by both /etc/bash.bashrc.local and profile.local.


So the functions are accessible for every body.
for user root :
Code:
LC_ALL=C type -t function_1

return 'function'
for standard user
Code:
LC_ALL=C type -t function_1

return 'function'
for root user.



Now I write a script named test.sh which contain

#
# test.sh

#
# begin


function_1


# end




and put it in /tmp


Running as standard user :
Code:
:~>/tmp/test

I got
Code:
Hello world

Running as root user :
Code:
:#>/tmp/test

I got
Code:
Hello world

Now as standard user
Code:
sudo /tmp/test.sh

I got :
Code:
:~>sudo /tmp/test.sh
[sudo] password for root: 
/tmp/test.sh: line 1: function_1: command not found

How to give sudo access to exported function.

Now as root user
Code:
sudo /tmp/test.sh

I got :
Code:
:#>sudo /tmp/test.sh
[sudo] password for root: 
/tmp/test.sh: line 1: function_1: command not found

How to give sudo access to exported function.


sudo have access to environment variables which are exported , but not function.


Any help is welcome
# 2  
Old 10-22-2019
Quote:
This file is sourced by both /etc/bash.bashrc.local and profile.local.
These files are used on login. sudo bash is not a login.

Environment variables are external memory designed to be shared. Functions are part of a shell's internals and are not. For sudo bash to have a function, it will need to source that file.

Code:
#!/bin/bash

. /etc/bash.bashrc.local

function_1

sudo often blocks environment variables, by the way, to prevent people putting in strange values for EDITOR and the like and executing them with dangerous privileges.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-23-2019
Hi,

You can preserve your current environment if you have been granted sufficient rights to do so with the -E switch or --preserve-env switch.

Regards

Gull04
# 4  
Old 11-09-2019
Quote:
Originally Posted by gull04
Hi,

You can preserve your current environment if you have been granted sufficient rights to do so with the -E switch or --preserve-env switch.

Regards

Gull04
My test show that does not work for function as Corona688 just said.


Thank you

--- Post updated at 18:06 ---

Quote:
Originally Posted by Corona688
These files are used on login. sudo bash is not a login.

Environment variables are external memory designed to be shared. Functions are part of a shell's internals and are not. For sudo bash to have a function, it will need to source that file.

Code:
#!/bin/bash

. /etc/bash.bashrc.local

 function_1

sudo often blocks environment variables, by the way, to prevent people putting in strange values for EDITOR and the like and executing them with dangerous privileges.

That mean that any script I run which need to be started with sudo needs to contains something like that :
Code:
#
# ~/test_001.sh
#

. /path/to/my_list_of_functions


#
# Code followed

#

But if the same script may be run by normal user, my functions will be sourced twice
One times by the login process via /etc/profile.local
One times by the code added on top of script.
What happens ?



Any comment is welcome.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Loading associative array from exported function

Hello. I have an export of an associative array build using declare -p SOME_ARRAY_NAME > SOME_FILE_NAME.txt. Producing some thing like declare -A SOME_ARRAY_NAME=( ="some_text" ="a_text" ......... ="another_text" ) in a text file. I have a stock of functions which are sourced from... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Using plink with sudo access

I have similar issue as mentioned in 167174-how-run-script-using-batch-file.html It works good, but the control is not coming back to source i tried adding exit to remote script. Thanks, Suresh (0 Replies)
Discussion started by: snsuresh
0 Replies

3. UNIX for Dummies Questions & Answers

Inheriting SUDO access?

I had a question on users inheriting SUDO capabilities of another account. Let's say that there are three users A, B, and C. A has access to Sudo into B. B has access to Sudo into C. Does this give A access to sudo into B and then sudo into C. A -> B B -> C A -> B -> C ? Another example. My... (2 Replies)
Discussion started by: sbcopty
2 Replies

4. Red Hat

Sudo access issue

Hi, I have given access to user mwadmin in shudders file as : mwadmin ALL:NOPASSWD:/www/* /usr/* /opt/* However, not able to execute below command: sudo mkdir -p /usr/test password for mwadmin: Sorry, user mwadmin is not allowed to execute '/bin/mkdir -p /usr/test' as root. ... (4 Replies)
Discussion started by: saurau
4 Replies

5. UNIX for Advanced & Expert Users

Help needed in sudo access

I want to give root access to a user called denielr on server - tsprd01, but do not want to share root password. I have sudoers configured already. He should have all access equal to root. I made this entry in /etc/sudoers, but it is not working denielr tsprd01 =(root) NOPASSWD: ALL I tried to... (2 Replies)
Discussion started by: solaris_1977
2 Replies

6. Ubuntu

Restrict SUDO Access

Linux ubuntu 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux Hi Folks, Please help me. I am bit struck here. Here is the OS info. Linux ubuntu 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux I have a... (17 Replies)
Discussion started by: explorer007
17 Replies

7. UNIX for Dummies Questions & Answers

sudo/root access

I'm actually working with a Ubuntu-System here and have a question about executing a command with 'sudo'. I tried and got a error message like "not allowed". After this I logged in with 'sudo -s' and typed the command without 'sudo'. This worked well. Can please somebody explain me this... (0 Replies)
Discussion started by: daWonderer
0 Replies

8. Shell Programming and Scripting

ONLY SU Sudo access

Hello All, I want to create a script that will do ONLY su to any user on the server with hpadmin login using sudo. Can anyone let me know how can it do it. Regards Ankit (1 Reply)
Discussion started by: ajaincv
1 Replies

9. Shell Programming and Scripting

Scope of exported function

Hi I'm hoping someone can tell me how to extend the scope of an exported function in the korn shell. I have written a function in a file that I dot in from my .kshrc file and it works fine. However I would like this function to be available to anyone in a certain group on the machine... (10 Replies)
Discussion started by: steadyonabix
10 Replies

10. Linux

sudo access verification

Hi All, I got lots of request with sudo, a manager request, verbal command, do this and do that. The problem with this kind of request is when I added that script and that. It will not be perfect, it's because I can't verify the userid sudo access, I can't reset their password as well, I... (2 Replies)
Discussion started by: itik
2 Replies
Login or Register to Ask a Question