006 - Linux Essentials for DevOps: Digging into Environment Variables, Networking, SSH

Hamza IqbalHamza Iqbal
6 min read

Environment Variables

Environment variables in Linux function similarly to variables in programming languages, but they are defined at the system level, allowing them to be accessed globally across different programs and scripts. This is particularly useful for setting configuration options or sensitive information like API keys, which need to be accessed by multiple applications.

Every Linux system comes with a set of predefined environment variables, such as the username and home directory. You can view these variables using the printenv command. For example, to see all environment variables, simply type:

printenv

To set an environment variable manually, you can use the export command. For instance, to set a variable named MY_VAR with the value 123, you would use:

export MY_VAR=123

You can then verify that the variable is set by using the echo command:

echo $MY_VAR

However, a limitation of setting environment variables this way is that they are only available in the current shell session. Once you close the terminal, these variables are lost. To make environment variables persistent across sessions, you can add them to your .bashrc file, which is executed every time a new shell session is started. For example, add the following line to your ~/.bashrc file:

export MY_VAR=123

After editing .bashrc, run source ~/.bashrc to apply the changes immediately.

For system-wide environment variables that should be available to all users, you can define them in the /etc/environment file. This file is used to set variables that are available to all users and processes. For example, to add a new path to the PATH variable, you can edit /etc/environment and include:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/new/path"

Remember to use caution when editing system files like /etc/environment, as incorrect configurations can affect all users on the system.

Networking

In a DevOps context, understanding networking concepts is crucial for managing and deploying applications efficiently. Here's a brief overview of key networking components and how they interconnect:

  1. LAN (Local Area Network): A LAN is a network that connects computers within a limited area, such as an office or building. It enables devices to communicate and share resources like files and printers. In a DevOps environment, LANs facilitate collaboration and resource sharing among development and operations teams.

  2. Switch/Router: Switches and routers are networking devices that direct data traffic. A switch connects devices within a LAN, while a router connects multiple networks, such as a LAN to the internet. In DevOps, routers and switches ensure seamless communication between different network segments and external services.

  3. IP Address & Ranges: An IP address is a unique identifier for a device on a network. IP ranges define the scope of addresses available within a network. DevOps teams use IP addresses to configure network interfaces and manage access to services. For example, a server might have an IP address like 192.168.1.10 within a private range.

  4. NAT (Network Address Translation): NAT is a technique used to map private IP addresses to a public IP address, allowing devices on a LAN to access external networks. In DevOps, NAT is essential for enabling internet access for internal services while maintaining network security.

  5. Firewall: A firewall is a security device that monitors and controls incoming and outgoing network traffic based on predetermined rules. DevOps teams configure firewalls to protect applications and data from unauthorized access and cyber threats.

  6. Port: A port is a communication endpoint used by network protocols to identify specific processes or services. In DevOps, managing ports is crucial for ensuring that applications can communicate over the network. For example, web servers typically use port 80 for HTTP traffic.

  7. DNS (Domain Name System): DNS translates human-readable domain names into IP addresses, allowing users to access websites using easy-to-remember names. DevOps teams configure DNS settings to ensure that applications are accessible via domain names.

  8. Domain & Subdomains: Domains are unique names that identify websites, while subdomains are extensions of domains used to organize and navigate different sections of a website. In DevOps, managing domains and subdomains is essential for structuring web applications and services.

  9. Basic Networking Commands:

    • ifconfig: Displays network interface configurations, useful for troubleshooting network issues.

    • netstat: Shows network connections, routing tables, and interface statistics, helping DevOps teams monitor network activity.

    • ps aux: Lists running processes, aiding in identifying network-related processes.

    • nslookup: Queries DNS to obtain domain name or IP address mapping, useful for verifying DNS configurations.

    • ping: Tests connectivity between devices, helping diagnose network connectivity issues.

These networking concepts and tools are interconnected, forming the backbone of a robust DevOps infrastructure. Understanding and managing them effectively ensures smooth deployment and operation of applications.

SSH

SSH (Secure Shell) is a critical concept in server management, providing a secure method for connecting to and managing remote servers. It allows users to establish a secure connection to a server, either through a username and password or, more securely, by using a pair of cryptographic keys.

To enhance security, SSH connections often use public key authentication. This involves generating a pair of keys: a public key and a private key. The public key is stored on the server in the ~/.ssh/authorized_keys file, while the private key remains on the client machine. This method eliminates the need for password authentication, reducing the risk of unauthorized access.

To set up SSH key-based authentication, you can generate a key pair using the following command:

ssh-keygen -t rsa

This command creates a private key and a corresponding public key. You then copy the public key to the server's ~/.ssh/authorized_keys file. Once this setup is complete, you can connect to the server without being prompted for a password.

For SSH connections to work, ensure that the server's firewall allows traffic on port 22, the default port for SSH. This is crucial for establishing a successful connection.

Additionally, to transfer files between your local machine and the server, you can use the scp (Secure Copy Protocol) command. For example, to copy a file named example.txt from your local machine to the server, you would use:

scp example.txt user@server:/path/to/destination

By understanding and implementing SSH, you can securely manage and interact with remote servers, ensuring both convenience and security in your DevOps practices.

I hope this article helps you understand Linux environment variables, networking concepts, and SSH. Feel free to reach out to me if you have any questions.

Summary

This article covers the essentials of Linux environment variables, networking concepts, and SSH for secure server management. It explains how environment variables are used for system-wide configurations and how to make them persistent. Key networking components are outlined, including LAN, switches, routers, IP addresses, NAT, firewalls, and DNS. Additionally, it discusses basic networking commands and their role in maintaining a DevOps infrastructure. The article also details the setup and benefits of SSH, emphasizing secure connections and file transfers in server management.

0
Subscribe to my newsletter

Read articles from Hamza Iqbal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hamza Iqbal
Hamza Iqbal

Hi, Hamza Iqbal here. I'm a MERN Stack developer with 3+ years of experience. I'm a tech enthusiast who love to learn new skills and read tech related news. Currently, I'm learning DevOps.