I need to run a bunch of scripts (with sudo) that use a single file.sh as a configuration file for all. Initially I've put the file.sh in /etc/profile.d and when I ran the scripts as root everything was ok (because when I connected to the machine it first sourced the file.sh and all vars in that file were available) but now, for security reasons, I need to run them with another user with sudo rights.
我需要运行一堆脚本(使用sudo),它们使用单个file.sh作为所有的配置文件。最初我把file.sh放在/etc/profile.d中,当我以root身份运行脚本时,一切正常(因为当我连接到机器时,它首先获取file.sh并且该文件中的所有vars都可用)但是现在,出于安全原因,我需要与另一个具有sudo权限的用户运行它们。
When running with sudo the "configuration file" in /etc/profile.d does not get sourced even if I'm root and do sudo - it's the same.
当使用sudo运行时,即使我是root用户并执行sudo,/ etc / profile.d中的“配置文件”也不会获取 - 它是相同的。
Using "sudo -E" is not an option, also this kind of solution "Defaults env_keep += "ftp_proxy http_proxy https_proxy no_proxy"" does not work for me as the vars in the file change a lot and it's easier to throw a file, with all the vars, in a location - like /etc/profile.d/ - instead to adding options to /etc/sudoers.
使用“sudo -E”不是一个选项,这种解决方案“Defaults env_keep + =”ftp_proxy http_proxy https_proxy no_proxy“”对我来说不起作用,因为文件中的变量变化很大并且更容易抛出文件,与所有变量,在一个位置 - 如/etc/profile.d/ - 而不是添加选项到/ etc / sudoers。
Later Edit (working):
稍后编辑(工作):
Moved original sudo command to sudo.orig. Created a new sudo bash script
将原始sudo命令移至sudo.orig。创建了一个新的sudo bash脚本
[root@NS1 bin]# cat sudo
#!/bin/bash
source /etc/profile.d/set_env_vmdeploy.sh
sh /usr/bin/sudo.orig "$@"
and gave it permissions
并赋予它权限
[root@NS1 bin]# chmod 4111 sudo
[root@NS1 bin]# ll sudo*
---s--x--x 1 root root 78 May 7 13:42 sudo
---s--x--x 1 root root 123832 Jul 31 2014 sudo.orig
1 个解决方案
#1
If you want sudo
to execute all the profile scripts in the child shell, you can tell it to invoke the shell as a login shell: sudo -i /usr/local/bin/my_script.sh
. (Note that the child shell will start with the working directory set to /root
, and also that this may have other unintended side effects.)
如果您希望sudo执行子shell中的所有配置文件脚本,您可以告诉它将shell作为登录shell调用:sudo -i /usr/local/bin/my_script.sh。 (请注意,子shell将以工作目录设置为/ root开头,并且这可能会产生其他意外的副作用。)
Alternatively, invoke bash explicitly with a command parameter: sudo /bin/bash -c "source ./config.sh; ./real_script.sh"
.
或者,使用命令参数显式调用bash:sudo / bin / bash -c“source ./config.sh; ./real_script.sh”。
#1
If you want sudo
to execute all the profile scripts in the child shell, you can tell it to invoke the shell as a login shell: sudo -i /usr/local/bin/my_script.sh
. (Note that the child shell will start with the working directory set to /root
, and also that this may have other unintended side effects.)
如果您希望sudo执行子shell中的所有配置文件脚本,您可以告诉它将shell作为登录shell调用:sudo -i /usr/local/bin/my_script.sh。 (请注意,子shell将以工作目录设置为/ root开头,并且这可能会产生其他意外的副作用。)
Alternatively, invoke bash explicitly with a command parameter: sudo /bin/bash -c "source ./config.sh; ./real_script.sh"
.
或者,使用命令参数显式调用bash:sudo / bin / bash -c“source ./config.sh; ./real_script.sh”。