Advanced knowledge points for Linux (1)

Source: Internet
Author: User
Tags bz2 all mail rsyslog egrep

I. Scheduled Tasks (i), Crond introduction
  • Crond is a daemon that is used to periodically perform certain tasks or wait for certain events under Linux, similar to Scheduled tasks under Windows, when the operating system is installed, the Service tool is installed by default and the Crond process is started automatically. The Crond process periodically checks to see if there is a task to perform and automatically executes the task if there are tasks to perform. (ii), task scheduling
  • The task scheduling under Linux is divided into two categories, system task scheduling and user task scheduling.
    1. System Task Scheduling
    The work to be performed by the system periodically, such as writing cache data to hard disk, log cleanup, etc. In the/etc directory there is a crontab file, this is the System Task Scheduler configuration file.
    2, the user regularly to perform the work, such as user data backup, timed email reminders and so on. Users can use the Crontab tool to customize their own scheduled tasks. All user-defined crontab files are saved in the/var/spool/cron directory. Its file name is the same as the user name.
  • crontab format

  • Crontab Special Symbols
    1, asterisk (*): represents all possible values, such as the month field if it is an asterisk, the command action is executed monthly after the constraints of other fields are met.
    2, comma (,): You can specify a list range with a comma-separated value, for example, "1,2,5,7,8,9"
    3, Middle Bar (-): You can use a middle bar between integers to represent an integer range, for example "2-6" means "2,3,4,5,6"
    4, forward slash (/): You can use a forward slash to specify the time interval frequency, for example, "0-23/2" means to execute every two hours. A forward slash can be used with an asterisk, for example,/10, if used in the minute field, indicating that it is executed every 10 minutes. (iii), writing timing task notes
  • When we write timed tasks, we must redirect the results of the task execution to a file or a black hole directory, the default system will send all the information through the mail to the user, if the mailbox service is closed, all mail will be stored to/var/spool/postfix/ Maildrop This directory creates a large number of small files that cause the inode to be stained with faults.
  • When executing a script with a timed task, be sure to indicate the environment variable at the beginning of the script, and the default timer task is to identify only the commands below/bin and/usr/bin, and the% to represent line breaks in the timed task. (iv), how to configure the timing task (a chestnut)
  • Write timed tasks Complete: 12 o'clock every night backup/etc/rc.local/etc/sysconfig//var/spool/cron/etc/fstab These files to the/backup directory

    [Email protected] ~]# CRONTAB-E
    #backup Log Script
    0 0 */bin/tar-jcpf/backup/beifen.$ (date +\%f). tar.bz2/etc/rc.local/etc/sysconfig//var/spool/cron/etc/fst Ab
    Just a little verification.
    [[email protected] ~]# date-s 23:59-----? Modify the system time
    Wed 23:59:00 CST 2018
    [[email protected] ~]# ll/backup/-----? See if you want to back up and see when the compression package was created
    Total 48
    -rw-r--r--1 root root 47922 00:00 beifen.2018-08-30.tar.bz2
    [[email protected] ~]# tail-f/var/log/cron-------------? log to see if the execution was successful
    00:00:01 Jiangjunwang crond[3041]: (Root) CMD (/bin/tar-jcpf/backup/beifen.$ (date +%f). tar.bz2/etc/rc.local/etc /sysconfig//var/spool/cron/etc/fstab 2>&1)

Second, how to let a script or command can be powered on (a), the command or script in the/etc/rc.local (ii), through the Chkconfig management
  • The
  • Chkconfig command is primarily used to update (start or stop) and query run-level information for system services. Keep in mind that Chkconfig does not immediately automatically disable or activate a service, it simply changes the symbolic connection.

    [[email protected] ~]# chkconfig--list iptables
    iptables 0:off 1:off 2:off 3:off 4:off 5: Off 6:off
    [[email protected] ~]# ll/etc/rc3.d/|grep "Iptab"
    lrwxrwxrwx 1 root root + 19:47 K92iptables .. /init.d/iptables
    [[email protected] ~]# chkconfig iptables on
    [[email protected] ~]# ll/etc/rc3.d/| grep "Iptab"
    lrwxrwxrwx 1 root root, Sep 6 15:42 s08iptables. /init.d/iptables
    [[email protected] ~]# head-10/etc/init.d/iptables
    #!/bin/sh
    #iptables Start iptables firewall
    #chkconfig: 2345
    #description: Starts, stops and saves Iptables firewall

    /li>
  • From the above results we are not difficult to find, we in the execution of chkconfig on or off, in fact, just changed the name of a symbolic link inside the/etc/rc3.d, where k means that the turn off S is open, the number behind is the order of the start and the order of closing. which is #chkconfig:2345 08 92 of 08 and 92

    (iii), add script to chkconfig for management (a chestnut)

    [Email protected] ~]# vim/etc/init.d/test

    #chkconfig: 2345 88 88
    Echo Jiangjunwang
    ~
    [Email protected] init.d]# chmod +x test
    [Email protected] init.d]# chkconfig--add test
    [[email protected] init.d]# chkconfig test on
    [Email protected] init.d]# chkconfig--list test
    Test 0:off 1:off 2:on 3:on 4:on 5:on 6:off
    [Email protected] init.d]# ll/etc/rc3.d/|grep "test"
    lrwxrwxrwx 1 root root, Sep 6 16:11 s88test. /init.d/test

