Setting Environment Variables in Linux

Reading Time: 4 minutes

Introduction

Setting up environment and system variables is always of a bit of a deal in Linux. Windows based system have it figured out via GUI in Control Panel. The variables can be set for specific to a user, as well as system-wide.

The same set of variables applies to the Linux as well. In this post, I’ll conclude all the things that I’ve encountered in my previous mistaken experiences and the information that I have derived over the internet.

In this post, I’ll demonstrate how you can define multiple environments namely Ant and Java paths, then we will append the PATH with those.

Additionally, you may find this post a bit comprehensive, and may like to get to the business immediately. I strongly suggest using .bashrc and User Profile for Login methods. On top of that, you may read the rest of the content later to be enlightened about the details.

Common Environment Variables

  • SHELL: This describes the shell that will be interpreting any commands you type in. In most cases, this will be bash by default, but other values can be set if you prefer other options.
  • TERM: This specifies the type of terminal to emulate when running the shell. Different hardware terminals can be emulated for different operating requirements. You usually won’t need to worry about this though.
  • USER: The current logged in user.
  • PWD: The current working directory.
  • PATH: A list of directories that the system will check when looking for commands. When a user types in a command, the system will check directories in this order for the executable.
  • LANG: The current language and localization settings, including character encoding.
  • HOME: The current user’s home directory

Reference[1]

To print the environment values that were set previously, in terminal you can use env or printenv commands

Environment and Shell Variables

Environmental variables are variables that are defined for the current shell and are inherited by any child shells or processes. Environmental variables are used to pass information into processes that are spawned from the shell.

Shell variables are variables that are contained exclusively within the shell in which they were set or defined. They are often used to keep track of ephemeral data, like the current working directory.

By convention, these types of variables are usually defined using all capital letters. This helps users distinguish environmental variables within other contexts.[1]

Specific-User Variable Definitions

The variables will be defined only for a specific user.

User Profile for Login

This applies to the users that have a login to the system. Open the your profile file using nano:

nano ~/.profile

add the below content

export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
export ANT_HOME=/path/to/ant/dir
export PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin

To activate the changes immediately either use source or .

source ~/.profile

Shell Session

This applies to the users that only have a SSH login to the system. Open the your bash profile file using nano:

nano ~/.bash_profile

add the below content

export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
export ANT_HOME=/path/to/ant/dir
export PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin

To activate the changes immediately either use source or .

source ~/.bash_profile

System-wide Variable Definitions

The variables defined this way will be visible not only for users, but anywhere in the whole system.

Using .bashrc

Fortunately, most Linux distributions configure the login configuration files to source the non-login configuration files. This means that you can define environmental variables that you want both inside and the non-login configuration files. They will then be read in both scenarios.

We will usually be setting user-specific environmental variables, and we usually will want our settings to be available in both login and non-login shells. This means that the place to define these variables is in the ~/.bashrc file.[1]

Open the file with nano

nano ~/.bashrc

add the below content

export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
export ANT_HOME=/path/to/ant/dir
export PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin

To activate the changes immediately either use source or .

source ~/.bash_profile

Using /etc/profile

/etc/profile contains Linux system wide environment and startup programs. It is used by all users with bash, ksh, sh shell. Usually used to set PATH variable, user limits, and other settings for user. It only runs for login shell. If you wanted to make large changes or application specific changes use /etc/profile.d/[2]

Open the file with nano

sudo nano /etc/profile

add the below content

export JAVA_HOME=/usr/java/jdk1.5.0_07/bin/java
export ANT_HOME=/path/to/ant/dir
export PATH=$PATH:$JAVA_HOME/bin:$ANT_HOME/bin

To activate the changes immediately either use source or .

source /etc/profile

Using an Independent sh file in profiles

This sort of solution came up on my way while I was looking for an alternative. I fully grabbed it from the Ubuntu forum. I personally vote for this method

Files with the .sh extension in the /etc/profile.d directory get executed whenever a bash login shell is entered (e.g. when logging in from the console or over ssh), as well as by the DisplayManager when the desktop session loads. [3]

Use nano to create the “sh” file

sudo nano /etc/profile.d/java.sh

add the below content

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0
export PATH=$PATH:$JAVA_HOME/bin

To activate the changes immediately either use source or .

source /etc/profile.d/java.sh

Using /etc/environment

This solution is supported in the Ubuntu forum[3] and I have used this way for a quite long time as well. However, in Debian I have seen some difficulties of it as well. While looking solutions out there, I found these really useful Linux Stack exchange posts that enlightened me[4][5]. Let’s look at the definition of this file

