撒的阿坎努斯 发表于 2019-2-17 09:10:21

Linux基础练习

  学习Linux,以下是一些有用的链接:
  镜像下载地址:
https://wiki.centos.org/Download
http://mirrors.aliyun.com
http://mirrors.sohu.com
http://mirrors.163.com
  网站和搜索:
http://www.nginx.org
http://tomcat.apache.org
http://httpd.apache.org
http://www.pyhton.org
https://kubernetes.io/docs/tutorials/
https://docs.docker.com/
http://tldp.org
http://www.slideshare.net
http://www.google.com
  以下是一些习题练习,可以帮助巩固知识点:
  练习一
  1、显示当前时间,格式:2016-06-18 10:20:30
#date +"%F %T"
  2、显示前天是星期几
#date -d "-2 day" +%A
  3、设置当前时间为 2019-8-7 06:05:10
date [-u|--utc|--universal] YY][.ss]]    //date 月日时分年秒

   #date 080706052019.10
  练习二
  1、在本机字符终端登录时,除显示原有信息外,再显示当前登录终端号,主机名和当前时间
vim /etc/issue

\b : Insert the baudrate of the current line.
\d : Insert the current date.
\s : Insert the system name, the name of the operating system.
\l : Insert the name of the current tty line.
\m : Insert the architecture identifier of the machine, eg. i486
\n : Insert the nodename of the machine, also known as the hostname.
\o : Insert the domainname of the machine.
\r : Insert the release number of the OS, eg. 1.1.9.
\t : Insert the current time.
\u : Insert the number of current users logged in.
\U : Insert the string "1 user" or " users" where is the number of current users logged in.
\v : Insert the version of the OS, eg. the build-date etc.
  2、今天18:30自动关机,并提示用户
shutdown ...TIME
#shutdown 18:30please logoff before that time.
#shutdown -h 18:30 "dao dian guan ji,18:30"
  (1) 如何创建?
/data/dir1/x,
/data/dir1/y,
/data/dir1/x/a,
/data/dir1/x/b,
/data/dir1/y/a,
/data/dir1/y/b
  #mkdir /data/dir1/{x,y}/{a,b} -pv
#rm -rf dir1/
#tree /data/
/data/
-- dir1|-- x|   |-- a|-- b
-- y|-- a-- b
  7 directories, 0 files
  (2) 如何创建?
/data/dir2/x,
/data/dir2/y,
/data/dir2/x/a,
/data/dir2/x/b