A city Case Study/etc/skel directory (a), ask the following error should be how to resolve?

[Email protected] skel]# Su-jiangjunwang
-bash-4.1$
-bash-4.1$

    • It's really simple. We just need to copy all the. bash files under/etc/skel to the current user home directory

      -bash-4.1$ \cp-rf/etc/skel/.bash*/home/jiangjunwang/
      -bash-4.1$ Logout
      [Email protected] ~]# Su-jiangjunwang
      [Email protected] ~]$

(b), why is this?
    • Because the/etc/skel/directory is the directory used to store the new user profile, when we add a new user, all files in this directory are automatically copied to the newly added user's home directory.
      By modifying, adding, and deleting files in the/etc/skel directory, we can provide a unified, standard, and initialized user environment for newly created users.
      /etc/skel/.bash_logout = = User exits will run the commands inside
      /etc/skel/.bash_profile = = =/etc/profile
      /ETC/SKEL/.BASHRC = = =/ETC/BASHRC
Iv. the md5sum of the recognition of true and false
    • The MD5 algorithm is typically used to check file integrity, and is often used to detect file correctness (a), md5sum usage (a chestnut) in the case of (network) file transfers, copies, disk errors, or other non-malicious involvement.

      [Email protected] ~]# md5sum a.txt
      53A8F548D07E3D0EFFF01D6AF6943AB4 A.txt
      [Email protected] ~]# md5sum a.txt >police.log
      [Email protected] ~]# md5sum-c Police.log
      A.txt:ok
      [Email protected] ~]# echo a>>a.txt
      [Email protected] ~]# md5sum-c Police.log
      A.txt:failed
      Md5sum:warning:1 of 1 computed checksum did not match

Five, the Linux Imperial Sword sudo
    • Imagine a scenario where developers need to look at a log with root permissions, which is what should be done, maybe you think of suid or open the file permissions, so that although the requirements can be achieved, but for the system is not safe enough, then we can think of the Linux Imperial Sword, sudo to carry out on demand, what permissions you want to give what permissions! (a), the main configuration file
    • sudo configuration file in/etc/sudoers, it is recommended to use the Visudo command to edit the configuration file, because this command will be the sudoers file syntax self-test, so that we can find errors in time.

    • Configuration file Basic Syntax
