Linux经典面试题
Q1:分析日志log,获取ip地址,并统计出现次数,按从大到小的顺序,进行排序
log文件内容
http://192.168.200.10/index1.html
http://192.168.200.10/index2.html
http://192.168.200.20/index1.html
http://192.168.200.30/index1.html
http://192.168.200.40/index1.html
http://192.168.200.30/order.html
http://192.168.200.10/order.html
shell指令
[root@rootylq q1]# cat t.log
http://192.168.200.10/index1.html
http://192.168.200.10/index2.html
http://192.168.200.20/index1.html
http://192.168.200.30/index1.html
http://192.168.200.40/index1.html
http://192.168.200.30/order.html
http://192.168.200.10/order.html
[root@rootylq q1]# cat t.log | cut -d '/' -f 3
192.168.200.10
192.168.200.10
192.168.200.20
192.168.200.30
192.168.200.40
192.168.200.30
192.168.200.10
[root@rootylq q1]# cat t.log | cut -d '/' -f 3| sort
192.168.200.10
192.168.200.10
192.168.200.10
192.168.200.20
192.168.200.30
192.168.200.30
192.168.200.40
[root@rootylq q1]# cat t.log | cut -d '/' -f 3| sort | uniq
192.168.200.10
192.168.200.20
192.168.200.30
192.168.200.40
[root@rootylq q1]# cat t.log | cut -d '/' -f 3| sort | uniq -c
3 192.168.200.10
1 192.168.200.20
2 192.168.200.30
1 192.168.200.40
[root@rootylq q1]# cat t.log | cut -d '/' -f 3| sort | uniq -c |sort
1 192.168.200.20
1 192.168.200.40
2 192.168.200.30
3 192.168.200.10
[root@rootylq q1]# cat t.log | cut -d '/' -f 3| sort | uniq -c |sort -nu
1 192.168.200.20
2 192.168.200.30
3 192.168.200.10
[root@rootylq q1]# cat t.log | cut -d '/' -f 3| sort | uniq -c |sort -r
3 192.168.200.10
2 192.168.200.30
1 192.168.200.40
1 192.168.200.20
[root@rootylq q1]# cat t.log | cut -d '/' -f 3| sort | uniq -c |sort -nr
3 192.168.200.10
2 192.168.200.30
1 192.168.200.40
1 192.168.200.20
Q2 :统计连接到服务器的各个ip情况,并按照连接数从大到小进行排序
[root@rootylq q1]# netstat -an | grep "ESTABLISHED" | awk '{print $5 " "}' | awk -F ":" '{print $1}'
100.100.30.26
36.159.138.13
[root@rootylq q1]# netstat -an | grep "ESTABLISHED" | awk '{print $5 " "}' | cut -d ':' -f 1
100.100.30.26
36.159.138.13
[root@rootylq q1]# netstat -an | grep "ESTABLISHED" | awk '{print $5 " "}' | cut -d ':' -f 1 |sort
100.100.30.26
36.159.138.13
[root@rootylq q1]# netstat -an | grep "ESTABLISHED" | awk '{print $5 " "}' | cut -d ':' -f 1 |sort |uniq -c
1 100.100.30.s
1 36.159.138.13
[root@rootylq q1]# netstat -an | grep "ESTABLISHED" | awk '{print $5 " "}' | cut -d ':' -f 1 |sort |uniq -c |sort -nrs
1 36.159.138.13
1 100.100.30.26
Q3:找回MySql的root密码
step1:进入/etc/my.conf,添加配置项
skip-grant-tables
step2: 重启mysql服务
[root@rootylq /]# service mysqld restart
step3:登录mysql,密码为空
[root@rootylq /]# mysql -u root -p
Enter password:
step4:选择进入mysql数据库
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| aliyun |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables
-> ;
step5修改mysql密码
update user set authentication_string=password("Rootroot") where user='root';
step6,刷新mysql服务,并修改之前启动项,重启mysql
mysql> exit
Bye
[root@rootylq /]# vim /etc/my.conf
[1]+ Stopped vim /etc/my.conf
[root@rootylq /]# vim /etc/my.cnf
[root@rootylq /]# service mysqld restart
Q4:统计IP访问情况,分析nginx访问日志(assess.log),找出访问页面数量在前2位的ip
[root@rootylq ylq]# cat access.log
192.168.130.21 aaa.html
192.168.130.20 aaa.html
192.168.130.20 aaa.html
192.168.130.20 aaa.html
192.168.130.23 aaa.html
192.168.130.20 aaa.html
192.168.130.25 aaa.html
192.168.130.20 aaa.html
192.168.130.20 aaa.html
192.168.130.25 aaa.html
192.168.130.20 aaa.html
[root@rootylq ylq]# cat access.log | awk -F " " '{print $1}'
192.168.130.21
192.168.130.20
192.168.130.20
192.168.130.20
192.168.130.23
192.168.130.20
192.168.130.25
192.168.130.20
192.168.130.20
192.168.130.25
192.168.130.20
[root@rootylq ylq]# cat access.log | awk -F " " '{print $1}' | sort
192.168.130.20
192.168.130.20
192.168.130.20
192.168.130.20
192.168.130.20
192.168.130.20
192.168.130.20
192.168.130.21
192.168.130.23
192.168.130.25
192.168.130.25
[root@rootylq ylq]# cat access.log | awk -F " " '{print $1}' | sort | uniq -c
7 192.168.130.20
1 192.168.130.21
1 192.168.130.23
2 192.168.130.25
[root@rootylq ylq]# cat access.log | awk -F " " '{print $1}' | sort | uniq -c
7 192.168.130.20
1 192.168.130.21
1 192.168.130.23
2 192.168.130.25
[root@rootylq ylq]# cat access.log | awk -F " " '{print $1}' | sort | uniq -c | sort
1 192.168.130.21
1 192.168.130.23
2 192.168.130.25
7 192.168.130.20
[root@rootylq ylq]# cat access.log | awk -F " " '{print $1}' | sort | uniq -c | sort -nr
7 192.168.130.20
2 192.168.130.25
1 192.168.130.23
1 192.168.130.21
[root@rootylq ylq]# cat access.log | awk -F " " '{print $1}' | sort | uniq -c | sort -nr | head -2\
> ;
7 192.168.130.20
2 192.168.130.25
[root@rootylq ylq]# cat access.log | awk -F " " '{print $1}' | sort | uniq -c | sort -nr | head -2;
7 192.168.130.20
2 192.168.130.25
Q5: 使用tcpdump监听本机,将来自ip 192.168.200.1, tcp端口为22的数据,保存输出到tcpdump.log,用来实现数据分析
tcpdump -i eth0 host 47.96.10.50 and port 22 >> /home/ylq/tcpdump.log
Q6:常用的Nginx模块,用来做什么
- rewrite: 实现重写功能
- access:来源控制
- ssl: 安全加密
- ngx_http_gzip_module:网络传输压缩
- ngx_http_proxy_module:代理
- ngx_http_upstream_module:定义后端服务器列表
- ngx_cache_purge:缓存清除功能
Q7:如果你是系统管理员,在进行Linux系统权限划分时,应该考虑哪些因素
- 首先阐述Linux权限的主要对象
- 文件权限
- r:文件内容查看权限
- w:文件内容编辑权限(但不能删除文件本身)
- x:文件执行权限
- 目录权限
- r:目录内文件列表查看权限
- w:目录内文件的增删,复制,剪切权限
- x:能否进入目录的权限
- 权限修改指令
- chmod [augo] [ ±= ] [ rwx ] filename
- chmod 777 filename
- -R 权限递归选项
- 文件权限
- 根据自己实际的经验来阐述需要考虑的因素
- 注意权限分离,比如,工作中,Linux系统权限和数据库权不能在同一部门
- 权限最小原则:在满足使用的情况下最少有限
- 减少使用root用户,尽量使用普通用户+sudo提权进行日常操作
- 重要的系统文件,例如 passwd,shadow,fstab,sudoers 日常建议使用chattr锁定,需要操作时再打开
- 使用SUID,SGID,Sticky设置特殊权限
- 可以利用工具,比如chkrootkit/rootkit hunter检测rootkit脚本
- 利用工具Tripwire检测文件系统完整性
Q8:文件权限思考题
- 用户Tom对目录有 执行 和 读 写权限,目录中的文件是只读文件,tom 能 读文件,不能修改文件,能删除文件
- 用户Tom对目录有 读 写权限,目录中的文件时是只读文件,tom 不能 读文件,不能修改文件,不能删除文件
- 用户Tom对目录有 执行权限,目录中的文件时是只读文件,tom 能 读文件,不能修改文件,不能删除文件
- 用户Tom对目录有 执行和 写权限,目录中的文件时是只读文件,tom 能 读文件,不能修改文件,能删除文件
Q9:说明CentOS7启动流程,并说明和CentOS6相同和不同的地方
CentOS7启动流程
- 硬件引导启动
- power on
- BIOS
- MBR
- GRUB2启动引导阶段
- Boot.img
- core.img
- *.mod
- grub.cfg
- 内核引导阶段
- systemd初始化阶段
Q10:列举Linux高级命令,至少6个
- netstat 网络装填监控
- top 系统运行状态
- lsblk 查看硬盘分区
- find
- ps -aux 查看运行进程
- chkconfig 查看服务启动状态
- systemctl 管理系统服务器
Q11: Linux查看内存、io读写、磁盘存储、端口占用、进程查看命令是什么
查看内存
[root@rootylq ylq]# top
io读写
[root@rootylq ylq]# iotop
磁盘存储
[root@rootylq ylq]# df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 909M 0 909M 0% /dev
tmpfs 919M 16K 919M 1% /dev/shm
tmpfs 919M 576K 919M 1% /run
tmpfs 919M 0 919M 0% /sys/fs/cgroup
/dev/vda1 40G 6.8G 31G 19% /
tmpfs 184M 0 184M 0% /run/user/0
tmpfs 184M 0 184M 0% /run/user/1003
[root@rootylq ylq]#
端口占用
[root@rootylq ylq]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 530/rpcbind
tcp 0 0 0.0.0.0:18166 0.0.0.0:* LISTEN 1488/python3
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1272/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1262/master
tcp6 0 0 :::111 :::* LISTEN 530/rpcbind
tcp6 0 0 ::1:25 :::* LISTEN 1262/master
tcp6 0 0 :::33060 :::* LISTEN 26576/mysqld
tcp6 0 0 :::3306 :::* LISTEN 26576/mysqld
udp 0 0 127.0.0.1:323 0.0.0.0:* 537/chronyd
udp 0 0 0.0.0.0:691 0.0.0.0:* 530/rpcbind
udp 0 0 0.0.0.0:68 0.0.0.0:* 913/dhclient
udp 0 0 0.0.0.0:111 0.0.0.0:* 530/rpcbind
udp6 0 0 ::1:323 :::* 537/chronyd
udp6 0 0 :::691 :::* 530/rpcbind
udp6 0 0 :::111 :::* 530/rpcbind
[root@rootylq ylq]#
进程查看
[root@rootylq ylq]# ps -auf
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
ylq 31059 0.0 0.1 116076 2580 pts/2 Ss+ 22:22 0:00 -bash
root 30950 0.0 0.1 116324 2904 pts/1 Ss 22:17 0:00 -bash
root 31988 0.0 0.0 155448 1856 pts/1 R+ 23:22 0:00 \_ ps -auf
root 30861 0.0 0.1 116204 2692 pts/0 Ss+ 22:14 0:00 -bash
root 1281 0.0 0.0 110208 868 ttyS0 Ss+ Jan19 0:00 /sbin/agetty --keep-baud 115200,38400,9600 ttyS0 vt220
root 1280 0.0 0.0 110208 860 tty1 Ss+ Jan19 0:00 /sbin/agetty --noclear tty1 linux
[root@rootylq ylq]#
Q12:使用Linux命令计算t2.txt第二列的和并输出
[root@rootylq ylq]# cat test.txt
张山 40
李四 30
王五 90
[root@rootylq ylq]# cat test.txt | awk -F " " '{sum+=$2} END {print sum}'
160
Q13:shell脚本中如何检查一个文件是否存在并给出提示
if [-f 文件名] then echo "存在" else echo "不存在" fi
Q14:用shell写一个脚本,对文本t3.txt中无序的一列数字进行排序,并将总和输出
[root@rootylq ylq]# sort -nr test.txt | awk '{sum+=$0; print $0} END {print "和=" sum}'
9
8
7
5
5
4
3
1
0
和=42
Q15:请用指令写出查找当前文件夹(/home)下所有的文本文件内容中包含有字符“cat”的文件名称
[root@rootylq ylq]# grep -r "cat" /home | cut -d ":" -f 1
Q16:请写出统计/home目录下所有文件个数和所有文件总行数的指令
[root@rootylq home]# find /home -name "*.*" | wc -l
[root@rootylq home]# find /home -name "*.*" |xargs wc -l
Q17:列出你所知道的web服务器负载架构
- Nginx
- Haproxy
- Keepalived
- LVS
Q18: 每天晚上10点30分,打包站点目录/var/spool/mail备份到/home目录下(每次备份按时间生成不同的备份包)
[root@rootylq ~]# cat mail.sh
#!/bin/bash
cd /var/spool/ && /bin/tar zcf /home/mail-`date +%Y-%m-%d_%H%M%S`.tar.gz mail/
Q19:如何优化Linux系统,说出你的方法
- 网络
- 磁盘IO
- 文件连接
- 安全性
- 防火墙
- 内存
- 对Linux 结构的优化和原则分析
- 对Linux系统本身的优化-规则
- 不用root,使用sudo提升权限
- 定时自动更新服务器时间, npdate npt1.aliyun.com 定时更新
- 配置yum源,指向国内镜像
- 配置合理的防火墙策略,打开必要的端口,关闭不必要的端口
- 调整最大文件数(调整文件的描述数量) vim /etc/profile ulimit -SHn 65535
- 配置合理的监控策略
- 配置合理的系统重要文件的备份策略
- 对安装的软件进行有些话,ngnix,apache
- 对内核参数进行优化 /etc/stsctl.conf
- 锁定重要的系统文件
- 禁用不必要的服务 setup ntsysv