Linux for DevOps

Linux for DevOps

Linux Founder

Linus Benedict Torvalds is a Finnish software engineer best known for having initiated the development of the Linux kernel and git revision control system.

History of Linux

Linus Torvalds a student at the University of Helsinki, Finland, USA developed the first code of Linux i.e. Linux 0.01, which became so popular that people encourages him to develop the new code of Linux. On 5 September 1991 Linus Torvalds developed the first official version of Linux i.e. Linux 0.02.

Linux File System Hierarchy

Linux is an open-source operating system. Linux follows the File System Hierarchy in which everything is represented as a file i.e. stored in a directory. Linux has a single-rooted, inverted tree-like structure. The root directory in Linux is represented as "/" (forward slash) also called the top-level directory.

Top-level directory -> "/"

  • The base of the Linux directory is the root. This is the starting point of FSH. Every directory arises from the root directory. It is represented by a forward slash (/).

  • If someone says to look into the slash directory, they refer to the root directory.

/root

  • It is the home directory for the root user (superuser).

/bin -> User Binaries

  • Contains binary executable.

  • Common Linux commands you need to use in single-user modes are located under this directory.

  • Commands used by all the users of the system are located here.

/sbin -> System Binaries

  • Just like /bin, /sbin also contains binary executables.

  • But, the Linux commands located under this directory are used typically by the system administrators, for system maintenance purposes.

  • For example, iptables, reboot, fdisk, ifconfig, and swapon.

/dev -> Device Files

  • contains hardware device files.

  • Contains device files.

  • These include terminal devices, USB, or any device attached to the system.

  • For example: /dev/tty1, /dev/usbmon0.

/var -> Variable Files

  • The variable data files such as log files are located in the /var directory.

  • File contents that tend to grow are located in this directory. This includes:-

    • /var/log: System log files generated by OS and other applications.

    • /var/lib: Contains database and packages files.

    • /var/mail: Contains Emails.

    • /var/tmp: Contains temporary files needed for reboot.

/mnt -> Mount Directory

  • This directory is used to mount a file system temporarily.

/media -> Removable Media Devices

  • The /media directory contains subdirectories where removable media devices inserted into the computer are mounted.

/usr -> User Binaries

  • The /usr directory contains applications and files used by users, as opposed to applications and files used by the system.

/etc -> Configuration files

  • It contains all configuration files of the server.

  • The core configuration files are stored in the /etc directory. It controls the behavior of an operating system or application. This directory also contains startup and shutdown program scripts that are used to start or stop individual programs.

/boot -> Boot Loader Files

  • The /boot directory contains the files needed to boot the system.

  • For example, the GRUB bootloader's files and your Linux kernels are stored here.

/opt -> Optional Applications

  • The opt directory is used for installing the application software from third-party vendors that are not available in the Linux distribution. Usually, the software code is stored in the opt directory and the binary code is linked to the bin directory so that all users can run that software.

/home -> Home Directory

  • It contains the secondary user's home directory.

/tmp -> Temporary Files

  • A directory that contains temporary files created by the system and users.

  • Files under this directory are deleted when the system is rebooted.

Linux Architecture

In the Linux architecture, the two most important components are the shell and Kernel. The kernel is the one that gives commands to the hardware to perform necessary actions. On the other hand, the shell is used to access the kernel.

In simple words, the shell is used to communicate with the kernel using Linux commands, and based on these commands the kernel further orders the hardware to do so. Shell is used to get the kernel and the kernel is used to get the hardware.

Hardware includes several peripheral devices such as a CPU, HDD, and RAM.

Basic Commands

  • pwd -> It shows the present working directory.

  • Is -> It shows the available files and directory list in the current working directory.

  • uname -> It shows the name of the kernel (OS).

  • uname -r -> It shows the version of the kernel.

  • cd -> It is used to change the directory.

  • clear -> It is used for clearing the screen.

  • whoami -> It shows the current login user name.

  • history -> It shows a list of previously used commands.

  • date -> It shows the time and date.

Create a directory

  • To create a single directory.

      mkdir dir_name
    
  • To create multiple directories.

      mkdir dir_name1 dir_name2 dir_name3
    
  • To create a directory path (directory inside the directory).

      mkdir -p dir1/dir2/dir3/dir4
    
  • To create directories with serial-wise numbers.

      mkdir dir{1..10}
    

Create a file

  • To create a file.

      touch file
    
  • To create multiple files.

      touch file1 file2 file3
    
  • To create files with serial-wise numbers.

      touch file{1..10}
    

For copy & paste

  • To copy and paste files or directories.

      cp <option> <source> <destination>
    
  • Options :

    -r or recursive

    -v for verbose

    -f for forcefully

