how to set environment variable for root user - macos

I'm Mac user.
I want to set PYTHONPATH env for root. so
$ sudo su -
# vi ~/.profile
and add to file 'export PYTHONPATH=/mypythonlib'
then
# env
I can see this line
PYTHONPATH=/Users/simpnet2/projects/meiji/src/hershey
but..
when I use sudo command, cannot find that
$ sudo env
.. there's no PYTHONPATH
My program has to run with sudo command and needs PYTHONPATH.

If you use sh try /etc/profile, bash try /etc/bashrc and if you use zsh try /etc/zshenv.

You can make PYTHONPATH visible to sudo be editing your sudoers file. Notice you should ONLY do this through visudo as explained here.

You should try sudo -i which will simulate logging in as root and source the ~root/.profile.

As of 10.8.5, putting my environment statements in the .profile path in the home of the root user (/var/root) worked. after quitting bash and coming back to the root user prompt with 'su -', I could see my new path, etc. with the 'env' command and my MacPorts installationw orking correctly.
MacBook-Pro:~ root# cat /var/root/.profile
export MANPATH=/opt/local/share/man:$MANPATH
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
MacBook-Pro:~ root# which port
/opt/local/bin/port

Well, in other Linux system, it is also right that 'sudo' does not use local environment variable. But you can declare the temporary environment variable along with 'sudo' command.
For example, in your case, you can add 'PYTHONPATH=/mypythonlib' in your command 'sudo env', and the final command is:
sudo PYTHONPATH=/mypythonlib env
You can also read this article: Using sudo. You can see how 'sudo' keep or ignore user-defined environment variables.

In the case of logging in as a normal user and invoking "su - root" I found that Mac OS 10.8.5's bash was ignoring .profile and .bash_profile; I was unable to change root's $PATH by editing those files. What did work was editing /etc/paths. After exiting the root shell and entering again with "su - root" the new path was present.

Related

Command not found with sudo, but works without sudo

I've installed a binary dep in my GOPATH at /home/me/go/bin to be used.
Running dep successfully executes the binary, however running sudo dep results in sudo: dep: command not found:
$ dep
Dep is a tool for managing dependencies for Go projects
Usage: "dep [command]"
...
Use "dep help [command]" for more information about a command.
$ sudo dep
sudo: dep: command not found
The paths are not the issue here:
$ echo $PATH
/usr/share/Modules/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin:/home/me/.local/bin:/home/me/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin
$ sudo echo $PATH
/usr/share/Modules/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/var/lib/snapd/snap/bin:/home/me/.local/bin:/home/me/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin:/home/me/.local/bin:/home/me/bin:/home/me/go/bin
The paths are identical as me and as superuser both referencing the key directory /home/me/go/bin.
Why does running dep without sudo succeed but with sudo results in command not found?
By default, sudo does NOT pass the user's original PATH into the superuser process, and it gets some default PATH defined on the system. That's easy to see if you run "sudo env" to see the entire environment of the sudo'ed process:
$ sudo env | grep PATH
PATH=/sbin:/bin:/usr/sbin:/usr/bin
The command you tried, "sudo echo $PATH" doesn't check anything, because the shell first translates the $PATH to whatever value this variable has - and only then calls the command (sudo), so it just prints your outer environment's value :-)
To get your PATH to pass inside sudo, you can do something like this:
$ sudo PATH=$PATH sh -c env | grep PATH
PATH=/usr/share/Modules/bin:/usr/lib64/ccache:/home/nyh/gaps:/home/nyh/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/sbin:/sbin:/usr/games:/usr/local/android-sdk-linux/tools:/usr/local/android-sdk-linux/platform-tools:/home/nyh/google-cloud-sdk/bin
Basically the command I passed for sudo to run starts by setting PATH to $PATH (remember that $PATH is expanded by the outer shell, before sudo ever runs, so is the real path I want!) and running a shell (which will use this new PAT) to "env". As you can see, env did get the right path. You can replace "env" by whatever program you wanted to run.

go install errors out with mkdir: permission denied

Even after ensuring proper read / write access to your GOPATH folder you can still receive permission errors.
I tried sudo chmod -R sourcefolder/ and sudo chown -R username sourcefolder/ without any luck.
I needed to unset my GOBIN variable in order to fix this issue. GOBIN doesn't need to be set anymore by default. More details here: https://github.com/golang/go/wiki/InstallTroubleshooting
You can check whether it's set with echo $GOBIN. You can unset it via unset GOBIN. If it's set in your bash file then it's different per operating system:
macOS: nano ~/.bash_profile
Linux: nano ~/.bash_aliases
Use CTRL+W on either platform to search and type in GOBIN.

run inline shell script as root

I have a user, who's a passwordless sudoer. I need to execute shell script file with him, and make him execute one block as sudo. E.g:
su root <<"AS_ROOT"
# do something with my linux
AS_ROOT
But nothing works. I tried:
su - root <<...
sudo -s -- <<...
It barks back at me. I'm on ubuntu 16.04 lts.
Thank you.
As Cyrus points out, su is a different utility to which sudo's configuration doesn't apply.
It sounds like you're looking for something like this:
sudo -s <<'AS_ROOT'
echo "Hi from $USER."
AS_ROOT
This should output Hi from root.
Note that -s is needed to tell sudo to create a shell in order to interpret the commands passed via stdin (the here-doc). That shell is the current user's default shell, as reflected in environment variable $SHELL.

IPython Notebook doesn't see environment variables with "bash magic"

I am running IPython Notebook on a vagrant virtual machine, and port-forwarding to my local machine so that I can view the notebook in my web browser. I run a bootstrap script that sets an an environment variable JYTHON_HOME in my /etc/environment with:
sudo echo "JYTHON_HOME=/usr/lib/jvm/jython" | sudo tee -a /etc/environment
source /etc/environment
The boot script later starts up ipython notebook. When I open up the notebook,
%%bash
cat /etc/environment
yeilds
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JYTHON_HOME=/usr/lib/jvm/jython
but
%%bash
echo $JYTHON_HOME
prints an empty line.
I have also tried to append the export line to ~/.profile, /.profile, ~/.bashrc, and etc/profile, all with the same result. I know that the notebook is operating as root, but I can't seem to be able to get it to see my environment variable. What can I include in my boostrapping script that will remedy this?
I needed to actually export the variable in the script itself.
sudo echo "JYTHON_HOME=/usr/lib/jvm/jython" | sudo tee -a /etc/environment
source /etc/environment
export JYTHON_HOME=/usr/lib/jvm/jython

Why is bash on Ubuntu not loading aliases correctly?

in /etc/profile.d/foo.sh I have:
set -o vi
export ECLIPSE_HOME=/usr/local/eclipse
alias eclipse=${ECLIPSE_HOME}/eclipse
After rebooting my Ubuntu 11.10 32-bit install, and after logging in from the GUI and opening a terminal as a regular user, and type alias eclipse the alias is not defined: bash: alias: eclipse: not found
If I then do sudo su - and then alias eclipse then I correctly get alias eclipse='/usr/local/eclipse/eclipse'
Just to be sure, if I then do (as root) su - myusername and then alias eclipse then I correctly get alias eclipse='/usr/local/eclipse/eclipse'
What gives?
It's better to set aliases in bashrc than in profile. profile is only read if you start a login shell. bashrc is read whenever you start an interactive shell.

Resources