面试题-5

1.linux下常见的压缩包类型有哪些

格式 压缩工具
.zip zip压缩工具
.gz gzip压缩工具,只能压缩文件,会删除原文件(通常配合tar使用)
.bz2 bzip2压缩工具,只能压缩文件,会删除原文件(通常配合tar使用)
.tar.gz 先使用tar命令归档打包,然后使用gzip压缩
.tar.bz2 先使用tar命令归档打包,然后使用bzip压缩

2.将/etc/hosts文件用tar格式打包。

[root@chengyinwu ~]# tar czf hosts.tar.gz /etc/hosts

3.使用tar打包/var/log/目录。

[root@chengyinwu ~]# tar czf log.tar.gz /var/log/

4.使用zip打包/etc目录。

[root@chengyinwu ~]# zip -r etc.zip  /etc/

5.查看/var/log/目录的压缩包中有哪些内容。

[root@chengyinwu ~]# tar tf log.tar.gz

6.将/var/log/目录解压到/opt目录中。

[root@chengyinwu ~]# tar xf log.tar.gz -C /opt/

7.查看/etc/目录的压缩包的压缩比率。

[root@chengyinwu ~]# du -h etc.zip 
14M	etc.zip
[root@chengyinwu ~]# du -sh /etc/
34M	/etc/
14/35=41%

8.查看/etc/目录的压缩包的内容是否完好。

[root@chengyinwu ~]# zip -T etc.zip 
test of etc.zip OK

9.解压/etc/目录到/opt目录中。

[root@chengyinwu ~]# unzip etc.zip -d /opt/

10.用zip打包/opt目录,要求不显示打包过程。

[root@chengyinwu ~]# zip -q -r opt.zip /opt/

11.打包/etc/目录,要求是.bz2格式

[root@chengyinwu ~]# tar cjf etc.tar.bz2 /etc/

12.打包/var/log目录,要求是.xz格式

[root@chengyinwu ~]# tar cJf log.tar.xz /var/log/

13.使用tar命令打包/etc/时,会出现一个删根的操作,怎样打包不会进行删根的操作

[root@chengyinwu ~]# tar czPf etc.tar.gz /etc/

14.打包/etc/目录,要求不打包/etc/hosts这个文件。

[root@chengyinwu ~]# tar czf etc.tar2.gz /etc/ --exclude=etc/hosts

15.打包/etc/目录,要求不打包/etc/hosts和/etc/hostname这两个文件。

[root@chengyinwu ~]# tar czf etc.tar3.gz /etc/ --exclude=etc/hosts --exclude=etc/hostname

16.已知文件oldboy.zip,请问在不解压的情况下,怎样查看该文件的内容。

[root@chengyinwu ~]# echo "oldboyedu.com" >oldboy
[root@chengyinwu ~]# zip  oldboy.zip oldboy 
  adding: oldboy (stored 0%)
[root@chengyinwu ~]# zcat oldboy.zip
oldboyedu.com

17.打包/etc/目录,命令以ip地址+当前时间方式的压缩包: 比如: 10.0.0.98_2019-4-23_etc.tar.gz

[root@chengyinwu ~]# tar czPf $(ifconfig eth0 |awk 'NR==2 {print $2}')_$(date +%F).tar.gz /etc/

一、基础

1.1 什么是绝对路径,什么是相对路径(2 分)

  • 绝对路径:只要从根开始的路径都算是绝对路径,比如:a.txt

  • 相对路径:相对于当前的位置,比如:.a.txt

1.2 简述命令执行的流程(2 分)

  • 检查命令是不是从绝对路径执行
  • 检查是否存在别名设置
  • 看是内置还在外置命令
  • 若是内置,可直接执行,是外置的话,查询是否有缓存
  • 通过$PATH变量查找命令,若有则执行,无就会报错

1.3 简述软连接与硬连接的区别(2 分)

  • 软链接:相当于快捷方式;删除源文件则软链接失效;使用ln -s 创建;可以跨分区;inode 与源文件不同;支持目录
  • 硬链接:相当于副本;删除源文件硬链接无影响;使用ln 创建;不可以跨分区;inode与源文件相同;不支持目录

1.4 创建目录/old/boy/(2 分)

mkdir -p /old/boy/

1.5 使用一条命令在/old/boy/目录下创建 bgx1.txt、bgx2.txt、bgx3.txt 三个文件(2 分)

touch /old/boy/bgx{1..3}.txt

1.6 将/old/boy/目录下所有内容复制到/root/目录下(2 分)

