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

Creating and converting an ssl certificate in numerous formats

Reading Time: < 1 minuteCreating a key file 

openssl genrsa -des3 -out tugrul.key 1024

Creating CSR(Certificate Signing Request) file

openssl req -new -key tugrul.key -out tugrul.csr

Creating a CRT  Self-Signed SSL Certificate

openssl x509 -req -days 365 -in tugrul.csr -signkey tugrul.key -out tugrul.crt

Converting a Certificate File to PKCS#12(PFX) Format

 openssl pkcs12 -export -out tugrul.p12 -inkey tugrul.key -in tugrul.crt -certfile tugrul.crt

for pfx format alter “-out tugrul.p12” as “-out tugrul.pfx ”

Converting .crt file into the Microsoft .cer format

Double Click the crt file and Skip to the Details tab

ssl1

then choose the format you wish to be exported as

ss2

 

LAMP Stack on Windows

Reading Time: 2 minutes

Apache 2.4 Installation

Download Apache 2.4.16 win32 from Apache lounge – http://www.apachelounge.com/download/

Unzip entire content to C: and keep the folder name as given

Visual C++ Redistributable for Visual Studio 2015 package for Win32 and install it – https://www.microsoft.com/en-us/download/details.aspx?id=48145

Go to unzipped Apache folder –> conf edit httpd.conf and edit those lines;

Listen *:80

remove # from the line of  LoadModule rewrite_module modules/mod_rewrite.so

AllowOverride All

run Command line and go to Apache2.4 bin folder

then issue httpd -t

you will see Syntax OK message

then run the apache as service — but run the command line as admin!

httpd -k install

issue services.msc command in run and start apache server and go to localhost and see the landing page then voila if all works out1

PHP 5.6 Installation

Download Php 5.6 threadsafe win32, http://windows.php.net/download#php-5.6

make sure you install win32 thread safe package(includes apache php lib file) and x86 one not X64 otherwise it does fail for some reason! e.g – php-5.5.18-Win32-VC11-x86.zip

unzip everything in C folder php

then go to the folder and rename php.ini-development to php.ini

open php.ini file, find and alter those lines

this is important make sure you enter the correct path and its not backslashes!!

extension_dir = "C:/php/ext"

uncomment necessary extensions

extension=php_bz2.dll
extension=php_curl.dll
;extension=php_fileinfo.dll
extension=php_gd2.dll
;extension=php_gettext.dll
;extension=php_gmp.dll
;extension=php_intl.dll
;extension=php_imap.dll
;extension=php_interbase.dll
;extension=php_ldap.dll
extension=php_mbstring.dll
;extension=php_exif.dll      ; Must be after mbstring as it depends on it
extension=php_mysql.dll
extension=php_mysqli.dll
;extension=php_oci8_12c.dll  ; Use with Oracle Database 12c Instant Client
;extension=php_openssl.dll
;extension=php_pdo_firebird.dll
;extension=php_pdo_mysql.dll
;extension=php_pdo_oci.dll
;extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
;extension=php_pdo_sqlite.dll
;extension=php_pgsql.dll
;extension=php_shmop.dll

add php folder to your environment variable

041-installing-php

reopen apaches httpd conf file and add index.php

DirectoryIndex index.php index.html

and add the below line to the end of file

# PHP5 module
LoadModule php5_module "c:/php/php5apache2_4.dll"
AddType application/x-httpd-php .php
PHPIniDir "C:/php"

then issue httpd -t to make sure syntax is correct

then start the service in services.msc

Mysql Installation

Download the mysql community edition + workbench bundle full stack — https://dev.mysql.com/downloads/windows/installer/5.6.html

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