laitimes

DevOps minimum — Linux

author:Venucia 8
DevOps minimum — Linux

This is my opinion that being a DevOps engineer should at least know Linux.

1. Linux file system and structure

Root: Understand the importance and structure of the root (/) directory and subdirectories such as /etc/, /var/, /home/, and /bin/.

File types: Identify standard file types such as general files, directories, symbolic links, and device files.

Path and PATH variables: Absolute and relative paths can be used whenever browsing the file system. You can use the pwd command to find out where you are:

pwd
# output
/Users/flavius/Workspace/example

ls
# files/directories in the current directory
a   b   c   dir           

For example, if I want to go to the dir directory, I can use an absolute path or a relative path:

# Relative Path
cd dir

# Absolute Path
cd /Users/flavius/Workspace/example/dir           

The PATH variable is not a relative or absolute path, it is a critical environment setting in Linux that determines where the system searches for executables.

# Example setting of the PATH variable
export PATH=$PATH:/new/directory/path           

File permissions: Master the concepts of users, groups, and other permissions, as well as commands such as chmod, chown, and chgrp. Before getting into the examples of these commands, let's dive into permissions.

ls -la

drwxr-xr-x    6 flavius  staff   192 Aug 27 15:42 .
drwxr-xr-x  127 flavius  staff  4064 Aug 27 15:42 ..
-rw-r--r--    1 flavius  staff     0 Aug 27 15:42 a
-rw-r--r--    1 flavius  staff     0 Aug 27 15:42 b
-rw-r--r--    1 flavius  staff     0 Aug 27 15:42 c
drwxr-xr-x    2 flavius  staff    64 Aug 27 15:42 dir           

By using the ls -la option, you can easily find all files and directories in the current directory, including hidden files and directories, and their permissions. Their permissions are those that start at the beginning of the line. If the permission starts with "d", it means that the structure is a directory. Let's see what the other letters stand for: "r" → read, "w" → write, "x" → execute.

Let's look at an example and analyze the permissions of the a file: rw-r--r--。 As you can see, there are 9 characters in it.

The first three are the permissions that the file owner has: rw-

The next three are the permissions of the group to which the file owner belongs: r--

The last three are permissions that other users have: r--

Now, be patient as we will involve some simple math to make it easier to change permissions. If you see a letter in one of these 3 places, it means that a bit is enabled and has a value of 1 (binary), otherwise, the bit is disabled and has a value of 0.

Let's take a few examples to make things easier to understand:

r-- Convert to 100 (binary)

rw- to 110 (binary)

R-X to 101 (binary)

To convert a number from binary to decimal, the easiest way is to start right to left, multiply by the power of 2, and add them together as follows:

  • 100 → 0 * 2⁰ + 0 * 2¹+ 1 * 2² = 0 + 0 + 4 = 4
  • 110 → 0 * 2⁰ + 1 * 2¹ + 1 * 2² = 0 + 2 + 4 = 6

Basically, to make it as simple as possible, if read is enabled, its value is 4, if write is enabled, it is 2, and if execution is enabled, it is 1.

Now, let's go back to the file with rw-r--r-- permissions.

Suppose you want to grant write permissions to the group, you can do so using the following command:

# g+w translates to group gets write permissions
chmod g+w a

# using numbers (the first number corresponds to the owner, the second to the group, the third to others)
chmod 664 a           

If you want to grant full permissions, you can:

# Everyone had read, now we give to everyone write and execute permissions
chmod ugo+wx a

# using numbers
chmod 777 a           

2. Command line basics

Basics: LS, CD, CAT, ECHO, MKDIR, RM

File operations: CP, MV, FIND, TAR, and ZIP are used to process and locate files.

Text manipulation: grep, awk, sed, and cut are useful for text processing.

Network utilities: Netstat, IFCONFIG (or IP), Curl, and Ping are essential for troubleshooting.

# List the contents of the current directory in a long listing format
ls -l

# List the contents of the current directory including hidden files or directories
ls -la

# Navigate to the 'documents' directory located in the 'user' home directory
cd /home/user/documents

# Go to the previous directory
cd ..

# Display the content of 'filename.txt' on the screen
cat filename.txt

# Display the phrase "Hello, World!" on the screen
echo "Hello, World!"

# Create a new directory named 'new_directory' in the current location
mkdir new_directory

# Delete the file named 'file_to_delete.txt'
rm file_to_delete.txt

# Recursively delete 'directory_to_delete' and all its contents (use with caution)
rm -r directory_to_delete

# Copy the file 'source.txt' to 'destination.txt'
cp source.txt destination.txt

# Move (or rename) the file 'oldname.txt' to 'newname.txt'
mv oldname.txt newname.txt

# Search for files in the '/home/user' directory with the name 'target.txt'
find /home/user -name target.txt

