If these variables need to be set for every account, the simpler would be to add the export statements to /etc/profile
. This file is not root's profile but a file sourced for every account using an interactive Bourne shell family shell (sh, ksh, bash, ...)
You can then just add at the end of this file.
export ORACLE_HOME=/oracle/product/11.1.0/db_1/
export ORACLE_SID=orcl
export TESTFOLDER_DIR=/test/testfolder/bin/
Otherwise, just add these three lines in your shell startup file (likely one of $HOME/.profile
or $HOME/.bash_profile
).
If you are logging in as root (which is not a good practice), that would be /.profile
or /root/.profile
Finally, should you want to unconditionally set these variables whatever the shell is used and even for non interactive login stuff, like services, you can use the /etc/default/init
file. Note that the export
builtin is not required in this file as it is not a real shell script but really a configuration file, eg:
ORACLE_HOME=/oracle/product/11.1.0/db_1/
ORACLE_SID=orcl
TESTFOLDER_DIR=/test/testfolder/bin/
Normally systemd
services have only a limited set of environment variables,
and things in /etc/profile
, /etc/profile.d
and bashrc
-related files are not set.
To add environment variables for a systemd
service you have different possibilities.
The examples as follows assume that roscore
is at /opt/ros/kinetic/bin/roscore
, since systemd
services must have the binary or script configured with a full path.
One possibility is to use the Environment
option in your systemd
service and a simple systemd
service would be as follows.
[root@localhost ~]# cat /etc/systemd/system/ros.service
[Unit]
Description=ROS Kinetic
After=sshd.service
[Service]
Type=simple
Environment="One=1" "Three=3"
Environment="Two=2"
Environment="Four=4"
ExecStart=/opt/ros/kinetic/bin/roscore
[Install]
WantedBy=multi-user.target
You also can put all the environment variables into a file that can be read with the EnvironmentFile
option in the systemd
service.
[root@localhost ~]# cat /etc/systemd/system/ros.env
One=1
Three=3
Two=2
Four=4
[root@localhost ~]# cat /etc/systemd/system/ros.service
[Unit]
Description=ROS Kinetic
After=sshd.service
[Service]
Type=simple
EnvironmentFile=/etc/systemd/systemd/ros.env
ExecStart=/opt/ros/kinetic/bin/roscore
[Install]
WantedBy=multi-user.target
Another option would be to make a wrapper script for your ros
binary and call that wrapper script from the systemd
service. The script needs to be executable. To ensure that, run
chmod 755 /opt/ros/kinetic/bin/roscore.startup
after creating that file.
[root@localhost ~]# cat /opt/ros/kinetic/bin/roscore.startup
#!/bin/bash
source /opt/ros/kinetic/setup.bash
roscore
[root@localhost ~]# cat /etc/systemd/system/ros.service
[Unit]
Description=ROS Kinetic
After=sshd.service
[Service]
Type=simple
ExecStart=/opt/ros/kinetic/bin/roscore.startup
[Install]
WantedBy=multi-user.target
Note that you need to run systemctl daemon-reload
after you have edited the service file to make the changes active. To enable the service on systemboot, you have to enter systemctl enable ros
.
I am not familiar with the roscore
binary and it might be necessary to change Type=
from simple
(which is the default and normally not needed) to forking
in the first two examples.
For normal logins, you could copy or symlink /opt/ros/kinetic/setup.bash
to /etc/profile.d/ros.sh
, which should be sourced on normal logins.
Best Answer
You can add it to the file
.profile
or your login shell profile file (located in your home directory).To change the environmental variable "permanently" you'll need to consider at least these situations:
bash
/etc/profile
,~/.bash_profile
,~/.bash_login
,~/.profile
in the order~/.bashrc
$BASH_ENV
zsh
ksh
bourne
csh or tcsh
If you want to make it permanent for all users, you can edit the corresponding files under
/etc/
, i.e./etc/profile
for Bourne-like shells,/etc/csh.login
for (t)csh, and/etc/zsh/zprofile
and/etc/zsh/zshrc
for zsh.Another option is to use
/etc/environment
, which on Linux systems is read by the PAM modulepam_env
and supports only simple assignments, not shell-style expansions. (See Debian's guide on this.)These files are likely to already contain some assignments, so follow the syntax you see already present in your file.
Make sure to restart the shell and relogin the user, to apply the changes.
If you need to add system wide environment variable, there's now
/etc/profile.d
folder that contains sh script to initialize variable.You could place your sh script with all you exported variables here.
Be carefull though this should not be use as a standard way of adding variable to env on Debian.