zoukankan      html  css  js  c++  java
  • linux基础:第三关课前考试题整理

    1.如何取得/etc/hosts 文件的权限对应的数字内容,如-rw-r--r-- 为 644, 要求使用命令取得
    644 这样的数字。

     1 [root@server ~]# stat /etc/hosts
     2   File: "/etc/hosts"
     3   Size: 178             Blocks: 8          IO Block: 4096   普通文件
     4 Device: 803h/2051d      Inode: 130078      Links: 1
     5 Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
     6 Access: 2015-12-24 15:50:01.351799952 +0800
     7 Modify: 2015-12-02 21:43:38.484600576 +0800
     8 Change: 2015-12-02 21:43:38.484600576 +0800
     9 #方法1:后向引用法
    10 [root@server ~]# stat /etc/hosts|sed -nr '4s#^.*: (0(.*)/-.*$#1#gp'
    11 644
    12 #方法2:awk多分隔符取列
    13 [root@server ~]# stat /etc/hosts|awk -F '[0/]' 'NR==4 {print $2}'
    14 644
    15 #方法3:awk多分隔符取列
    16 [root@server ~]# stat /etc/hosts|sed -n '4p'|awk -F '[0/]' ' {print $2}'     
    17 644
    18 #方法4:命令直接取
    19 [root@server ~]# stat -c %a /etc/hosts    # %a表示访问权限八进制
    20 644
    21 #方法5:扩展如何将ls -l命令中的权限字符转换为八进制?
    22 [root@server ~]# ll /etc/hosts|cut -c 2-10|tr "rwx-" "4210"|awk -F "" '{print $1+$2+$3 $4+$5+$6 $7+$8+$9}'
    23 644
    24 #知识点:1.tr字符替换的使用;2.cut -c截取字符;3.awk指定分隔符为空。

    2.linux下通过mkdir命令创建一个新目录/oldboy/ett,它的硬链接数是多少,为什么?

    如果在/oldboy/ett下面再创建一个目录test,再问/oldboy/ett的硬链接数是多少?为什么?

     1 [root@server oldboy]# ls -lid /oldboy/ett/
     2 144998 drwxr-xr-x 3 root root 4096 12月 21 11:54 /oldboy/ett/
     3 [root@server oldboy]# cd /oldboy/ett/
     4 [root@server ett]# ls -lai
     5 总用量 12
     6 144998 drwxr-xr-x 3 root root 4096 12月 21 11:54 .
     7 130085 drwxrwxrwx 4 root root 4096 12月 21 11:52 ..
     8 144999 drwxr-xr-x 2 root root 4096 12月 21 11:54 test
     9 [root@server ett]# cd test/
    10 [root@server test]# ls -lai
    11 总用量 8
    12 144999 drwxr-xr-x 2 root root 4096 12月 21 11:54 .
    13 144998 drwxr-xr-x 3 root root 4096 12月 21 11:54 ..
    14 [root@server test]# 
    15 
    16 #备注:inode节点号相同,则硬链接数为/oldboy/ett算一个,/ett目录下隐藏文件.算一个,还有/ett/test目录下..算一个,则在不创建/test目录情况下,/oldboy/ett的硬链接数为2,在创建了/oldboy/ett/test目录后,硬链接数为3。

    3.请执行命令取出Linux中eth0的IP地址(请用cut,有能力者也可分别用awk,sed命令答)。

     1 [root@server test]# ifconfig eth0
     2 eth0      Link encap:Ethernet  HWaddr 00:0C:29:93:5D:E4  
     3           inet addr:192.168.1.11  Bcast:192.168.1.255  Mask:255.255.255.0
     4           inet6 addr: fe80::20c:29ff:fe93:5de4/64 Scope:Link
     5           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
     6           RX packets:42296 errors:0 dropped:0 overruns:0 frame:0
     7           TX packets:21437 errors:0 dropped:0 overruns:0 carrier:0
     8           collisions:0 txqueuelen:1000 
     9           RX bytes:3458185 (3.2 MiB)  TX bytes:2862338 (2.7 MiB)
    10 #使用sed替换
    11 [root@server test]# ifconfig eth0|sed -nr '2s#^.*:(.*)B.*$#1#gp'
    12 192.168.1.11  
    13 #使用awk取列
    14 [root@server test]# ifconfig eth0|awk -F '[: ]+' 'NR==2 {print $4}'
    15 192.168.1.11    ## NR代表行,$4代表取第四列;
    16 #使用sed awk组合使用
    17 [root@server test]# ifconfig eth0|sed -n '2p'|awk -F '[: ]+' '{print $4}'
    18 192.168.1.11  # “+”代表分隔括号中的分隔符会在本行中出现多次;
    19 #使用cut命令
    20 [root@server test]# grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0|cut -d= -f2
    21 192.168.1.11

    4.请给出默认情况eth0网卡配置文件路径及客户端DNS的路径。

     1  1 #/etc/sysconfig/network-scripts/ifcfg-eth0:
     2  2 DEVICE=eth0 #设备
     3  3 HWADDR=00:0C:29:93:5D:E4 #对于虚拟机克隆需要将这项删除,防止冲突
     4  4 TYPE=Ethernet #网络类型
     5  5 UUID=4f58613e-e829-49a8-86da-f0444d86781d  ##对于虚拟机克隆需要将这项删除,防止冲突
     6  6 ONBOOT=yes #开机启动
     7  7 NM_CONTROLLED=yes
     8  8 #BOOTPROTO=dhcp
     9  9 BOOTPROTO=static #静态IP
    10 10 IPADDR=192.168.1.11 #IP地址
    11 11 NETMASK=255.255.255.0 #子网掩码
    12 12 GATEWAY=192.168.1.1 #网关
    13 13 DNS1=192.168.1.1  #DNS
    14 14 
    15 15 #/etc/resolv.conf
    16 16 ;generated by /sbin/dhclient-script   #由这个脚本生成
    17 17 search localdomain oldboy.local #搜索域
    18 18 nameserver 192.168.1.1  #名称服务器,写入会被覆盖

    5.查找当前目录下所有文件,并把文件中的www.google.com字符串替换成www.baidu.com;

     1 #模拟创建文件
     2 [root@server ~]# cat >>newfile<<EOF
     3 fgsdgsdfdsmldsmgdls,,,,;sdkgsdwww.google.comgelewgmewlgmewgwelgewww.google.com
     4 gsgewgewgeomomodsgmegipweigtepvsnvdvd dfjwwewww.google.com
     5 rewrwero39r309,.;l'121'l'..www.google.com
     6 erjeowew.emglwewetwewww.google.com
     7 35325325230www.google.com
     8 EOF
     9 #sed替换
    10 [root@server ~]# find ./ -type f -name "newfile"|xargs sed -i 's#www.google.com#www.baidu.com#g
    11 #查看文件是否替换
    12 [root@server ~]# cat newfile 
    13 fgsdgsdfdsmldsmgdls,,,,;sdkgsdwww.baidu.comgelewgmewlgmewgwelgewww.baidu.com
    14 gsgewgewgeomomodsgwww.baidu.commegipweigtepvsnvdvd dfjwwewww.baidu.com
    15 rewrwero39r309,.www.baidu.com;l'121'l'..www.baidu.com
    16 erjwww.baidu.comeowew.emglwewetwewww.baidu.com
    17 353www.baidu.com25325230www.baidu.com
    18 #扩展:在生产场景,如果遇到木马,广告脚本植入,所有网络文件,以及用户文件都被植入,不可代码重新铺设;使用备份还原会损失上个备份点到备份还原完成的数据,不可取,此时就可以使用sed -i批量将脚本删除,而不影响正常的业务。

    6.问题:如何赋予oldboy文件-rw-r--r-x权限属性

     1 [root@server ~]# ls -ld oldboy
     2 drwxr-xr-x 2 root root 4096 12月 24 17:50 oldboy #原目录权限为755
     3 方法1:数字修改方法
     4 [root@server ~]# chmod 645 oldboy
     5 [root@server ~]# ls -ld oldboy   
     6 drw-r--r-x 2 root root 4096 12月 24 17:50 oldboy
     7 方法2:字符权限修改方法
     8 [root@server ~]# chmod u-x,g-x oldboy ##等价于chmod u=rw,g=r,o=rx
     9 [root@server ~]# ls -ld oldboy       
    10 drw-r--r-x 2 root root 4096 12月 24 17:50 oldboy
    11 #注释
    12 r===4
    13 w===2
    14 x===1

    7.执行下面命令时发现提示需要输入密码,请问提示输入的密码是哪个用户的密码;

    1 [root@server ~]# su - test
    2 [test@server ~]$ sudo su - oldboy
    3 [sudo] password for test:    ##输入test的密码

    8.问题:请问在一个命令上加什么参数可以实现下面命令的内容在同一行输出。

     1 [root@server ~]# echo "oldboy";echo "oldboy"
     2 oldboy
     3 oldboy
     4 [root@server ~]# echo -n "oldboy";echo "oldboy"   ## -n不换行输出
     5 oldboyoldboy
     6 [root@server ~]# echo -e "oldboy	oldgirl"   ## -e:解析转义字符,	 tab   
     7 oldboy  oldgirl
     8 [root@server ~]#
     9 [root@server ~]# echo -e "oldboy
    oldgirl"   ##
    回车
    10 oldboy
    11 oldgirl

    9.问题:请给出如下格式的date命令 例:11-02-26。在给出实现按周输出 比如:周六输出6,请分别给出命令。

     1 #年短格式
     2 [root@server ~]# date +%y-%m-%d 
     3 15-12-24
     4 #年长格式
     5 [root@server ~]# date +%Y-%m-%d
     6 2015-12-24
     7 [root@server ~]# date +%F
     8 2015-12-24
     9 #显示周几
    10 [root@server ~]# date +%u
    11 4
    12 [root@server ~]# date +%w
    13 4
    14 #拓展:日志轮询可以使用使用定时任务定期删除7天前的日志;也可以使用*.log.$(date +%w)创建,轮一周新的日志就自动覆盖就的日志文件。还可以表示为*.log.`date +%w`来创建。其中`date +%w`===$(date +%w) 

    10.问题:当从root用户切到普通用户时,执行ifconfig会提示如下。

    [oldboy@student ~]$ ifconfig
    -bash: ifconfig: command not found
    提示: c58 会遇到, c64 没有此问题

    1 #环境变量PATH的问题
    2 #可以临时生效
    3 export PATH=/sbin/ifconfig:$PATH
    4 #也可以永久生效,将以上语句写入/etc/profile中 

    11.扩展问题:打印三天前的日期格式如:2015-12-10

     1 [root@server ~]# date +%F
     2 2015-12-24
     3 [root@server ~]# date +%F -d "-3day" ##显示三天前的日期
     4 2015-12-21
     5 #date的其他用法:
     6 #现在时间
     7 [root@server ~]# date +%F %T 
     8 2015-12-24 21:34:19
     9 #1表示过去三天时间
    10 [root@server ~]# date +%F %T -d "-3day"
    11 2015-12-21 21:35:31
    12 #2表示将来三天时间
    13 [root@server ~]# date +%F %T -d "+3day"
    14 2015-12-27 21:36:03

    12.已知/oldboy/test.txt文件内容如下:

    编辑文件内容:

    [root@server oldboy]# echo "
    > fsdfdsgdsgsdoldboy
    >
    > gdgdsgsdgweewgewg
    >
    > whalodlggoyw" >>test.txt
    [root@server oldboy]# ls
    test.txt
    [root@server oldboy]# cat test.txt


    fsdfdsgdsgsdoldboy

    gdgdsgsdgweewgewg

    whalodlggoyw

    请将文件中空行过滤掉;

     1 #方法1:
     2 [root@server oldboy]# grep -v '^$' test.txt  
     3 fsdfdsgdsgsdoldboy
     4 gdgdsgsdgweewgewg
     5 whalodlggoyw
     6 #方法2:
     7 [root@server oldboy]# grep -o '.*' test.txt 
     8 fsdfdsgdsgsdoldboy
     9 gdgdsgsdgweewgewg
    10 whalodlggoyw
    11 #方法3:使用sed显示
    12 [root@server oldboy]# sed '/^$/d' test.txt 
    13 fsdfdsgdsgsdoldboy
    14 gdgdsgsdgweewgewg
    15 whalodlggoyw
    16 #方法4:使用awk
    17 [root@server oldboy]# awk '/^[^$]/' test.txt 
    18 fsdfdsgdsgsdoldboy
    19 gdgdsgsdgweewgewg
    20 whalodlggoyw
    21 #方法5:使用awk NF判断
    22 [root@server oldboy]# awk 'NF>0 {print $0}' test.txt 
    23 fsdfdsgdsgsdoldboy
    24 gdgdsgsdgweewgewg
    25 whalodlggoyw

    13.已知/oldboy/ett.txt文件内容为:

    oldboy
    olldbooooy
    qerwerwer

    请使用grep或egrep正则匹配的方式过滤出前两行内容

     1 #方法1:过滤以“o”开头的
     2 [root@server oldboy]# grep '^o' ett.txt 
     3 oldboy
     4 olldbooooy
     5 #方法2:过滤以“y”结尾的
     6 [root@server oldboy]# grep 'y$' ett.txt   
     7 oldboy
     8 olldbooooy
     9 方法3:排除以“^q”开头的
    10 [root@server oldboy]# grep -v '^q' ett.txt 
    11 oldboy
    12 olldbooooy

    14.描述下列路径的内容是做什么的?

    1 /var/log/messages #系统日志
    2 /var/log/secure#安全日志
    3 /var/spool/clientmqueue#邮件临时目录
    4 /proc/interrupts#中断相关文件
    5 /etc/fstab#磁盘文件系统开机自启动文件
    6 /etc/profile#环境变量配置文件

    15.如何快速查到ifconfig的全路径,请给出命令;

     1 [root@server oldboy]# which ifconfig    #which
     2 /sbin/ifconfig
     3 [root@server oldboy]# whereis -b ifconfig    #whereis -b  -b:表示查二进制命令
     4 ifconfig: /sbin/ifconfig
     5 [root@server oldboy]# locate ifconfig
     6 /sbin/ifconfig
     7 /usr/lib/python2.6/site-packages/Sphinx-1.0.8-py2.6.egg/sphinx/ext/ifconfig.py
     8 /usr/lib/python2.6/site-packages/Sphinx-1.0.8-py2.6.egg/sphinx/ext/ifconfig.pyc
     9 /usr/lib/python2.6/site-packages/Sphinx-1.0.8-py2.6.egg/sphinx/ext/ifconfig.pyo
    10 /usr/lib/python2.6/site-packages/sphinx/ext/ifconfig.py
    11 /usr/lib/python2.6/site-packages/sphinx/ext/ifconfig.pyc
    12 /usr/lib/python2.6/site-packages/sphinx/ext/ifconfig.pyo
    13 /usr/sbin/pifconfig
    14 /usr/share/man/man8/ifconfig.8.gz
    15 /usr/share/man/man8/pifconfig.8.gz
    16 /usr/share/man/pt/man8/ifconfig.8.gz
    17 /usr/share/zsh/4.3.11/functions/_ifconfig
    18 [root@server oldboy]# sudo find / -type f -name ifconfig ##find查找,不加sudo,会查找proc等
    19 /sbin/ifconfig

    16.每周日上午 9: 30 来老男孩 linux 培训上课(用/oldboy.sh 代替),请用 linux 定时任务命
    令表示。

     1 30 09 * * 0 sh /oldboy.sh 

    17. 请给出查看当前哪些用户在线的 linux 命令。

     1 [root@server oldboy]# who ## 2 root pts/0 Dec 23 19:42 (192.168.1.151) 

    18. 公司一开发人员申请对某一服务器 10 天的普通用户权限,请问你如何操作?

    1 [root@server ~]# sudo useradd test2 -e `date +%F -d "+10day"`
    2 [root@server ~]# sudo tail -2 /etc/shadow
    3 test:!!:16777:0:99999:7:::
    4 test2:!!:16793:0:99999:7::16803:   ##10天有效期,时间到取消账户

    19.请给出正确的关机和重起服务器的命令。

     1 halt 2 shutdown -h now 3 reboot 4 shutdown -r 5 init 0  

    20.请写出下面 linux SecureCRT 命令行快捷键命令的功能?

     1 Ctrl + a  命令首
     2 Ctrl + c  取消进程或者命令
     3 Ctrl + d  logout
     4 Ctrl + e  命令尾
     5 Ctrl + l  清屏
     6 Ctrl + u  删除光标以前
     7 Ctrl + k  删除光标以后
     8 tab       命令、路径补全
     9 Ctrl+shift+c  复制
    10 Ctrl+shift+v  粘贴 
  • 相关阅读:
    网页打开qq
    正则表达式
    微信小程序 如何使用globalData
    微信小程序中常见的typeof cb == “function” && cb(that.globalData.userInfo)
    MongoDB
    Elasticsearch学习
    Kafka学习之路 (一)Kafka的简介
    mybatis sql语句中转义字符
    定时任务
    mybatis insert into 返回id,useGeneratedKeys,keyColumn,keyProperty,parameterType
  • 原文地址:https://www.cnblogs.com/hackerer/p/5073469.html
Copyright © 2011-2022 走看看