What are environment variables?
Each user has their own environment. Each user can configure their own environment/account by setting preferences.
These OS configurations should be isolated from other user environments.
Where does OS store all these configurations?
These are stored in environment variables which are just KEY = value pairs
Variables that store information. By convention, names are defined in UPPERCASE. For example, SHELL=/bin/bash
(Default shell program of the user)
Users can change these environment variable values. For example, SHELL=/bin/zsh
- Variables are variables, which means they can be changed.
Commands for environment variables
printenv
=> prints all the environment variables.
printenv <Env. variables>
=> prints the provided environment variable's name.
Referencing Environment Variable
- Using the
$
sign. For example,$USER
Use cases of Environment Variables
OS stores information about the environment.
We can create our own environment variables.
Creating Environment Variables
export (env.) variable_name=value
-> This is available all over the environment and not unlike bash variables which are available in the script.
For example,
export DB_USERNAME=dbuser
export DB_PASSWORD=secretpwdvalue
export DB_NAME=mydb
Deleting Environment Variables
unset (env.) variable_name=value
For example,
unset DB_NAME
NOTE:
These are only available for the current session*(temporary env. variable) and* after exiting the terminal the env. variable created is removed.
Persisting Environment Variables
This is user-specific.
-
There are per-user shell-specific configuration files.
- For example, if you are using BASH, you can declare the variables in the
~/.bashrc
file.
- For example, if you are using BASH, you can declare the variables in the
Variables set in this file are located whenever a bash login shell is entered.
To add these variables, open the .bashrc
file and at the last entry
export DB_USERNAME=dbuser
export DB_PASSWORD=secretpwdvalue
export DB_NAME=mydb
Now, after reopening the terminal, the environment variables will be there as they are saved on the system.
To load the new environment variables into the current shell session
source ~/.bashrc
Persisting Environment Variables System-wide
- To add for all users there is a configuration file in Linux in the home folder.
/etc/environment
PATH environment variable
These are lists of directories to executable files, separated by
:
(colon)This tells the shell which directories to search for the executable in response to our executed command.
PATH = $PATH:.....
You need to provide an absolute location to make that file available.
It adds our custom app/command to be available for the user(if configured in
.bashrc
) or for all users (if configured in/etc/environment
)
SSH - Secure Shell
It is a network protocol that gives users a secure way to access a computer over the internet. SSH also refers to the suite of utilities that implement that protocol.
Some Use Cases
Copy the file to the remote server.
Install the software on a new server.
2 ways to authenticate with the remote server
- Username and Password.
- Admin creates a user on the remote server and the user can then connect with the username and password.
- SSH key pair (more secure way).
- The client creates an SSH key pair.
key-pair = Private key + Public key
Private Key
=> Secret key which is stored securely on the client machine.Public Key
=> This can be shared with the remote server.The client machine for that public key can safely connect.
The client can "unlock" the public key with his private key.
NOTE: If the public key of a person is not registered on the remote server, he/she will not be able to connect to it.
SSH for services
Services, like Jenkins, often need to connect to another server via SSH.
Create a Jenkins user on the application server.
Create SSH key pair on Jenkins server.
Add public SSH key to
authorized_keys
on the application server.
Connect via SSH(Password Authentication)
ssh username@SSHserver(IP address)
For example,
ssh root@159.89.14.94
Generate SSH key pair
mkdir .ssh/
=> create a directory for storing keys*(if not there)*.
ssh-keygen -t rsa
This generates ssh key pairs.
Here,
-t
means type andrsa
means the method of encryption.
~/.ssh
=> .ssh folder under the home directory is the default location for your ssh key pair.
id_rsa
=> Private key.
id_rsa.pub
=> Public key.
Copy files to the remote server
scp
(secure copy) => allows you to securely copy files and directories.
For example,
scp test.sh root@159.89.14.94:/root
Top comments (0)