#mkdir /data/dir2/{x/{a,b},y} -pv
#tree /data/
/data/
`-- dir2
|-- x
|   |-- a
|   `-- b
`-- y
5 directories, 0 files
  (3) 如何创建?
/data/dir3,
/data/dir4,
/data/dir5,
/data/dir5/dir6,
/data/dir5/dir7
  #mkdir /data/dir{3,4,5/dir{6,7}}-pv
#tree /data/
/data/
|-- dir3
|-- dir4
-- dir5|-- dir6-- dir7
  练习三

1. #tr 'a-z' 'A-Z' < /etc/issue > isse.out                        //把指定文件的内容用大写字母显示
2. #who | tr 'a-z' 'A-Z'> who.out                                 //同上
3. #mail -s 'help' rootplease help me to check it, thanks!
> `lsb_release -a`
> EOF
//用户名用变量 $USER   主机名用命令`hostname`
uname -a//查看内核版本
lsb_release -a //查看操作系统版本
#lsb_release -a      
bash: lsb_release: command not found...    //未安装lsb_release 命令
4.#ls /root/ |tr '\n' ' '
anaconda-ks.cfg bush.py Desktop Documents Downloads initial-setup-ks.cfg Music Pictures Public scrip
  练习四

新浪面试题执行以下命令,至少需要什么权限?
#cp /etc/fstab/data/
cp: 至少需要 x 权限
/etc/fstab: 目录需要x权限,文件需要r权限
/data/ : 目录需要x权限,需要w权限
文件夹x权限, 文件r权限,   就可以cp这个文件
  练习五

1、在/data/dir里创建的新文件自动属于webs组,
组apps的成员如:tomcat能对这些新文件有读写权限,
组dbs的成员如:mysql只能对新文件有读权限,
其它用户(不属于webs,apps,dbs)不能访问这个文件夹    //网上参考:https://www.jianshu.com/p/d3f275159bf2
在/date/dir里创建的新文件自动属于webs组
1) #mkdir   -pv   /date/dir
2) #groupadd webs
3) #chgrp   webs    /date/dir
4) #chmod    g+s      /date/dir      
组apps的成员如:tomcat能对这些新文件有读写权限,组dbs的成员如:mysql只能对新文件有读权限,其它用户(不属于webs,apps,dbs)不能访问这个文件夹
1)#groupadd apps;groupadd dbs
2)#useradd -G apps tomcat;useradd -G dbs mysql
3)#setfacl -m g:apps:rw /date/dir/
4)#setfacl -m g:dbs:r /date/dir/
5)#chmod o= /date/dir
把用户xp018970加入到root组的3种方法:
#usermod -aG root xp018970
#gpasswd -a xp018970 root
#groupmems -a xp018970 -g root
     2、备份/data/dir里所有文件的ACL权限到/root/acl.txt中,清除/date/dir中所有ACL权限,最后还原ACL权限

   # getfacl /data/dir/ > /root/acl.txt                                          //备份/data/dir里所有文件的ACL权限到/root/acl.txt
# setfacl -R -b /data/dir/                                                      //清除/date/dir中所有ACL权限      
# setfacl --set-file=/root/acl.txt /data/dir/                              //还原ACL权限
  练习六
1、找出ifconfig “网卡名” 命令结果中本机的IPv4地址
方法一: #ifconfig eth0 |head -2 | tail -1 |tr -s &quot; &quot; : |cut -d: -f4   
192.168.33.135

       方法二: #ifconfig ens33 |grep -Eo "({1,3}\.){3}{1,3}" |head -1      //用扩展的正则表达式来匹配
192.168.33.134
     2、查出分区空间使用率的最大百分比值
#df|grep &quot;^/dev/sd&quot; | tr -s &quot; &quot; % |cut -d% -f5 |sort -nr |head -1
16
     3、查出用户UID最大值的用户名、UID及shell类型
#cat /etc/passwd |cut -d: -f1,3,7 |sort -t: -k2 -nr      //-t 指定分隔符, -k 指定列-n 按数字大小排列 -r 按反向排序
nfsnobody:65534:/sbin/nologin
xp018970:1000:/bin/csh
polkitd:999:/sbin/nologin
unbound:998:/sbin/nologin
libstoragemgmt:997:/sbin/nologin
colord:996:/sbin/nologin
saslauth:995:/sbin/nologin
setroubleshoot:994:/sbin/nologin
chrony:993:/sbin/nologin
geoclue:992:/sbin/nologin
     4、查出/tmp的权限,以数字方式显示
#stat /tmp/ |head -4 |tail -1 |cut -d&quot;/&quot; -f1 |cut -d&quot;(&quot; -f2
1777
     5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
#ss -nt| tr -s&quot; &quot; : |cut -d: -f6 |sort |uniq -c |sort -nr |head -1
3 192.168.35.1
  练习七
  0、 显示/etc/passwd 文件中ID号最大的用户的用户名:            
#sort -t: -k3 -n passwd |tail -1 |cut -d: -f3      //sort    -t: 指定分隔符   -k3 指定参考字段   cut -d: 指定分隔符-f3:
65534
  1、显示/proc/meminfo文件中以大小s开头的行
#grep -i ^s /proc/meminfo
SwapCached:      55976 kB
SwapTotal:       2097148 kB
SwapFree:      1776664 kB
Shmem:             10556 kB
Slab:             129672 kB
SReclaimable:      61180 kB
SUnreclaim:      68492 kB
  2、显示/etc/passwd文件中不以/bin/bash结尾的行
#grep -v&quot;/bin/bash$&quot; /etc/passwd
  3、显示用户rpc默认的shell程序
#grep -i ^rpc /etc/passwd |cut -d: -f7
/sbin/nologin
/sbin/nologin
  4、找出/etc/passwd中的两位或三位数
#grep -o&quot;\&quot;/etc/passwd
12
11
12
100
14
50
99
99
66
65
192
192
  5、显示CentOS7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行
  6、找出“netstat -tan”命令结果中以LISTEN后跟任意多个空白字符结尾的行
  7、显示CentOS7上所有系统用户的用户名和UID
#cat /etc/passwd |cut -d: -f1,3
  8、添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/passwd用户名和shell同名的行
  9、利用df和grep,取出磁盘各分区利用率,并从大到小排序
方法一:#df |grep &quot;^/dev/sd&quot; |sort -t% -k1 -n
/dev/sda1      1038336159296    87904016% /boot
/dev/sda2       52403200 59175364648566412% /
/dev/sda3       31441920   3298031408940   1% /data

方法二:#df |grep "^/dev/sd" |tr -s " " % |cut -d% -f5 |sort -nr
16
12
1
  练习八
1、显示三个用户root、mage、wang的UID和默认shell
#grep -E &quot;^(root|mage|jack)&quot; /etc/passwd|cut -d: -f3,7
0:/bin/bash
501:/bin/bash
502:/bin/bash
  2、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
  3、使用egrep取出/etc/rc.d/init.d/functions中其基名
4、使用egrep取出上面路径的目录名
5、统计last命令中以root登录的每个主机IP地址登录次数
6、利用扩展正则表达式分别表示0-9、10-99、100-199、200-249、250-255
0-9   
10-99   
100-199 1
200-249 2
250-255 25
  7、显示ifconfig命令结果中所有IPv4地址
#ifconfig |grep -Eo &quot;({1,3}.){3}{1,3}&quot;      //使用扩展正则取IP
192.168.33.134
255.255.255.0
192.168.33.255
172.20.119.227
255.255.0.0
172.20.255.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
  8、将此字符串:welcome to magedu linux 中的每个字符去重并排序,重复次数多的排到前面
#echo &quot;welcome to magedu linux&quot; |grep -o |sort |uniq -c |sort -r                     //sort 排序;uniq -c 去重    sort -r 从大到小排序
3 e
2 u
2 o
2 m
2 l
1 x
1 w
1 t
1 n
1 i
1 g
1 d
1 c
1 a


[*]如果用户root存在,显示其默认的shell程序:
id root &> /dev/null && grep &quot;^root\>&quot; /etc/passwd |cut -d: -f7
  /bin/bash

  练习九
  1、编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第20用户的ID之和
#vim sumid.sh
#!/bin/bash

n1=`head -10 /etc/passwd| tail -1 |cut -d: -f3`
n2=`head -20 /etc/passwd| tail -1 |cut -d: -f3`
echo "The 10th user id is $n1."
echo "The 20th user id is $n2."
let total=n1+n2
echo "The 10th user id and the 20th user is $total."
  2、编写脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计算这两个文件中所有空白行之和
#!/bin/bash

    n1=`grep "^$" $1 |wc -l`
n2=`grep "^$" $2 |wc -l`
echo "The first file with total blank line is $n1."
echo "The second file with total blank line is $n2."
let total=n1+n2
echo "$1 and $2 has total blank lines are $total."   
  3、编写脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级子目录和文件
#!/bin/bash

d1=`ls -l -d /etc/* |grep "^d" |wc -l`
d2=`ls -l -d /var/* |grep "^d" |wc -l`
d3=`ls -l -d /usr/* |grep "^d" |wc -l`
echo "/etc has dir $d1."
echo "/var has dir $d2."
echo "/usr has dir $d3."
let dtotal=d1+d2+d3
echo "Total dir is $dtotal."
f1=`ls -l -d /etc/* |grep "-" |wc -l`
f2=`ls -l -d /var/* |grep "-" |wc -l`
f3=`ls -l -d /usr/* |grep "-" |wc -l`
echo "/etc has file $f1."
echo "/var has file $f2."
echo "/usr has file $f3."
let ftotal=f1+f2+f3
echo "Total file is $ftotal."


页: [1]
查看完整版本: Linux基础练习