cp -r /old/boy/* /root/

1.7 删除/root 下所有 a 开头的文件及目录(2 分)

rm -rf /root/a*

1.8 在/tmp/目录下面创建一个 etc 的软连接 etc-test(2 分)

ln -s /etc /tmp/etc-test

1.9 写出 20 个学过的命令及含义(20 分)

1.ls 列出当前目录下的文件和目录
2.pwd 打印出当前目录所在的路径
3.cat 查看文件的内容
4.cp 复制
5.mv 移动
6.cd 切换目录
7.mkdir 创建目录
8.touch 创建文件
9.vim 用于编辑
10.rm 删除
11.echo 输出
12.tr  替换
13.file 查看文件类型
14.which 查命令所在的路径
15.useradd 创建用户
16.grep 过滤
17.sed 用于增删改查
18.awk 用于过滤取行取列
19.alias 设置别名
20.history 历史命令

1.10 以下目录的作用(8 分)

/bin     --->>>普通用户存放的二进制相关命令路径  如 /bin/ls
/sbin    --->>>超级用户存放的二进制相关命令路径  如 /sbin/ls
/boot    --->>>系统启动相关程序存放的位置 如 kernel grub(引导启动菜单)
/dev     --->>>存放一些设备的目录   如 硬盘、光驱
/etc     --->>>系统的配置文件 大多数服务的相关配置文件都在/etc目录下 如 /etc/hostname 
/home    --->>>普通用户的家目录
/lib     --->>>存放的是一些库文件 不可删除
/lib64   --->>>存放的是64位的库文件 不可删除
/media   --->>>存放的是一些与媒体设备有关的文件
/mnt     --->>>临时挂载点
/opt     --->>>第三方软件安装的路径
/proc    --->>>虚拟的文件系统
/root    --->>>超级用户的家目录
/run     --->>>进程运行的pid,(放在一个文件中)   如 lock相关文件
/srv     --->>>存放一些服务启动之后需要提取的数据
/sys     --->>>虚拟文件系统,存放进程和内核信息的
/tmp     --->>>临时目录 相当于回收站
/usr     --->>>存放系统文件的目录 相当于windows下的c盘
/var     --->>>可变化数据的目录,常用于存放系统日志相关的文件

二、awk
2.1 显示 1.txt 文件第 3 到第 10 行的内容?(2 分)

awk 'NR==3,NR==10' 1.txt

2.2 取出 IP 地址(2 分)

ifconfig eth0 |awk 'NR==2 {print $2}'

2.3 写一个文件,文件内容如下

cat >>test.txt<<EOF
server {
lisTEN 80;
server_nAme docs.xuliangwei.com;
root /code/dOcs
index INDEX.html;
}
EOF

2.3.1 过滤 docs.xuliangwei.com 这段关键字(2 分)

grep 'nAme' test.txt |awk '{print $2}' 

2.3.2 同时过滤出 root 和 index 的行,不区分大小写(2 分)

grep -E 'root|index' test.txt

2.3.3 过滤 index,区分大小写(2 分)

grep -i 'index' test.txt

2.3.4 过滤出带"O"的行,不区分大小写(2 分)

grep 'O' test.txt 

2.3.5 过滤出不带";"的行(2 分)

grep -v ';' test.txt

3.4 将"web3_access.log"上传至你的 linux 服务器
2.4.1 统计出该文件 IP 地址(第一列)出现的次数,并按正序对其进行排序(2 分)

awk '{print $1}' access.log |sort -n |uniq -c |sort -n

2.4.2 统计该文件内 HTTP 状态返回码出现的次数(例如 200,404,403,在第九列),并按照倒序进行排
序(2 分)

awk '{print $9}' access.log |sort -n |uniq -c |sort -nr

2.4.3 过滤出所有状态返回码是 200 的行,并将这些返回码为 200 行的全部替换成 300(2 分)

grep -o '200' access.log |sed 's#200#300#g'

3.5 使用 hostnamectl 查看当前系统信息

[root@test-200 /home]# hostnamectl
Static hostname: test-200
Icon name: computer-vm
Chassis: vm
Machine ID: 955d9cf9777d49748690808a0b38096b
Boot ID: 9565d1bd772543beb49d178479e7c94f
Virtualization: vmware
Operating System: CentOS Linux 7 (Core)
PE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-957.el7.x86_64
Architecture: x86-64

2.5.1 取出 kernel 内核版本信息(2 分)

hostnamectl |awk 'NR==9 {print $3}'

2.5.2 取出系统名称(2 分)

hostnamectl |awk 'NR==1 {print $3}'

2.5.3 取出系统版本信息,只显示"linux 7"(2 分)

hostnamectl |awk 'NR==7 {print $4,$5}'

三、用户与权限
3.1 在 Linux 系统中,哪个文件保存着用户信息(2分) (B)
A:/etc/Profile B:/etc/passwd
C:/usr/bin/env D:/boot

3.2 一个用户想要修改 dir 目录下的 file 文件,他需要对 dir 目录以及 file 文件有什么权限(2 分)(A)
A:file 的写权限和 dir 目录(以及一直向上到/目录)的写执行权限
B:只需要 file 的写权限 C:只需 file 的读和写权限
D:file 的写权限和 dir 目录(以及一直向上到/目录)的写权限

3.3 默认情况下管理员创建了一个用户,就会在(B)目录下创建一个用户主目录(2 分)
A:/usr B:/home C:/root D:/etc

3.4 为脚本程序指定执行权限的命令参数是(2 分)(A)
A:chmod +x filename.sh B:chown +x filename.sh
C:chmod +w filename.sh D:chown +r filename.sh

3.5 如果执行命令 # chmod 746 file.txt , 那么该文件的权限是?(2 分) (A)
A:rwxr--rw- B:rw-r--r--
C:–xr—rwx D:jrwxr—r—
3.6 某文件的权限为:drw--r--r--,用数值形式表示该权限,则该八进制数为(644),该文件属性是(目录)(2 分)

3.7 某个服务器有 a.sh 脚本,用户权限为 644,需要执行什么命令,才可以执行./a.sh 命令(2 分)

chmod a+x a.sh

3.8 唯一标识每一个用户的是用户的(uid)和(用户名)(2 分)

3.9 将以下权限翻译成数字,将数字权限用字母表示(14 分)

rw-r-xr--   654 
rw-r--r--   644
rwx--x--x   711
rw-------   600 
rwxr--r--   744  
rw-rw-r--   664
751 771 632 644
551 622 746 755

3.10 设置/home/user1/test 文件权限为所有者可读可写可执行,所有组可读可写,其他所有账户可读,并将该文件的所有者和所有组都修改为 root(3 分)

  • chmod 764 /home/user1/test
    chown root.root /home/user1/test
    

3.11 当用户 zabbix 对/testdir 目录有写和执行权限时,该目录下的只读文件 file1 是否可修改和删除?(3 分)

  • 可以强制修改保存,也可以删除

3.12 把 jacky 的密码设置为 123(3 分)

echo "123" |passwd --stdin jacky

3.13 使用"ls -l /"以长格式查看根目录,写出引号内容所代表的详细含义(4 分)
“dr-xr-xr-x.” 5 “root root” 4096 May 16 01:36 boot

d:表示目录

r-x 表示属主的权限

r-x 表示属组的权限

r-x 表示其他人的权限

root 表示属主

root 表示属组

3.14 当用户 mysql 对/data/DB 目录无读权限,但是拥有写和执行权限,意味着能做哪些操作,无法做哪些操作?(3 分)

  • 可以创建修改文件,可以删除(前提是必须要知道具体的文件名)

四、进阶
4.1 如果某一天你误操作了"rm -rf *",会发生哪些情况(3 分)

  • 会把当前目录下的内容删掉

4.2 用命令行更改 config.txt 文件,把里面所有的"name"更改为"address" (3 分)

sed 's#name#address#g'

4.3 用 awk 获取文件中第三行的倒数第二列字段(2分)

awk 'NR==3 {print $ (NF-1)}'

4.4 删除 file.txt 文件中的空行(3 分)

grep -v '^$' file.txt

4.5 删除/tmp 目录下所有 a 开头的文件(3 分)

rm -rf /tmp/a*

五、翻译(每个 2 分)

5.1 [root@test-200 ~]# cd /rot
-bash: cd: /rot: No such file or directory
5.2 [root@test-200 ~]# mdkir a
-bash: mdkir: command not found
5.3 [root@test-200 ~]# mkdir a
mkdir: cannot create directory ‘a’: File exists
5.4 [root@test-200 ~]# rm a
rm: cannot remove ‘a’: Is a directory
5.5 [root@test-200 ~]# rm a.txt
rm: remove regular empty file ‘a.txt’?
5.6 [root@test-200 ~]# cp /tmp/a.txt /root/a.txt
cp: overwrite ‘/root/a.txt’?
5.7 [root@test-200 ~]# id www
id: www: no such user
5.8 [test@test-200 /]$ cd /root
bash: cd: /root: Permission denied
5.9 [root@test-200 /tmp]# cp -q a.txt c.txt
cp: invalid option -- 'q'
5.10 [root@test-200 /home]# useradd test
useradd: user 'test' already exists
原文地址:https://www.cnblogs.com/yinwu/p/11369419.html