Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 22, 2022 11:17 am GMT

Environment Variables & SSH

Environment Variables.

What are environment variables?

  • Each user has its own environment.
  • Each user can configure its own environment/account by setting preferences.
  • These OS configurations should be isolated from other user environments.

environment-variables-on-linux.png

Where does OS store all these configurations?

  • Environmental Variables -> KEY = value pairs
  • Variables store information.
  • By convention, names are defined in UPPERCASE.Eg; SHELL=/bin/bash (Default shell program(location) of the user)
  • User can change these environment variable values.Eg; SHELL=/bin/zsh
  • Variables are variables, which means they can be changed.

List all Environment Variables:-

  • printenv -> prints all the environment variables.

Print Specific Environment Variable:-

  • printenv <Env. variables> -> prints the provided environment variable's information.

Referencing Environment Variable:-

  • Using the $ sign.Eg; $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 & not like bash variables which are available in the script.

For Eg;

export DB_USERNAME=dbuserexport DB_PASSWORD=secretpwdvalueexport DB_NAME=mydb

Deleting Environment Variables:-

  • unset (env.) variable_name=value

For Eg;

unset DB_NAME

Note:-

  • Available for the current session(temporary env. variable)
  • After exiting the terminal the env. variable created is removed.

Persisting Environment Variables:-

  • This is user-specific.

Shell specific configuration file;

  • per-user shell-specific configuration files.
  • Eg-> 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 & at the last enter:

export DB_USERNAME=dbuserexport DB_PASSWORD=secretpwdvalueexport 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:-

  • list of directories to executable files, separated by :(colon)
  • 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 available that file.
  • adds our custom app/command to available for user*(if configured in .bashrc)* or for all user*(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

Some Use Cases:-

  • Copy file to the remote server.
  • Install the software on a new server.

2 ways to authenticate with the remote server:-

1) Username & Password.

  • Admin creates a user on the remote server
  • User can then connect with the username & password.

2) 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, e.g., with the remote server.

  • Client machine for that public key can safely connect.

  • 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 cannot 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 Eg;
ssh [email protected]

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 & rsa means the method of encryption.

~/.ssh -> .ssh folder under 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 & directories.

For Eg;
scp test.sh [email protected]:/root


Original Link: https://dev.to/kcdchennai/environment-variables-ssh-199n

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To