For removing files and directory

  • For deleting files or directories.

      rm -rvf file_name
      rm -rvf dir_name
    

For moving or renaming files & directory

  • To rename a file or directory.

      mv old_name new_name
    
  • To move a file, directory to another directory.

      mv source_file destination
    

User Management

  • For creating a user account.

      useradd user_name
    
  • For checking user account properties.

      cat /etc/passwd
    
  • For creating a user account password.

      passwd user_name
    
  • For checking user password properties.

      grep user_name/etc/shadow
    
  • For switching user accounts.

      su user_name
    
  • To log out from a user account.

      exit
    
  • Or press Ctrl + D key.

  • For Deleting a user account.

      userdel user_name
    
  • To change the user Login name.

      usermod -l login_name old_name
    

Group Management

In Linux, groups are collections of users. Creating and managing groups is one of the simplest ways to deal with multiple users simultaneously, especially when dealing with permissions. The /etc/group file stores group information and is the default configuration file.

  • To add a group account.

      groupadd group_name
    
  • To check group account property.

      cat /etc/group
    
  • For checking group admin property.

      cat /etc/gshadow
    
  • To delete a group.

      groupdel group_name
    
  • To add a single member to a group.

      gpasswd -a user_name group_name
    
  • To add multiple members to a group.

      gpasswd -M user_name1,user_name2,user_name3 group_name
    
  • To remove a group member.

      gpasswd -d user_name group_name
    
  • To make a group admin.

      gpasswd -A user_name group_name
    

Linux File System Permission

Type of File Permission

  • Basic Permission.

  • Special Permission.

  • Access Control List (ACL) Permission.

For checking file permission

ls -l file_name

For checking directory permissions

ls -ld dir_name

Permission in detail

There are 3 types of permission classes:-

  1. User

  2. Group

  3. Other

Permission classes tell us about the permissions that users, group members, and other users have over that file/directory.

  • File type tells the type of file for eg. "d" stands for directory and "-" stands for file.

  • "r" stands for Read.

  • "w" stands for Write.

  • "x" stands for Execute.

Permission Set

Permission with numeric & symbol

For changing permissions

  • To add read permissions to the owner.

      chmod u+r file_name
    
  • To add read-write permissions to the group.

      chmod g+rw file_name
    
  • To remove read permission for others.

      chmod o-r file_name
    

For changing ownership

chown user_name file_name
chown user_name directory_name

For changing group ownership

chgrp group_name file_name
chgrp group_name directory_name

Set permissions with numeric values

r (read) = 4
w (write) = 2
x (execute) = 1

chmod 751 file_name

Access Control List (ACL)

  • Access control list (ACL) provides an additional, more flexible permission mechanism for file systems.

  • Access Control List is a service that provides special permission to specific users and groups for particular directories and files.

Use of ACL

Think of a scenario in which a particular user is not a member of the group created by you but still, wants to give some read or write access, how can you do it without making the user a member of the group, here comes in picture Access Control List, ACL helps us to do this trick.

  • For checking ACL permissions.

      getfacl file_name
      getfacl dir_name
    
  • To set ACL permissions for the user.

      setfacl -m user::rwx file_name
      setfacl -m user::rwx dir_name
    
  • To remove the ACL permissions for the user.

      setfacl -x user:user_name file_name
      setfacl -x user:user_name dir_name
    
  • To set ACL permissions for the group.

      setfacl -m group::rwx file_name
      setfacl -m group::rwx dir_name
    
  • To remove ACL permissions for the group.

      setfacl -x group file_name
      setfacl -x group dir_name
    
  • To remove all the ACL permissions.

      setfacl -b file_name
      setfacl -b dir_name
    

Regular Expressions

Regular expressions are special characters that help search data, matching complex patterns.

GREP (Global Regular Expression Print)

The grep filter searches a file for a particular pattern of characters and displays all the lines that contain that pattern.

  • Search a word (string in a file).

      grep root /etc/passwd
    
  • Search a string in multiple files.

      grep root /etc/passwd /etc/group
    
  • Search a string insensitively in a file.

      grep -i RooT /etc/passwd
    
  • Search a string in all files recursively.

      grep -r root /
    
  • Inverting the string match.

      grep -v root /etc/passwd
    
  • Display the total lines of the string matched.

      grep -c root /etc/passwd
    
  • Display the file names that match the string.

      grep -l root /etc/passwd /etc/shadow
    
  • Display the file names that do not contain the string.

      grep -L root /etc/passwd /etc/shadow
    
  • Displaying the string match line with a number.

      grep -n root /etc/passwd
    
  • Display the lines that start with a string.

      grep ^root /etc/passwd
    
  • Display the lines that end with a string.

      grep /bin/bash$ /etc/passwd
    
  • Search and redirect output in a new file.

      grep root /etc/passwd > /home/ubuntu/grep.txt
    