can be executed
User or group machine = (authorization role) commands that
User Machine= COMMANDS
Jiangjunwang All= (All) Nopasswd:/bin/ls,/bin/touch
(b), how to configure (a chestnut)
  • Authorize the Jiangjunwang user to execute the ls,touch,passwd command as root, but disable the root user password change

    [[email protected] ~]# visudo
    Jiangjunwang all= (Root) nopasswd:/bin/ls,/bin/touch/usr/bin/passwd [!-]*,! /USR/BIN/PASSWD root
    [[email protected] ~]$ sudo-l
    Matching Defaults entries for Jiangjunwang on this host:!VISIBLEPW, Always_set_home, Env_reset, env_keep= "COLORS DISPLAY HOSTNAME histsize INPUTRC
    Kdedir ls_colors", env_ keep+= "MAIL PS1 PS2 qtdir USERNAME LANG lc_address lc_ctype",
    env_keep+= "Lc_collate lc_identification lc_measurement Lc_messages ", env_keep+=" Lc_monetary
    lc_name lc_numeric lc_paper lc_telephone ", env_keep+=" LC_TIME LC_ALL LANGUAGE Linguas
    _xkb_charset xauthority ", Secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin

    User Jiangjunwang may run the following commands on this host:
    (Root) NOPASSWD:/bin/ls, (Root)/bin/touch/usr/bin/passwd [!-]*, (root)!/usr/bin/passwd root
    [email protected] ~]$ sudo ls/etc/passwd
    /etc/passwd
    [email protected] ~]$ sudo passwd
    [sudo] password for Jiangjunwang:
    Sorry, user Jiangjunwang is not allowed to execute '/usr/bin/passwd ' as the root on Jiangjunwang.
    [Email protected] ~]$

Note: NOPASSWD indicates that the user does not need to enter a password when executing the command, [!-]* indicates that the parameter must be taken when the command is entered, because if there is no such symbol here a normal user can use sudo passwd to modify the root user's password.

Six, disk partitioning

Now the main partitioning method has two types of--MBR partition and GPT partition, the FDISK command supports the MBR partitioning method parted partitioning tool supports GPT and MBR way

(a), add a 100M hard drive and mount it permanently onto the/DATA01 (a chestnut)
  • First step: Insert the hard drive into the server and then format the partition

    [Email protected] ~]# Fdisk-cu/dev/sdb
    Device contains neither a valid DOS partition table, nor Sun, SGI or OSF Disklabel
    Building a new DOS disklabel with disk identifier 0x174e0a0c.
    Changes'll remain in memory only, until the decide to write them.
    After that, of course, the previous content won ' t is recoverable.

    Warning:invalid flag 0x0000 of partition Table 4 would be a corrected by W (rite)

    Command (M for help): N
    Command Action
    E Extended
    P primary partition (1-4)
    P
    Partition number (1-4): 1
    First sector (2048-209715199, default 2048):
    Using Default Value 2048
    Last sector, +sectors or +size{k,m,g} (2048-209715199, default 209715199):
    Using Default Value 209715199

    Command (M for help): P

    disk/dev/sdb:107.4 GB, 107374182400 bytes
    255 heads, Sectors/track, 13054 cylinders, total 209715200 sectors
    Units = sectors of 1 * MB = bytes
    Sector size (logical/physical): bytes/512 bytes
    I/O size (minimum/optimal): bytes/512 bytes
    Disk identifier:0x174e0a0c

    Device Boot Start End Blocks Id System
    /DEV/SDB1 2048 209715199 104856576-up Linux

    Command (M for help): W
    The partition table has been altered!

    Calling IOCTL () to re-read partition table.
    Syncing disks.

  • The second step informs the/DEV/SDB that the disk partition table changes the hard disk partition table information of the update kernel

    [Email protected] ~]# Partprobe/dev/sdb

  • Format the partition in the third step,

    [[email protected] ~]# mkfs.ext4/dev/sdb1
    mke2fs 1.41.12 (17-may-2010) Br>filesystem label=
    OS type:linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    stride=0 blocks, Stripe width=0 blocks
    6553600 inodes, 26214144 blocks
    1310707 blocks (5.00%) reserved for the super user
    First Da Ta block=0
    Maximum filesystem blocks=4294967296
    block groups
    32768 blocks per group, 32768 fragments per Grou P
    8192 inodes per group
    superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 88473 6, 1605632, 2654208,
    4096000, 7962624, 11239424, 20480000, 23887872

    Writing inode tables:done
    Creating J Ournal (32768 blocks): Done
    Writing superblocks and filesystem accounting Information:done

    This filesystem WI ll be automatically checked every mounts or
    in days, whichever comes first. Use Tune2fs-c or-i to override.

  • The fourth step turns off the automatic disk detection function

    [Email protected] ~]# tune2fs-c 0-i 0/dev/sdb1
    TUNE2FS 1.41.12 (17-may-2010)
    Setting Maximal mount Count to-1
    Setting interval between checks to 0 seconds

  • Fifth mount the partition to the directory

    [Email protected] ~]# mount/dev/sdb1/data01/
    [Email protected] ~]# df-h
    Filesystem Size used Avail use% mounted on
    /dev/sda3 17G 3.6G 13G 23%/
    Tmpfs 932M 0 932M 0%/dev/shm
    /DEV/SDA1 190M 40M 141M 23%/boot
    /DEV/SDB1 99G 60M 94G 1%/data01

  • Sixth step Permanent Mount

    [Email protected] ~]# Vim/etc/fstab
    /DEV/SDB1/DATA01 EXT4 Defaults 0 0