/etc/environment is a configuration file for pam_env, not a file read by a shell. The syntax is somewhat similar, but it is not the same. In particular, you can’t refer to existing variables: you’ve set your search path to contain $ORACLE_HOME/bin and $PATH, i.e. directories with a dollar sign in their name.

Open the file with nano

sudo nano /etc/environment

add the below content

export JAVA_HOME=/usr/lib/jvm/jdk1.7.0
export PATH=$PATH:$JAVA_HOME/bin

To activate the changes immediately either use source or .

source /etc/environment

References

  1. How To Read and Set Environmental and Shell Variables on a Linux VPS
  2. https://www.cyberciti.biz/faq/set-environment-variable-linux/
  3. System-wide environment variables
  4. Setting variables in /etc/environment not having an affect but setting them in command line is [duplicate]
  5. Why does environment variable persistence break the PATH var?
  6. How to permanently export a variable in Linux?

Post centos installation after a minimal installation

Reading Time: < 1 minute1. Auto network discovery there is an app called dhclient. It will easily discovery ip address and launch network right away dhclient -v

commandresult2

2.  install Gnome gui

yum -y groupinstall “X Window System” “Desktop” “General Purpose Desktop”

3.start gui on startup

nano /etc/inittab

change id to 5

# Default runlevel. The runlevels used by RHS are: 
#   0 - halt (Do NOT set initdefault to this) 
#   1 - Single user mode 
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking) 
#   3 - Full multiuser mode 
#   4 - unused 
#   5 - X11 
#   6 - reboot (Do NOT set initdefault to this) 
#  id:3:initdefault:

4.installing vmware tools in vmware choose Install VMware tools

su
yum -y install perl
mkdir /mnt/cdrom
mount /dev/cdrom /mnt/cdrom
cd /mnt/cdrom
mkdir /installs
cp VMware*.tar.gz /installs
cd /installs/
tar -zxvf VMware*.tar.gz
cd vmware-tools-distrib
./vmware-install.pl
/usr/bin/vmware-toolbox

restart pc for the default values I kept pressing on entep  r

configure ssl

Reading Time: < 1 minuteExport from JKS to PKCS #12.
keytool -importkeystore -srckeystore keystore.jks -destkeystore keystore.p12 -deststoretype PKCS12 -srcalias <jkskeyalias> -deststorepass <password> -destkeypass <password>
mv keystore.p12 /root
cd /root

convert to key
openssl rsa -outform der -in private.pem -out private.key

nano /etc/httpd/conf.d/ssl.conf

find the line <VirtualHost _default_:443>

uncomment ServerName and type servername:443

uncomment:
SSLEngine on

SSLCertificateFile /root/downloadedcertificate.crt

SSLCertificateKeyFile /root/private.key

save and exit
restart httpd

service httpd restart

then test

ssl2

then

Configure SSL on Apache and Glassfish and forward traffic to Glassfish

Reading Time: 2 minuteskeytool -keysize 2048 -genkey -alias tugrulaslan.net -keyalg RSA –dname “CN=tugrulaslan.net,O=company,L=Istanbul,S=Istanbul,C=TR” -keystore keystore.jks

keytool –certreq –alias tugrulaslan.net –keystore keystore.jks –file cert_req.csr

keytool -import -alias tugrulaslan.net -keystore keystore.jks -trustcacerts -file yourdomain.com.crt

send that csr to generate your ssl file then download x509 certificate from geotrust

then move the certificate to /root

IMPORT SSL INTO KEYSTORE

 

download x509 format

and save first server certificate as root.crt

save last intermediate.crt and transfer all files to your ftp

then copy them to domain folder

then

keytool -import -alias intermediate -keystore keystore.jks -trustcacerts -file intermediate.crt

keytool -import -alias root -keystore keystore.jks -trustcacerts -file root.com.crt

keytool -import -alias yourdomain -keystore keystore.jks -trustcacerts -file yourdomain.com.crt

then nano domain.xml

here change all s1as as your domain name and fire  up your server

export key file

keytool -importkeystore -srckeystore keystore.jks -destkeystore keystore.p12 -deststoretype PKCS12 -srcalias jkskeyalias  -deststorepass password -destkeypass password

openssl pkcs12 -in keystore.p12  -nokeys -out cert.pem
openssl pkcs12 -in keystore.p12  -nodes -nocerts -out key.pem
convert pem to key
openssl rsa -outform der -in private.pem -out private.key

then move the key file to /root for security

edit /etc/httpd/conf.d/ssl.conf below

<VirtualHost _default_:443>

ServerName tugrulaslan.net
SSLProxyEngine on
ProxyPreserveHost On
ProxyPass / https://localhost:8181/
ProxyPassReverse / https://localhost:8181/

SSLEngine on

