Setting environment variables for no-login users - linux

I have an RHEL server with tomcat installed. Tomcat runs as a no-login user called tomcat . I have set the required environment variables in /etc/profile.d/myenvvars.sh as
export JRE_HOME=/usr/lib/jvm/jre
export MY_VAR=/usr/share/mydir
The environment variables are set and can be echoed in the terminal using
# echo $MY_VAR
# sudo -u tomcat echo $MY_VAR
However when tomcat starts my environment variable is not recognised by tomcat.
As per this article I found that my environment variables will not be recognised when tomcat starts as tomcat is a no-login user. Therefore I sourced the above file in ~/.bash_profile using
. /etc/profile.d/myenvvars.sh
However, I still have the same issue, the environment variable is not reccognised.
Any help would be appreciated.

As stated in the article your linked your settings need to be in ~/.bashrc.
There may be a chance that even this is not working... depending on how your tomcat is started.
You could have a custom script for tomcat to make sure it loads the environment variables e.g. something like this
tomcat-start.sh
. /etc/profile.d/myenvvars.sh
tomcat

Related

Set Environmnet Path Node.JS

I need to set environment variable for my NodeJS application. I'm using OS W10. I tried wrote in CMD
SET PORT=5000
When i wrote SET in CMD i saw value PORT=5000, but if i closed CMD and opend it one again and wrote again SET PORT i got message: Environment variable port not defined.
And if i try to get data from Node.JS application via command process.env.PORT i got value undefined. Do you have anybody experience with this isssue ?
Thanks.
An environment variable set in a Windows 10 command shell persists only for the lifetime of that command shell and is only seen in that command shell (or in child processes).
So, if you want to set an environment variable in the command shell for your node.js app, you have to set it in that very command shell that you're about to run your node.js app from. This can either be done manually or you can create a batch file that sets the environment variable and then runs your node.js program.
If you want to set a persistent environment variable that is automatically available in all future command shells, then you can go into Windows settings and modify the default environment that is passed to all new command shells. Here are some steps to do that:
Open Windows Settings
Type environment in the search box in the settings window.
If you are logged in as an administrator, you should get a drop-down that contains two options, edit system environment variables or edit environment variables for your account. Pick the desired choice.
A dialog comes up where you can then edit, add or delete persistent environment variables.
SET only sets the environment setting for current session, once closed you loose it. In order to get PORT value using process.env.PORT, you need to set PORT first then run your server within same CMD window.
If you are using BASH (Git Bash) you can do it using one line
PORT=5000 node server.js
I highly recommend using DOTENV package to manage your Environment Variables.

Ubuntu bug? Environment variables only after logging as root

I have an IP address that I use very often, so I tried to set it as an environment variable now that I installed Ubuntu. I edited /etc/environment and added a couple of lines for my api token and my IP address. It looks like this:
PATH="some/paths"
TOKEN="my:token"
ZRUS="my.ip.address"
Now if I want to access the IP I would in theory do ssh $ZRUS. However, it does not work; I do echo $ZRUS and I get a blank line, so I do printenv and I get a list of all the environment variables and I don't see my IP there. I then do su root and I do printenv again and I get the same list plus the IP address and the TOKEN. I then do su myuser and do echo $ZRUS and magically the IP works.
Now I'm wondering why I have to log in as root first to get my global environment variables to work in the local user. It seems as if the scope varied depending on whether root has had a go on the session or not, which seems strange to me.
Do you guys think this is a bug or a functionality? And how would you overcome this?
Environment variables set in an environment file /etc/environment will only take effect if you read them into your active shell source /etc/environment or you logout/login (which will re-read the environment files into your active shell).
The act of su myuser is essentially creating a new shell for your current user which re-reads the environment file

How to change Jenkins home directory (~/.jenkins) when there is no jenkins installed