(b), create a 500M file and permanently add it to swap (a chestnut)
    • The first step is to create a 500M empty file

      [[email protected] ~]# dd If=/dev/zero of=/tmp/test bs=1m count=500
      500+0 Records in
      500+0 Records out
      524288000 bytes (524 MB) copied, 0.446025 s, 1.2 GB/s

    • Change to swap type in the second step

      [Email protected] ~]# mkswap/tmp/test
      Mkswap:/tmp/test:warning:don ' t erase bootbits sectors
      On whole disk. Use-f to force.
      Setting up Swapspace version 1, size = 511996 KiB
      No label, Uuid=aa2a415e-96e6-46e4-84f9-44acad261a96

    • Step three, get him into effect. Activating swap partitions

      [Email protected] ~]# swapon/tmp/test
      [Email protected] ~]# swapon-s
      Filename Type Size used priority
      /DEV/SDA2 Partition 3071996 0-1
      /tmp/test file 511996 0-2

    • Fourth step setting boot from start for permanent

      [Email protected] ~]# vim/etc/rc.local
      Swapon/tmp/test

Vii. the Three Musketeers of Linux (i), the Three Musketeers of the SED
    • Grammar
      sed [options] [sed directive] [input file]
      8
Options meaning. directive meaning
-N Cancel default Output P Print matching content
-R Supports extended regular A Add the class capacity on the next line of the matching row
-I. Write File Support backup I Add a class tolerance on the previous line of the matching row
S.. G s means that replace G represents the global
(b), some chestnuts
    • Create a test file first

      [[email protected] test]# cat >bb.txt<<eof
      1 ni hao
      2 Wo Hao
      3 ta hao
      4 da Jia Hao
      5 wo men Dou Hao
      EOF

    • Print first line

      [[ Email protected] test]# sed-n ' 1p ' bb.txt
      1 ni hao

    • Print the class capacity of the second line to line fourth

      [[email protected] test]# sed-n ' 2,4p ' bb.txt
      2 wo hao
      3 ta hao
      4 da Jia Hao

    • display lines containing "wo"

      [[email protected] test]# sed-n '/wo/p ' bb.txt
      2 wo hao
      5 wo men Dou Hao

    • shows the line containing "NI" in the file to the line containing "TA"

      [[email protected] test]# sed-n '/ni/,/ta /P ' bb.txt
      1 ni hao
      2 wo hao
      3 ta hao

    • filter lines containing "Ni" and "ta"

      [email  protected] test]# sed-nr '/ni|ta/p ' bb.txt
      1 ni hao
      3 ta hao

