马哥教育网络班21期+第15周课程练习

1、总结sed和awk的详细用法;

sed的详细用法

awk的详细用法

2、删除/boot/grub/grub.conf文件中所有行的行首的空白字符;

# sed 's/^[[:space:]]*//' /boot/grub/grub.conf

3、删除/etc/fstab文件中所有以#开头,后跟至少一个空白字符的行的行首的#和空白字符;

# sed 's/^#[[:space:]]\+//' /etc/fstab

4、把/etc/fstab文件的奇数行另存为/tmp/fstab.3;

# sed 'n;d' /etc/fstab > /tmp/fstab.3
# awk '{if (NR%2==0) next; print}' /etc/fstab > /tmp/fstab.3

5、echo一个文件路径给sed命令,取出其基名;进一步的,取出其路径名;

# echo "/tmp/test/fstab" | sed 's/[^/]\+\/\?$//'
# echo "/tmp/test/fstab" | sed 's/\(\/.*\/\)//'

6、统计指定文件中所有行中每个单词出现的次数;

# awk '{for(i=1;i<=NF;i++){count[$i]++}}END{for(j in count) {print j,count[j]}}' /etc/fstab

7、统计当前系统上所有tcp连接的各种状态的个数;

# netstat -tan | awk '/^tcp\>/{test[$NF]++}END{for(j in test) { print j,test[j]}}'

8、统计指定的web访问日志中各ip的资源访问次数;

# awk '{ip[$1]++}END{for(i in ip) {print i,ip[i]}}' /var/log/httpd/access_log

9、写一个脚本:定义一个数组,数组元素为/var/log目录下所有以.log结尾的文件的名字;显示每个文件的行数;

#!/bin/bash
declare -a test
test=$(ls /var/log/*.log)
for i in $(seq 0 $[${#test[*]}-1]); do
    wc -l ${test[$i]}
done

10、写一个脚本,能从所有同学中随机挑选一位同学回答问题;进一步的,可接受一个参数,作为要挑选的同学的个数;

从给定的同学中随机挑选一位回答问题

#!/bin/bash
echo "Please enter some name of students,and the separator is space!!!" 
read -a student
random=$((RANDOM % ${#student[@]}))
echo "Please ${student[$random]} answer the question!"

可接受一个参数,作为要挑选同学的个数

#!/bin/bash
echo "Please enter some name of students,and the separator is space!!!" 
read -a student
read -p "Please enter the number of students to answer question !" num
if [ $num -gt ${#student[@]} ]; then
    echo "The number should be smaller than the total of you enter!"
    exit
fi

echo "The list to answer the question is:"
for ((i=0;i<num;i++)); do
    random=$((RANDOM % ${#student[@]}))
    echo ${student[$random]}
done

11、授权centos用户可以运行fdisk命令完成磁盘管理,以及使用mkfs或mke2fs实现文件系统管理;

# visudo
Cmnd_Alias DISK = /sbin/fdisk, /sbin/mkfs, /sbin/mke2fs
centos  ALL=(root)  DISK

12、授权gentoo用户可以运行逻辑卷管理的相关命令;

# visudo
Cmnd_Alias LVMMANAGE = /sbin/*create, /sbin/*reduce,/sbin/*scan, /sbin/*display, /sbin/fsck, /sbin/mke2fs
gentoo  ALL=(root)  LVMMANAGE

13、基于pam_time.so模块,限制用户通过sshd服务远程登录只能在工作时间进行;

# vim /etc/pam.d/sshd
account required pam_time.so -->//要位于文件的第一行
# vim /etc/security/time.conf 
*;*;*;MoTuWeThFr0900-1800 -->//表示工作时间的9点到下午6点

14、基于pam_listfile.so模块,定义仅某些用户,或某些组内的用户可以登录系统。

# vim /etc/sshd_userlist -->//在此文件中添加一些用户来登录系统
# vim /etc/pam.d/sshd
auth required pam_listfile.so item=user sense=allow file=/etc/sshd_userlist onerr=succeed

原创文章,作者:Jeason,如若转载,请注明出处:http://www.178linux.com/58298

(0)
JeasonJeason
上一篇 2016-11-14 08:28
下一篇 2016-11-14 08:28

相关推荐

  • 大概认识linux

             linux简介 其实对Linux系统不是很了解,都是在百度和谷歌搜索出来,才发现Linux很多版本。各版本各优缺点。首先简单说下发展史,Linux在1991年10月5号(这是第一次正式向外公布的时间)在芬兰诞生,以后借助于Internet网络向全世界各地传播,由计算机爱好者的再次开发新功能和…

    Linux干货 2016-05-29
  • RAID磁盘冗余探究

    RAID磁盘冗余探究 RAID的全称是Redundant Arrays of Inexpensive Disk,可以翻译为廉价的磁盘冗余阵列。由加利福尼亚大学伯克利分校的一位教授在1988年提出,是一种磁盘管理方式。其产生的初衷是为了降低成本,但是为了达到RAID的功能,其造价并不便宜,所以后来改为Redundant Arrays of Independen…

    2017-08-26
  • Linux软件包管理之rpm和yum的使用

    rpm命令的使用 什么是RPM? rpm:RPM Package Manager (原Redhat Package Manager) Linux 软件包管理工具 特定的程序由应用程序组成;     GPL:源码     glibs:标准的C库 常见Linux发行版的软件包管理器:…

    Linux干货 2016-08-25
  • 使用httpd反向代理模块实现tomcat负载均衡集群(下)

    上一篇讲解了http使用mod_http和mod_ajp代理模块实现tomcat负载均衡,下面我们来讲解使用http的mod_jk实现taomcat的负载均衡集群: 注意:http的mod_jk是第三方扩展模块,在新http版本中以不支持,在httpd 1.3和2.0效果较好 6、使用mod_jk实现tomcat负载均衡集群 6.1安装mod_jk [roo…

    Linux干货 2015-07-21
  • CentOS 系统启动流程

    CentOS 系统启动流程 一、Linux系统的组成部分:内核+根文件系统 1.内核: 进程管理:进程之间的通信为:IPC(Inter Process Communication)机制,有消息队列、semerphor、shm、socket(跨主机之间的通信) 内存管理; 网络管理; 文件系统; 驱动程序; 安全功能; 2.运行中的系统环境可分为两层:内核空间…

    Linux干货 2016-09-13
  • N25-第16周博客作业

    1、源码编译安装LNMP架构环境;     安装nginx:      1)安装依赖包 ]# yum groupinstall “Development Tools” “Development Libraries” -y ]# yum install wget openssl-devel ncurses-de…

    2017-05-21

评论列表(1条)

  • 马哥教育
    马哥教育 2016-11-16 15:47

    写的很好,排版也很棒,希望能够再接再厉