Find

The Linux Find Command is one of the most important and much-used commands in the Linux system. The find command is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments. Find can be used in a variety of conditions you can find files by permissions, users, groups, file type, date, size, and other possible criteria.

  • Find files under /home directory.

      find /home -name file_name
    
  • Find files with suid permission.

      find / -perm 4755
    
  • Find files with guid permission.

      find / -perm 2644
    
  • Find files with sticky bit permission.

      find / -perm 1755
    
  • Find command based on user.

      find / -user root
    
  • Find commands based on the group.

      find / -group group_name
    
  • Search the file with less than IOMB in a folder.

      find /tmp -size -10M
    
  • Search the file with more than IOMB in a folder.

      find /tmp -size +10M
    

WC (Word Count)

The wc command is used for counting words and line numbers.

  • Count the number of lines.

      wc -l /etc/passwd
    
  • Count the number of words.

      c -w /etc/passwd
    

Head

Head command is used to display the top lines of a file.

  • Display the top 10 lines of a file.

      head /etc/passwd
    
  • Display top specific no line of the file.

      head -n 15 /etc/passwd
    

Tail

The tail command is used to display the bottom lines of a file.

  • Display the bottom 10 lines of the file.

      tail /etc/passwd
    
  • Display the bottom specific lines of a file.

      tail -n 5 /etc/passwd
    

Archive File in Linux

Archiving is a process of combining multiple files and directories (same or different sizes) into one file. The archive process is very useful for the backup and compression size of data in Linux.

What is Tar?

The Linux tar stands for tape archive, which is used by a large number of Linux/Unix system administrators to compress size or drive backup. To create an archive tar there're needed some compression algorithms such as gzip, bz2 and xz.

Tar command syntax.

tar <options> <files>

c - To create

x - To extract

v - To verbose

f - To forcefully

t - To test

z - To gzip

j - To bz2

J - To xz

C - To specific destination

  • To create a tar archive file.

      tar -cvf /mnt/backup.tar /var
    
  • To show file size in a human-readable format.

      du -sh /var
      du -sh /mnt/backup.tar
    
  • To extract a tar archive file on the default location.

      tar -xvf /mnt/backup.tar
    
  • To extract a tar archive file on a specific location.

      tar -xvf /mnt/backup.tar -C /root/Desktop/
    
  • To create a tar archive file with compress in size (gzip).

      tar -cvzf /mnt/backup.tar.gz /var
    
  • To extract a tar archive file with compress in size (gzip).

      tar -xvzf /mnt/backup.tar.gz
    
  • To extract a tar archive file with compress in size (bzip2/bz2).

      tar -xvjf /mnt/backup.tar.bz2
    
  • To create a tar archive file with compress in size (xz).

      tar -cvJf /mnt/backup.tar.xz /var
    
  • To extract a tar archive file with compress in size (xz).

      tar -xvJf /mnt/backup.tar.xz
    

Job Automation

  • Job automation allows us to perform tasks automatically in OS by using tools.

  • This feature is very useful for the administrator to assign the task to OS whenever he is not present or performs daily basis work.

Two types of job automation

  1. at - at command is used to execute a job only one time.

  2. crontab - Crontab command is used to execute jobs multiple times.

  • To set a job with at command.

      date
      8:10 AM
      at > useradd shub
      at > 
      #Ctrl+d (write & quit)
    
  • To show pending at job.

      atq
    
  • To remove at job.

      atrm 2
    
  • To restrict a user from accessing at.

      vim /etc/at.deny
      Shub (add here user name)
      :wq #(write&quit)
    

    Cronjob:-

  • To start a crond service.

      systemctl start crond
    
  • To enable a crond service (Permanent on).

      systemctl enable crond
    
  • To set cron jobs.

      crontab -e
    
  • To show the cronjobs of the current user.

      crontab -l
    
  • To remove the cron jobs.

      crontab -r
      #Or go to the crontab file and remove job line
      crontab -e
    
  • To set a cronjob to other users.

      crontab -u shub -e
    
  • To show the cronjob, other users

      crontab -u shub -l
    
  • To restrict users from crond service.

      vim /etc/cron.deny
    
  • To check the crontab log file.

      tail -f /var/log/cron
    

Sudo Command

What is sudo?

  • Sudo ("superuser do", or "switch user do") allows a user with proper permissions to execute a command as another user, such as the superuser.

  • Sudo allows a permitted user to execute a command as another user, according to specifications in the /etc/sudoers file.

Wheel group

A wheel is a system group that by default has sudo privileges, if we add any member to that group then that user got sudo privileges.

By default, all the members of the wheel group got sudo privileges.