Linux Training : Section 5 (Part-1)

Module 5 - System Administration

Linux File Editor(vi)

  • A text editor is a program which enables you to create and manipulate data (text) in a Linux File

    • vi supplies commands for:

      • Inserting and Deleting text

      • Replacing text

      • Moving around the file

      • Finding and substituting strings

      • Cutting and pasting text

    • Most common keys-

      • i = insert

      • esc = escape out of any mode

      • r = replace

      • d = delete

      • dd = delete current line

      • x = delete the current character

      • u = undo

      • :q! = quit without saving

      • /<name> = to search

      • :wq! OR SHIFT+Z,Z\= quit and save

      • :set number = display no. of lines

      • :set nonumber = turns off numbering of lines

      • :%s/aditya/shiv = replace aditya with shiv

vi vs vim editor

  • Vi is known for its modal editing, where the meaning of typed keys depends on which mode the editor is in.

  • Vim is an improved clone of Vi that offers additional features and functionality.

  • Vim is designed for use both from a command-line interface and as a standalone application in a graphical user interface.

  • Vi is typist-friendly, with short commands that don't require removing hands from the keyboard.

  • Vim is the most commonly used implementation of the Vi standard.

“sed” Command

  • Replace a string in a file with a newstring

  • Find and delete a line

  • Remove empty lines

  • Remove the first or n lines in a file

  • To replace tabs with spaces

  • Show defined lines from a file

  • Substitute within vi editor

  1. sed ‘s/aditya/shiv/g’ seinfeld-characters - by this cmd aditya will get replace with shiv but this reflection will not occur in actual file

  2. sed -i ‘s/aditya/shiv/g’ seinfeld-characters - this will reflect in the file

  3. sed ‘/aditya/d’ seinfeld-characters - It will delete the aditya keyword from file and it will just display it on the screen, not inside the file.

  4. sed ‘/^$/d’ seinfeld-characters - To delete the empty lines inside the file

  5. sed ‘1d’ seinfeld-characters - To delete 1st line

  6. sed ‘1,2d’ seinfeld-characters - to delete 1st & 2nd line

  7. sed ‘s/\t/ /g’ seinfeld-characters - Replace tab from space

  8. sed -n 12,18p seinfeld-characters - Display define lines from bottom

  9. sed 12,18d seinfeld-characters - Display define lines from top

  10. sed G seinfeld-characters - Enter after every line

  11. sed ‘8!s/shiv/RAM/’ seinfeld-characters - Substitute the RAM over shiv after line 8.

User Account Management

Commands

  • useradd

  • groupadd

  • userdel

  • groupdel

  • usermod

Files

  • /etc/passwd

  • /etc/group

  • /etc/shadow

Example

  • useradd -g superheros -s /bin/bash -c “user desc“ -m -d /home/spiderman spiderman

Hands-On

  1. Create a super using useradd-

  2. Create a group-

  3. Delete a user-

  4. Delete the group-

  5. Modify the user-

    NOTE: use the passwd <filename> to setup the password.

Enable Password Aging

The /etc/login.defs File

  • The chage command - per user

    • Example

      chage [-m mindays] [-M maxdays] [-d lastday] [-I inactive] [-E expiredate] [-W warndays] user

  • File = /etc/login.def

    • PASS_MAX_DAYS 99999

    • PASS_MIN_DAYS 0

    • PASS_MIN_LEN 5

    • PASS_WARN_AGE 7

Switch Users and sudo Access

  • Commands

    • su - username

    • sudo command

    • visudo

  • File

    • /etc/sudoers

Monitor Users(who, last, w, id)

  • Commands

    • who - Used to check how many persons are logged into the system.

    • last - Check for the login history

    • w - Just like who with some extra details

    • finger OR pinky

    • id

Talking to Users

  • Commands-

    • users - print the user names of users currently logged into the current host

    • wall - write a message to all users…..cntl+d to send the msg.

    • write - write a message to specific user — CMD- write <username>

Linux Account Authentication

  • Types of Accounts

    • Local accounts

    • Domain/Directory accounts

  • Active Directory = Microsoft

  • IDM = Identity Manager

  • WinBIND = Used in Linux to communicate with Windows(Samba)

  • OpenLDAP(open source) = it’s a protocol

  • IBM Directory Server

  • JumpCloud

  • LDPA = Lightweight Directory Access Protocol

