美文网首页
linux练习

linux练习

作者: xm11211 | 来源:发表于2019-02-13 15:18 被阅读0次

    1.编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中

    1. 编写脚本
    [root@centos6 bin]vim backup.sh
    #!/bin/bash
    echo -e "\e[1;31mStart backup...\e[0m"   
    sleep 2
    cp -av /etc /root/etc`date +%F` &> /dev/null
    echo -e "\e[1;31mBackup is finished\e[0m"
    
    1. 测试结果
    [root@centos6 bin]backup.sh
    Start backup...
    Backup is finished
    [root@centos6 bin]ll -hd /root/etc2019-02-13/
    drwxr-xr-x. 125 root root 12K Feb 13 04:44 /root/etc2019-02-13/
    [root@centos6 bin]ll -hd /etc
    drwxr-xr-x. 125 root root 12K Feb 13 04:44 /etc
    

    2.编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

    1. 编写脚本
    [root@centos6 bin]vim disk.sh
    #!/bin/bash
    df | grep -E "^/dev/s[hd][a-z]" | grep -oE ' [0-9]+'% | grep -oE '[0-9]+' | sort -nr | head -1
    
    1. 测试结果
    [root@centos6 bin]disk.sh 
    12
    [root@centos6 bin]df -h
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda2        48G  5.1G   41G  12% /
    tmpfs           491M   80K  491M   1% /dev/shm
    /dev/sda1       976M   35M  891M   4% /boot
    /dev/sda3        29G   44M   28G   1% /data
    /dev/sr1        3.7G  3.7G     0 100% /media/CentOS_6.9_Final_
    /dev/sr0        2.1G  2.1G     0 100% /media/CentOS_6.9_Final
    

    3.编写脚本/root/bin/nologin.sh和login.sh,实现禁止和允许普通用户登录系统

    1. 编写脚本
    [root@centos6 bin]vim nologin.sh
    [ ! -e '/etc/nologin' ] && touch /etc/nologin && echo "DenyAccountLogon"
    [root@centos6 bin]vim login.sh 
    #!/bin/bash
    [ -e '/etc/nologin' ] && `rm -rf /etc/nologin` && echo "Allow login account"
    

    2.测试结果

    [root@centos6 bin]nologin.sh
    DenyAccountLogon
    [root@centos6 bin]ls /etc | grep nologin
    nologin
    [root@centos6 ~]ssh xm11211@192.168.48.128
    xm11211@192.168.48.128's password: 
    Connection closed by 192.168.48.128
    
    [root@centos6 bin]login.sh
    Allow login account
    [root@centos6 bin]ls /etc | grep nologin
    [root@centos6 bin]!ssh
    ssh xm11211@192.168.48.128
    xm11211@192.168.48.128's password: 
    [xm11211@centos6 ~]
    

    4.查找/var目录下不属于root、lp、gdm的所有文件

    [root@centos6 ~]find /var -not  \( -user root -o -user lp -o -user gdm -o -type d \)  -ls
    394626    4 -rw-r--r--   1 xm11211  xm11211        32 Dec 28 14:35 /var/cache/gdm/xm11211/dmrc
    264307    4 -rw-------   1 rpc      rpc           852 Jan 27 13:32 /var/cache/rpcbind/rpcbind.xdr
    264308    4 -rw-------   1 rpc      rpc           164 Jan 27 13:32 /var/cache/rpcbind/portmap.xdr
    264350    4 -rw-------   1 postfix  postfix        33 Feb 15 10:43 /var/lib/postfix/master.lock
    264544    4 -rw-r--r--   1 ntp      ntp             8 Feb 11 18:42 /var/lib/ntp/drift
    ...
    

    5.统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和sed两种方法分别实现)

    [root@centos6 ~]grep -Eow '[[:alpha:]]+' /etc/init.d/functions | sort | uniq -c | sort -n
    61 return
    65 echo
    72 fi
    77 then
    83 if
    ...
    
    [root@centos6 ~]sed -r 's/[^[:alpha:]]+/\n/g' /etc/init.d/functions | sort | uniq -c | sort -n
    61 return
    72 fi
    73 echo
    75 pid
    77 then
    83 if
    ...
    

    6.利用sed 取出ifconfig命令中本机的IPv4地址

    [root@centos7 ~]ifconfig | sed -r '2!d;s/.*inet (.*)  netmask.*/\1/' 
    192.168.48.129
    [root@centos6 ~]ifconfig | sed -r '2!d;s/.*r:(.*) B.*/\1/'
    192.168.48.128 
    

    7.在CentOS7上编译安装apache 2.4源码包,并启动此服务(最简化版,待完善)

    1.关闭firewalld并设置开机禁止启动

    [root@centos7 bin]systemctl stop firewalld
    [root@centos7 bin]systemctl disable firewalld
    

    2.关闭SElinux

    [root@centos7 bin]setenforce 0
    [root@centos7 bin]vim /etc/selinux/config
    # This file controls the state of SELinux on the system.
    # SELINUX= can take one of these three values:
    #     enforcing - SELinux security policy is enforced.
    #     permissive - SELinux prints warnings instead of enforcing.
    #     disabled - No SELinux policy is loaded.
    SELINUX=disabled
    

    3.安装开发包组

    [root@centos7 bin]yum groupinstall "development tools"
    

    4.编译安装httpd

    [root@centos7 ~]tar -xf httpd-2.4.38.tar.gz
    [root@centos7 ~]cd httpd-2.4.38/
    [root@centos7 httpd-2.4.38]./configure --help
    `configure' configures this package to adapt to many kinds of systems.
    
    Usage: ./configure [OPTION]... [VAR=VALUE]...
    
    To assign environment variables (e.g., CC, CFLAGS...), specify them as
    VAR=VALUE.  See below for descriptions of some of the useful variables.
    
    Defaults for the options are specified in brackets.
    
    Configuration:
      -h, --help              display this help and exit
          --help=short        display options specific to this package
          --help=recursive    display the short help of all the included packages
      -V, --version           display version information and exit
      -q, --quiet, --silent   do not print `checking ...' messages
          --cache-file=FILE   cache test results in FILE [disabled]
      -C, --config-cache      alias for `--cache-file=config.cache'
      -n, --no-create         do not create output files
          --srcdir=DIR        find the sources in DIR [configure dir or `..']
    [root@centos7 httpd-2.4.38]./configure --prefix=/app --sysconfdir=/etc/httpd22
    [root@centos7 httpd-2.4.38]make -j 4 && make install
    

    5.输出二进制程序

    [root@centos7 httpd-2.4.38]vim /etc/profile.d/env.sh
    PATH=/app/bin:$PATH
    [root@centos7 httpd-2.4.38]. /etc/profile.d/env.sh
    

    6.启动httpd

    [root@centos7 httpd-2.4.38]apachectl start
    

    相关文章

      网友评论

          本文标题:linux练习

          本文链接:https://www.haomeiwen.com/subject/ukcdeqtx.html