Intro to Environment Variables and SSH...
Tags: beginners, devops, linux, ssh
Published: 2022-03-22 11:17:38

**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.

![environment-variables-on-linux.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1647947375193/z9vHvH82G.png)

**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` =&gt; prints all the environment variables.

`printenv <Env. variables>` =&gt; 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` -&gt; This is available all over the environment and not unlike bash variables which are available in the script.

For example,

```bash
export DB_USERNAME=dbuser
export DB_PASSWORD=secretpwdvalue
export DB_NAME=mydb
```

#### Deleting Environment Variables

`unset (env.) variable_name=value`

For example,

```bash
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.
        
* 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

```bash
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

```bash
source ~/.bashrc
```

#### Persisting Environment Variables System-wide

* To add for all users there is a configuration file in Linux in the home folder.
    

```bash
/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.

![ssh.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1647947417330/krtRp1JbL.png)

#### 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

1. **Username** and **Password**.
    

* Admin creates a user on the remote server and the user can then connect with the username and password.
    

1. **SSH key pair** *(more secure way)*.
    

* The client creates an SSH key pair.
    

```bash
key-pair = Private key + Public key
```

* `Private Key` =&gt; Secret key which is stored securely on the client machine.
    
* `Public Key` =&gt; 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)

```bash
ssh username@SSHserver(IP address)
```

For example,

```bash
ssh root@159.89.14.94
```

#### Generate SSH key pair

`mkdir .ssh/` =&gt; create a directory for storing keys*(if not there)*.

```bash
ssh-keygen -t rsa
```

* This generates ssh key pairs.
    
* Here, `-t` means type and `rsa` means the method of encryption.
    

`~/.ssh` =&gt; .ssh folder under the home directory is the default location for your ssh key pair.

`id_rsa` =&gt; **Private** key.

`id_rsa.pub` =&gt; **Public** key.

#### Copy files to the remote server

`scp`*(secure copy)* =&gt; allows you to securely copy files and directories.

For example,

```bash
scp test.sh root@159.89.14.94:/root
```