Ps:sed inside the regular character must have "/"

  • Querying the contents of lines 2nd and fourth

    [Email protected] test]# sed-n ' 2p;4p ' bb.txt
    2 Wo Hao
    4 da Jia Hao

  • Add "Xinlai" to the next line in the second row

    [[Email protected] test]# sed ' 2a xinlai ' bb.txt
    1 Ni hao
    2 Wo Hao
    Xinlai
    3 Ta Hao
    4 da Jia Hao
    5 wo men Dou Hao

  • Add "Xinlai" and AA two lines on the next line containing the Wo Hao string

    [[Email protected] test]# sed '/wo hao/axinlai\naa ' bb.txt
    1 Ni hao
    2 Wo Hao
    Xinlai
    Aa
    3 Ta Hao
    4 da Jia Hao
    5 wo men Dou Hao
    Ps:\n indicates line break

  • Delete the second row to the last line class capacity

    [[Email protected] test]# sed ' 2, $d ' Bb.txt
    1 Ni hao

  • Do not show empty lines of files

    [[Email protected] test]# sed '/^$/d ' bb.txt
    1 Ni hao
    2 Wo Hao
    3 Ta Hao
    4 da Jia Hao
    5 wo men Dou Hao
    [Email protected] test]# sed-n '/^$/!p ' bb.txt
    1 Ni hao
    2 Wo Hao
    3 Ta Hao
    4 da Jia Hao
    5 wo men Dou Hao

  • Replace Wo in the file with AA

    [[Email protected] test]# sed ' s/wo/aa/g ' bb.txt
    1 Ni hao
    2 AA Hao
    3 Ta Hao
    4 da Jia Hao
    5 AA men Dou Hao

  • Replace Wo in the file with AA and back up the original file

    [Email protected] test]# sed-i.bak ' s/wo/aa/g ' bb.txt
    [[email protected] test]# ls
    Bb.txt Bb.txt.bak

  • Variable substitution

    [Email protected] test]# X=wo
    [Email protected] test]# Y=ni
    [Email protected] test]# sed "s/$x/$y/g" bb.txt
    1 Ni hao
    2 Ni Hao
    3 Ta Hao
    4 da Jia Hao
    5 ni men Dou Hao

  • Close Crond sshd network Sysstat rsyslog

    [[email protected] test]# chkconfig--l In addition to the following 5 services Ist|sed-r ' s# (. ). 0. #\1# ' | egrep-v ' Crond|sshd|network|sysstat|rsyslog ' |sed-r ' s# (. ) #chkconfig \1 off# ' |bash

    PS: () extended regular expressions function in sed to memorize a regular expression it contains and can be removed by \1 \2 \3. Usage: \9 ' ( ) '

    (ii), awk One of the Musketeers, Syntax:

    awk parameter ' pattern {action} ' file
    awk parameters ' condition (who to look for) {do what} ' file

    two or one some chestnuts

    First create a test file

    [em Ail protected] test]# cat >>reg.txt<<eof
    Zhang Dandan 41117397:250:100:175
    Zhang Xiaoyu 390320151:155:90:201
    Meng feixue 80042789:250:60:50
    Wu waiwai 70271111:250:80:75
    Liu bingbing 41117483 : 250:100:175
    Wang xiaoai 3515064655:50:95:135
    Zi gege 1986787350:250:168:200
    Li youjiu 918391635:175:75:300
    Lao Nanhai 918391635:250:100:175
    EOF

  • Show Xiaoyu's last name and ID number

    [[email protected] test]# awk '/xiaoyu/{print $1,$2,$3} ' reg.txt
    Zhang Xiaoyu 390320151
    * Rows in column 2nd containing Xiaoyu
    [Email protected] test]# awk ' $2~/xiaoyu/' reg.txt
    Zhang Xiaoyu 390320151:155:90:201

  • Displays the full name and ID number of all people with an ID number beginning with 41

    [[email protected] test]# awk ' $3~/^41/{print $1,$2,$3} ' reg.txt
    Zhang Dandan 41117397
    Liu Bingbing 41117483

  • Show all ID number the last digit is 1 or 5 of the person's full name

    [[email protected] test]# awk ' $3~/1$|5$/{print $1,$2,$3} ' reg.txt
    Zhang Xiaoyu 390320151
    Wu Waiwai 70271111
    Wang Xiaoai 3515064655
    Li Youjiu 918391635
    Lao Nanhai 918391635
    [[email protected] test]# awk ' $3~/[15]$/{print $1,$2,$3} ' reg.txt
    Zhang Xiaoyu 390320151
    Wu Waiwai 70271111
    Wang Xiaoai 3515064655
    Li Youjiu 918391635
    Lao Nanhai 918391635

  • Show Xiaoyu donations. Each value starts with $. such as $520$200$135

    [[email protected] test]# awk '/xiaoyu/{gsub (/:/, "$");p rint $1,$2,$4} ' reg.txt
    Zhang Xiaoyu $155$90$201