SSLCertificateFile /root/X509CERTIFICATE FROM GEOTRUST.crt

SSLCertificateKeyFile /root/private.key(EXPORTED FROM KEYTOOL)

</VirtualHost>

sslapacheglassfish

 

if you only want https when http request is received then do the following in /etc/httpd/conf/httpd.conf

<VirtualHost *:80>
   ServerName <span class="highlight">example.com</span>
   Redirect permanent / https://example.com/
</VirtualHost>

add firewall

<tt class="COMMAND">iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT</tt>
<tt class="COMMAND">iptables -A OUTPUT -p tcp -m tcp --dport 433 -j ACCEPT 

service iptables save

service iptables restart</tt>
<tt class="COMMAND"></tt>

Apache Mod_proxy to forward http traffic to Java App server 8080

Reading Time: < 1 minuteIn my vps server I wanted to forward http port 80 to glassfish 8080 ports without having glassfish config changed. So I assume httpd apache server is installed. So

cd /etc/httpd/conf

nano httpd.conf

Add the below lines and save and restart apache

<VirtualHost *:80>
# Your domain name
ServerName Domain_NAME_HERE

ProxyPreserveHost On


ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/

# The location of the HTML files, and access control information
DocumentRoot /var/www
<Directory /var/www>
Options -Indexes
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

Result

apacheforward

Source: https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6/html/Administration_and_Configuration_Guide/Install_the_Mod_proxy_HTTP_Connector_Into_Apache_HTTPD.html

 

Installing Oracle Java on Linux Manually

Reading Time: 2 minutes

Description

In this article we will install Oracle’s Java, besides the OpenJDK can be shipped with the package managers in the distros. This way will allow you to end-to-end fully install Oracle JDK. The version I used was against version 8, but you can also use the same way for the other versions as well

Specifications

Debian 10 64 Bit

Oracle Java 8 Update 261

Steps

1. Download the Java from the Oracle’s web site https://www.oracle.com/java/technologies/javase/javase8u211-later-archive-downloads.html

2. Unpack the tar file

tar zxvf jdk-8u261-linux-x64.tar.gz

3. Create a folder in the usr location

sudo mkdir /usr/java

4. Move all the files from the unzipped location to the newly created folder

sudo mv jdk1.8.0_261/ /usr/java

5. Install all the java commands using the alternatives

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/java/jdk1.8.0_261/bin/java" 1

sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/java/jdk1.8.0_261/bin/javac" 1

sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/java/jdk1.8.0_261/bin/javaws" 1

sudo update-alternatives --install /usr/bin/jar "jar" "/usr/java/jdk1.8.0_261/bin/jar" 1

6. Fix the permissions for the Java entries

sudo chmod a+x /usr/bin/java 
sudo chmod a+x /usr/bin/javac 
sudo chmod a+x /usr/bin/javaws
sudo chmod a+x /usr/bin/jar
sudo chown -R root:root /usr/java/jdk1.8.0_261

7. Add Java home to the Global Environment

sudo nano /etc/profile.d/java.sh

7.1. And add the below line and save

export JAVA_HOME=/usr/java/jdk1.8.0_261 
export JRE_HOME=$JAVA_HOME/jre 
export PATH=${JAVA_HOME}/bin:${PATH}
export PATH=${JRE_HOME}/bin:${PATH}

8. Make the file executable and Reload the “sh” file in the system

chmod +x /etc/profile.d/java.sh
source /etc/profile.d/java.sh

9. Check the installation for Java

java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)

10. Check the installation for Java Compiler

javac -version
javac 1.8.0_261

11. Check the installation for Java Web Start

javaws -version
Java(TM) Web Start 11.261.2.12-fcs

11. Check the installation for Jar

jar
Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config javaws
sudo update-alternatives --config jar

Installing and configuring tomcat on Centos 7

Reading Time: < 1 minutewget http://www.us.apache.org/dist/tomcat/tomcat-7/v7.0.57/bin/apache-tomcat-7.0.57.tar.gz

tar -xvvf apache-tomcat-7.0.57.tar.gz

mv apache-tomcat-7.0.57 /opt/tomcat

nano /opt/tomcat/conf/tomcat-users.xml

find the <tomcat-users> </tomcat-users> lines and in between add the below entry

 <role rolename="manager-gui"/>
 <role rolename="manager-script"/>
 <role rolename="manager-jmx"/>
 <role rolename="manager-status"/>
 <role rolename="admin-gui"/>
 <role rolename="admin-script"/>
 <user username="admin" password="admin" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-gui,admin-script"/>

and save the file and then issue command

chmod 755 /opt/tomcat
sh /opt/tomcat/bin/startup.sh