I never installed Jenkins on my server. All I have done was downloading .war file and tomcat is hosting it. By default it made a directory under my home directory (~/.jenkins) which Jenkins using for its own config and parameters.
Is there anyway to change the directory that Jenkins is pointing to? I need to move this directory.
I searched for that a lot but people all gave a solution for the case that Jenkins is installed already.
The server is running 'Red Hat Enterprise Linux Server release 6.7', and Jenkins version is 1.642.4.
You can easily change the JENKINS_HOME location by adding an environment entry in the context container used by tomcat for the jenkins webapp (typically in /etc/tomcat[VERSION]/Catalina/localhost/[context-name].xml), like this:
<Context (..)>
(..)
<Environment name="JENKINS_HOME" value="[your jenkins home]" type="java.lang.String"/>
</Context>
In case of Apache Tomcat you can specify startup parameters, including JENKINS_HOME for specifying Jenkins directory, in $CATALINA_BASE/bin/setenv.sh file:
$ cat setenv.sh
CATALINA_PID="${CATALINA_BASE}/tomcat.pid"
CATALINA_OPTS="-Duser.language=en -Duser.country=US -Djava.awt.headless=true -DJENKINS_HOME=${JENKINS_HOME} -Xms1024m -Xmx8192m -Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true"
In above example ${JENKINS_HOME} is set an environment variable, but you can replace it by absolute path.
Please refer to Apache Tomcat documentation for more details.

Setting path variable for apache user on Amazon EC2

I can't add /usr/local/bin to the apache users PATH variable. The user doesn't have a .profile, I can't su to the user, I can't export to the PATH from php using exec and adding
SetEnv PATH /usr/local/bin
To either the http.conf or the .htaccess file doesn't make a difference. I can't find the envvars file to change that but I suspect there's some other problem.
I have restarted apache, and indeed my server.
Ended up following what Alfe suggested in his answer, except rather than in the /etc/init.d/httpd file (which could be overwritten easily on update) I added to /etc/sysconfig/httpd:
export PATH=${PATH:+$PATH:}/usr/local/bin
Have a look at the /etc/passwd to see which login shell the apache user has (on EC2 Ubuntu instances it should be /bin/sh which is a link to /bin/dash). Then have a look at the man page of that shell and find out which configuration files are read upon login. (For /bin/dash that would be .login in the user's home directory.) In those you should be able to extend your $PATH as you like.
EDIT:
Since you seem to have no login shell for that user: Have a look at the /etc/init.d/* scripts which start the system services. Apache will be one of them. They are started as root and may change the current user (e. g. to the apache user). In there you might be able to adjust the PATH as you like it.
Patching those scripts, however, is not considered typical configuration. Updates might overwrite what ever you patch there.

Jenkins ignores proxy settings while building a job

I set a proxy under Plugins in Jenkins like suggested online.
I also edited the /etc/environment
bash-3.2$ cat /etc/environment
http_proxy=proxy.company.net:8080
https_proxy=proxy.company.net:8080
HTTP_PROXY=proxy.company.net:8080
HTTPS_PROXY=proxy.company.net:8080
HTTPS_PROXY_REQUEST_FULLURI=false
HTTP_PROXY_REQUEST_FULLURI=false
I verfied the variables and they are available on logon.
When I start ant manualy as root via ssh, my "composer.phar" script is able to connect and download files. As soon as Jenkins starts the job (I think its the "jenkins" linux user), he waits until timeout and aborts the build. I used "su jenkins -s /bin/bash" to get a shell as "jenkins" and the env-vars are set correctly...
What can I do? Why does Jenkins ignore these ENV-Vars?
Thanks.
The http_proxy variables (as seen e.g. on the wget man page) require a "http://" prefix to work properly for many programs.
Jenkins on the other hand has a proxy configuration at Manage Jenkins > Plugin Manager > Advanced. This configuration overrides the environment variables.
Check Alex' answer to another question for getting around this behavior for individual nodes/builds.
I did not get it solved. After a restart the server fails all Jenkins Jobs for some minutes... suddenly the connection to the proxy succeeds and everything works well.

Resources