System Utility Commands

  • date = shows date and time

  • uptime = tell from how much time the system is running

  • hostname = shows hostname

  • uname = print system information

  • which = shows full path of cmd

  • cal = display a calendar

  • bc = binary calculator

Processes and Jobs

  • Application = Service

  • Script = Shell Scripts or cmd are list of instructions e.g., adduser, cd, pwd etc

  • Process = Every Service has Process

  • Daemon = Runs till interrupted

  • Threads = Every Service has Process and it has threads

  • Job = Job or workorder- Run a service or process at a schedule time

Commands-

  • systemctl or service

    • systemctl command is a new tool to control system services

      • Examples:

        • systemctl start|stop|status servicename.service

        • systemctl enable servicename.service

        • systemctl restart|reload servicename.service

        • systemctl list-units --all

    • To add a service under systemctl management:

      • Create a unit file in /etc/systemd/system/servicename.service
    • To control system with systemctl

      • systemctl poweroff

      • systemctl halt

      • systemctl reboot


  • ps

    • ps command stands for process status and it displays all the currently running process in the Linux system.

      • Example:

        • ps = Shows the processed of the current shell

        • ps -e = Shows all running processes

        • ps aux = Shows all running processes in BSD format

        • ps -ef = Shows all running process in full format listing

        • ps -u username = shows all processes by the username


  • top

    • top command is used to show the Linux processes and it provides a real-view of the running system

    • This command shows the summary information of the system and the list of processes or threads which are currently managed by the Linux Kernel.

    • When the top command is executed then it goes into interactive mode and you can exit out by hitting q.

    • Example:

      • top -u username = shows tasks/processes by user owned

      • top then press c = shows commands absolute path

      • top then press k = kill a process by PID within top session

      • top then M and P = to sort all Linux running processes by Memory usage


  • kill

    • this command is used to terminate processes manually

    • It sends a signal which ultimately terminates or kills a particular process or group of processes

      • Example:

        • kill [OPTION] [PID]

          • OPTION\= Signal name or signal number/ID

          • PID = Process ID

      • kill PID = Kill a process with default signal

      • kill -1 = Restart

      • kill -2 = Interrupt from the keyboard just like ctrl+C

      • kill -9 = Forcefully kill the process

      • kill -15 = Kill a process gracefully

      • killall = Kill All

      • pkill = Kill by process name


  • crontab

    • Crontab command is used to schedule tasks

    • Usage:

      • crontab -e = edit the crontab

      • crontab -l = list the crontab entries

      • crontab -r = remove the crontab

      • crond = crontab daemon/service that manages scheduling

      • systemctl status crond = to manage the crond service

Crontab in Linux. Crontab is a UNIX command that creates… | by Ali Suat  Usta | Medium


  • at

    • at command is like crontab which allows you to schedule jobs but only once

    • When the command is run it will enter interactive mode and you can get out by pressing CTRL+D

    • Usage:

      • at HH:MM PM = Schedule a job

      • atq = List the at entries

      • atrm # = Remove at entry

      • atd = at daemon/service that manages scheduling

      • systemctl status atd = To manage the atd service

    • Other future scheduling format:

      • at 2:45 AM 101621 = schedule a job to run on Oct 16th, 2021 at 2:45am

      • at 4PM + 4 days = schedule a job at 4pm four days from now

      • at now +5 hours = schedule a job to run five hours from now

      • at 8:00 AM Sun = schedule a job to 8am on coming Sunday

      • at 10:00 AM next month = schedule a job to 10am next month

Additional Cron Jobs

  • by default there are 4 different types of cronjobs

    • Hourly

    • Daily

    • Weekly

    • Monthly

  • All the above crons are setup in

    • /etc/cron.____ (directory)
  • The timing for each are set in

    • /etc/anacrontab - - except hourly
  • For hourly

    • /etc/cron.d/0hourly

Process Signals in Linux

Types-

  • SIGNIT (Signal Interrupt) —> Used to interrupt process: Kill -SIGINT PID

  • SIGTERM (Signal Terminate) —> Process to terminate gracefully: kill -SIGTERM PID

  • SIGKILL (Signal Kill) —> Forcefully kill process: kill -SIGKILL PID

  • SIGSTOP (Signal Stop) —> Pause Running process: kill -SIGSTOP PID

  • SIGCONT (Signal Continue) —> Resume Stopped Process: kill -SIGCONT PID

  • SIGSEGV (Signal Segmentation Fault) —> Access Invalid Memory Location: kill -SIGSEGV PID