Iii. awk Special mode begin and end
    • The contents of the begin{} begin will run before awk reads the contents of the file.
      Test, calculate.

    • The contents of end{}*** end{} will run after awk reads the last line of the file.
      Used to display the final result. Calculate first, end displays the result.
      Formula:
1, a chestnut
    • /etc/services empty lines of statistics
      [[email protected] test]# awk '/^$/{i=i+1}end{print i} '/etc/services
      162. Statistical and computational awk arrays

      i=i+1 = = i++ Count of statistics
      i=i+ $n = = i+= $n Cumulative summation

Some chestnuts
    • 1.9 Simultaneous analysis of access.log files per IP and traffic per IP using awk

      [[email protected] ~]# awk ' {count[$1]++;sum[$1]+=$10}end{for (pol in sum) print "IP:" Pol, "number of times:" Count[pol], "Traffic:" Sum[pol] } ' Access.log
      ip:101.226.61.184 Number of times: 5 Flow: 53581
      ip:27.154.190.158 number of times: 2 Flow: 31602
      ip:218.79.64.76 number of times: 2 Flow: 36438
      ip:114.94.29.165 number of times: 1 Flow: 491

    • How many times does the root user appear in the secure file

      [Email protected] ~]# awk ' $9~/root/{i[$9]++}end{for (A in i) print I[a]} ' secure-20161219
      364611

Eight, Shell Programming Foundation (a), if statement

Grammar:
Single branch if condition 1;then; Action fi
Dual-branch if condition 1; then; action 1 Else Action 2 fi
Multi-branch if condition 1; then move to do 1 Elfi condition 2 then; Action 2 else Action 3 fi

    • A small chestnut of command-line size

      [email protected] ~]# cat tesh.sh
      #bin/bash
      A=$1
      B=$2
      If [$#-ne 2];then
      echo "Please input correct parameters."
      Exit
      Fi

      If [$a-eq $b];then
      echo "$a = $b"
      elif [$a-gt $b];then
      echo "$a > $b"
      Else
      echo "$a < $b"
      Fi

Summary:
1. Conditional expressions
[-d/oldboy]
[-f/oldboy/oldboy.txt]
[10-GT 9] great than >
[10-ge 9] Great equal >=
[10-eq] Equal = =
[10-ne 9] Not equal! =

[9-LT] Less than <
[9-le] Less equal <=

Mans test

[-d/oldboy] = = = Test-d/oldboy
Summary: Special variable location $? Number of parameters $#

(b), for loop
    • Syntax: A for variable in variable received parameter do command done
    • Create 10 users and generate random passwords and append the information to a file (a chestnut)

      #bin/bash
      pass=$ (Date +%n)
      For user in bb{01..10}
      Do
      Useradd $user
      echo $pass |passwd--stdin $user
      echo $user: $pass >>/pass.txt
      Done
      ~

    • Optimized Linux boot project, only keep Crond;sshd;network;rsyslog;sysstat, others are off (another chestnut)

      [[email protected] ~]# for I in $ (chkconfig--list |egrep-v "crond|sshd|rsyslog|sysstat|network" |awk ' {print $} '); Do chkconfig $i off; Done

Advanced knowledge points for Linux (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.