# Create a compressed archive named 'backup.tar.gz' of the 'backup' directory
tar -czvf backup.tar.gz backup/

# Create a compressed archive named 'archive.zip' containing the files 'file1.txt' and 'file2.txt'
zip archive.zip file1.txt file2.txt

# Search for the pattern "example" in 'filename.txt'
grep "example" filename.txt

# Print the second column of a file separated by commas
awk -F ',' '{print $2}' filename.txt

# Replace the first instance of the word "apple" with "orange" in 'filename.txt' and display the result
sed 's/apple/orange/' filename.txt

# Extract the third column of a file separated by colons
cut -d ':' -f 3 filename.txt

# Display all active network connections
netstat -a

# Display the configuration of all network interfaces
ifconfig

# Show information for all network interfaces
ip addr show

# Fetch the content of a website
curl www.example.com

# Send echo request packets to a domain to test connectivity
ping www.example.com           

3. Package management

Different Linux distributions have their own package managers. Familiarize yourself with at least one of the following:

Debian/Ubuntu:apt-get 或 apt

RedHat/CentOS:yum 或 dnf

SUSE:zypper

# Installs a package.
apt-get/yum/dnf/zypper install package_name

# Removes a package.
apt-get/yum/dnf/zypper remove package_name

# Update packages
apt-get/yum/dnf/zypper update           

4. Process Management It is important to know how to list, terminate, and prioritize processes using commands and tools such as ps, top, htop, and kill.

# Display a snapshot of the current processes
ps aux

# Display a real-time sorted view of system processes
top

# Interactive process viewer, an improved version of top
htop

# Send a SIGTERM signal to a process with process ID (PID) of 1234
kill 1234           

5. WE

VI is a text editor provided by default on almost every UNIX system and has been a staple of system administration for decades.

It has different modes, but the most relevant are:

Normal mode→ You start in this mode. It allows navigation, deletion, copying, and other text manipulation.

Insert mode → add new text

Command mode → Save, exit, etc

# Open a file named example.txt with vi (the file gets created if it doesn't exist)
vi example.txt

# Inside vi:

# Enter Insert Mode
i

# Save changes and exit
:wq

# Exit without saving changes
:q!

# Search for the string example
/example

# Set line number
:set_number           

6. User and Group Management

Ability to easily add, remove, and modify users and groups (useradd, usermod, groupadd, etc.) and understand /etc/passwd and /etc/group files.

# Add a new user named 'newuser' with a home directory
useradd -m newuser

# Modify 'newuser' to have the login shell as /bin/tcsh
usermod -s /bin/tcsh newuser

# Add a new group named 'newgroup'
groupadd newgroup

# Add 'newuser' to the 'newgroup'
usermod -aG newgroup newuser           

7. Shell Screenplay

Basic proficiency in scripting, many tasks can be automated using bash. You should know how to use variables, loops, and conditional statements.

This tutorial is great for learning bash.

8. Logging:

Learn where log files are typically stored (/var/log/) and tools that can help read log files, such as less, tail, head.

# View contents of a file.txt with navigation capabilities
less file.txt

# Display the last 10 lines of file.txt
tail file.txt

# Display the first 10 lines of file.txt
head file.txt

# Display the last 5 lines of file.txt
tail -n 5 file.txt

# Display the first 5 lines of file.txt
head -n 5 file.txt           

9. Network Basics:

Understand basic networking concepts such as IP addressing, subnets, ports, and protocols. You can check this tutorial for more information.

Learn how to configure and manage network interfaces, routes, and firewall rules. Tools and files like /etc/network/interfaces, iptables, etc. may come into play here.

10. Secure Shell (SSH):

SSH is the foundation of remote management. Learn how to use SSH for remote login, SCP for file transfers, and concepts about SSH keys.

You can use the ssh-keygen command to generate an SSH key. This creates a directory named .ssh and creates two keys (public key and private key) in it.

If you accept the defaults, the names of these keys will be id_rsa and id_rsa.pub.

Now that you have created the key, you can connect to the remote server. In the .ssh directory of the remote server, a special file called authorized_keys can be created, where if you add the contents of the public key, you will be able to connect to it using the private key.

If you use the default name, you do not need to specify the path to the private key, but otherwise, it is required:

ssh -i path_to_private_key user@server

11. Disk and Storage:

It is important to know how to view disk usage (DF, DUs), manage partitions and file systems (fdisk, mkfs), and mount/unmount storage devices.

# Display the amount of disk space used and available on mounted filesystems
df -h

# Estimate file space usage for example_dir
du -sh example_dir/

# Display the sizes of files and directories within example_dir
du -ah example_dir/           

Linux is really important, and to become a DevOps engineer, you must know how to navigate the file system, permissions, how to connect remotely to a server, write scripts in bash, and use vi.