Linux 企业面试题之一
来源:互联网 发布:中融信托工作知乎 编辑:程序博客网 时间:2024/06/08 01:15
一、如何过滤出当前目录下oldboy中的所有一级目录(提示:不包含oldboy目录下面目录的子目录及隐藏目录,即只能是一级目录)
1、ls -p:只给目录加斜线
[root@oldboy data]# ls1.txt 2.txt 3.txt 4444 test test1 test2[root@oldboy data]# ls -p --color | grep '/$'4444test1test2
2、ls -F:给不同的目录加不同的符号
[root@oldboy data]# ls -F --color | grep '/$'4444test1test2
3、用find来查找
[root@oldboy data]# find ./ -type d././4444./test1./test1/test3./test1/test3/test4./test2
发现其又往下级目录查找,故要加个-maxdepth参数,注意此参数要加在最前面。
[root@oldboy data]# find ./ -type d././4444./test1./test1/test3./test1/test3/test4./test2[root@oldboy data]# find ./ -maxdepth 1 -type d././4444./test1./test2
PS:./和./*的区别:
./:当前目录下的内容包括./本身
./*:当前目录下的所有内容不包括./本身
[root@oldboy data]# find ./ -maxdepth 1 -type d././4444./test1./test2[root@oldboy data]# find ./* -maxdepth 1 -type d./4444./test1./test1/test3./test2但是要是用./*会把深度的目录也匹配出来,故要是相符合题意,则还是要用./[root@oldboy data]# find ./ -maxdepth 1 -type d ! -name '.'./4444./test1./test2
4、awk来实现
[root@oldboy data]# ls -l | awk '{if($2>1) print $0}'total 28drwxr-xr-x. 2 root root 4096 Nov 19 07:14 4444drwxr-xr-x. 3 root root 4096 Nov 19 07:18 test1drwxr-xr-x. 2 root root 4096 Nov 19 07:13 test2
5、其它方法,了解即可
[root@oldboy data]# ls -dl */drwxr-xr-x. 2 root root 4096 Nov 19 07:14 4444/drwxr-xr-x. 3 root root 4096 Nov 19 07:18 test1/drwxr-xr-x. 2 root root 4096 Nov 19 07:13 test2/
sed
[root@oldboy data]# ls -l | sed -n '/^d/p'drwxr-xr-x. 2 root root 4096 Nov 19 07:14 4444drwxr-xr-x. 3 root root 4096 Nov 19 07:18 test1drwxr-xr-x. 2 root root 4096 Nov 19 07:13 test2[root@oldboy data]# ls -F | sed -n '/\/$/p'4444/test1/test2/
awk
[root@oldboy data]# ls -l | awk '/^d/'drwxr-xr-x. 2 root root 4096 Nov 19 07:14 4444drwxr-xr-x. 3 root root 4096 Nov 19 07:18 test1drwxr-xr-x. 2 root root 4096 Nov 19 07:13 test2[root\@oldboy data]\# ls -F | awk '/\/$/'4444/test1/test2/
二、假设所在当前目录为/oldboy,若切换到/tmp目录下后想要回到原来的/oldboy目录,但不能使用cd /oldboy,如何操作?
[root@oldboy oldboy]# pwdoldboy[root@oldboy oldboy]# cd /tmp[root@oldboy tmp]# cd -/oldboy[root@oldboy oldboy]# pwd/oldboy
cd - 的原理
#相当于有个跟屁虫一直记录你上一个目录的位置信息,即环境变量,当输入cd -就会直接切入到\$OLDPWD所指定的目录下了。[root@oldboy oldboy]# env | grep -i oldpwdOLDPWD=/tmp[root@oldboy oldboy]# cd /tmp[root@oldboy tmp]\# env | grep -i oldpwdOLDPWD=/oldboy
cd的一些用法
[root@oldboy tmp]# cd .[root@oldboy tmp]# cd ..[root@oldboy /]# cd ~[root@oldboy ~]# cd -/[root@oldboy /]# cd[root@oldboy ~]# cd /etc/sysconfig/[root@oldboy sysconfig]# cd ../../[root@oldboy /]# pwd/
三、一个目录中有很多文件(ls查看时好多屏),想要最快速查看到最近更新的文件,如何做?
#用ls -lrt命令,-r当排序时,翻转排序,-t按修改时间排序,故有必要的话需要修改时间
[root@oldboy ~]# cd /etc[root@oldboy etc]# touch test.txt[root@oldboy etc]# date -s '20171201 21:42'Fri Dec 1 21:42:00 EST 2017[root@oldboy etc]# dateFri Dec 1 21:42:02 EST 2017[root@oldboy etc]# echo 123 > test.txt[root@oldboy etc]# ls -lrt
四、已知apache 服务的访问日志按天记录在服务器本地目录/app/logs 下,由于磁盘空间紧张,现在要求只能保留最近7天访问日志!请问如何解决? 请给出解决办法或配置或处理命令。
解答: apache 日志测试数据: 企业面试题
(提示:可以从apache 服务配置上着手,也可以从生成出来的日志上着手。)
模拟数据:
mkdir -p /app/logscd /app/logsfor n in `seq 14` ;dodate -s "2017/12/$n" ;touch access_www_$(date +%F).log ;donedate -s "2017/12/15"
实现删除
法一:[root@oldboy logs]# find /app/logs/ -type f -mtime +7 | xargs rm -f法二:[root@oldboy logs]# find /app/logs/ -type f mtime +7 -exec rm -f {} \;法三:[root@oldboy logs]# rm -f `find /app/logs/ -type f -mtime +7`
工作中可以把它做成定时任务,每天或每周定时清理
五、调试系统服务器时,希望能实时查看系统日志/var/log/messages的更新,如何做?
模拟环境:打开另一个终端,不断往/var/log/messages文件中追加信息,本地实时查看
[root@oldboy ~]# for n in `seq 100` ; do echo $n >>/var/log/messages ; sleep 1 ; done
法一:
[root@oldboy logs]# tail -f /var/log/messages23242526272829303132
法二:
[root@oldboy logs]# tailf /var/log/messages919293949596979899100
法三:-F有个重试的功能,就是文件不存在了,会继续重试
[root@oldboy logs]# tail -F /var/log/messages989910012345678
六、打印nginx.conf 配置文件中的行号
模拟环境,由于原nginx.conf配置文件中内容过多,这里就自己创建一个nginx.conf文件
[root@oldboy oldboy]# echo stu{01..10} | xargs -n 1 > nginx.xonf
法一:nl nginx.conf(nl 显示文件行号,空行不计数)
[root@oldboy oldboy]# nl nginx.conf1 stu012 stu023 stu034 stu045 stu056 stu067 stu078 stu089 stu0910 stu10
法二:cat -n nginx.conf
[root@oldboy oldboy]# cat -n nginx.conf1 stu012 stu023 stu034 stu045 stu056 stu067 stu078 stu089 stu0910 stu10
法三:grep -n “.” nginx.conf 不匹配空白行
grep -n ".*" nginx.conf 可以把空白行也给匹配出来[root@oldboy oldboy]# grep -n . nginx.conf1:stu012:stu023:stu034:stu045:stu056:stu067:stu078:stu089:stu0910:stu10
法四:vim末行模式:set nu
法五:awk ‘{print NR,$0}’ nginx.conf
[root@oldboy oldboy]# awk '{print NR,$0}' nginx.conf1 stu012 stu023 stu034 stu045 stu056 stu067 stu078 stu089 stu0910 stu10
法六:sed = nginx.conf | sed ‘N;s/\n/ /’
[root@oldboy oldboy]# sed = nginx.conf| sed 'N;s/\n/ /'1 stu012 stu023 stu034 stu045 stu056 stu067 stu078 stu089 stu0910 stu10
法七:less -N nginx.conf
[root@oldboy oldboy]# less -N nginx.conf
七、如何解决中文乱码问题?
法一:临时生效
[root@oldboy oldboy]# echo $LANGen_US.UTF-8[root@oldboy oldboy]# export LANG="zh_CN.UTF-8" #此法执行后,仅在当前窗口生效[root@oldboy oldboy]# echo $LANGzh_CN.UTF-8#提示:要注意字符集的大小写
法二:永久生效
[root@oldboy oldboy]# echo 'LANG="zh_CN.UTF-8"' >/etc/sysconfig/i18n[root@oldboy oldboy]# source /etc/sysconfig/i18n #生效配置文件
八、如何优化linux系统?
01) 不用root,添加普通用户,通过sudo 授权管理。
02) 更改默认的远程连接SSH 服务端口及禁止root 用户远程连接。
03) 定时自动更新服务器时间。
04) 配置yum 更新源,从国内更新源下载安装rpm 包。
05) 关闭selinux 及iptables (iptables 工作场景如果有wan ip一般要打开,高并发除外)。
06) 调整文件描述符的数量,进程及文件的打开都会消耗文件描述符。
07) 定时自动清理/var/spool/clientmquene/ 目录垃圾文件,防止inodes节点被占满(c6.4 默认没有sendmail,因此,可以不配)。
08) 精简开机自启动服务只开启下面几个服务(crond,sshd,network,rsyslog,sysstat
(c5.8 syslog))。着重说一下sysstat,它是一个软件包,下面有几个比较重要的命令,如iostat(查看CPU使用率及硬盘吞吐效率的数据)、mpstat(提供与单个或多个处理器相关的数据)、sar(负责收集、报告并存储系统活跃的信息)最好只开启这几个服务,后续需要什么服务再开启什么服务ntsysv可以选择要开启的服务
新装的系统默认在开机后会有好多服务开启,如何把这些服务给关闭掉,要是一个一个关闭效率太低了,这时可以先把我们要开启的服务过滤出来,而后关闭其它服务
[root@oldboy init.d]# chkconfig --list | grep "3:on" | egrep -v "crond|sshd|sysstat|rsyslog|network" | awk '{print "chkconfig",$1,"off"}'chkconfig abrt-ccpp offchkconfig abrtd offchkconfig acpid offchkconfig atd offchkconfig auditd offchkconfig blk-availability offchkconfig cpuspeed offchkconfig haldaemon offchkconfig ip6tables offchkconfig iptables offchkconfig irqbalance offchkconfig kdump offchkconfig lvm2-monitor offchkconfig mdmonitor offchkconfig messagebus offchkconfig netfs offchkconfig postfix offchkconfig rdma offchkconfig udev-post off
#上面打印出来我们要执行的所有命令后,给bash执行
[root@oldboy init.d]# chkconfig --list | grep "3:on" | egrep -v "crond|sshd|sysstat|rsyslog|network" | awk '{print "chkconfig",$1,"off"}' | bash[root@oldboy init.d]# chkconfig --list | grep "3:on"crond 0:off 1:off 2:on 3:on 4:on 5:on 6:offnetwork 0:off 1:off 2:on 3:on 4:on 5:on 6:offrsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:offsshd 0:off 1:off 2:on 3:on 4:on 5:on 6:offsysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
# 也可以先把所有的服务给关闭,而后再开启我们所需要的服务即可
[root@oldboy ~]# chkconfig --list | grep "3:on" | awk '{print "chkconfig",$1,"off"}' | bash[root@oldboy ~]# chkconfig --list | grep "3:off" | egrep "crond|sshd|sysstat|rsyslog|network" |awk '{print "chkconfig",$1,"on"}'| bash
09) linux 内核参数优化/etc/sysct1.conf,执行sysctl-p 生效。
10) 更改字符集,支持中文,但建议还是用英文字符集,防止乱码问题,不要使用中文。
11) 锁定关键系统文件。
chattr +i /etc/passwd /etc/shadow /etc/group/etc/gshadow /etc/inittab
处理以上内容后把chattr,lsattr改名为oldboy,这样就安全多了。
12) 清空/etc/issue ,去除系统及内核版本登录前的屏幕提示
13) 清除无用的默认系统账户或组(非必须)
更多优化细节详见博客: http://oldboy.blog.51cto.com/2561410/988726
九、如何查看/etc/services文件有多少行
[root@oldboy oldboy]# wc -l /etc/services10774 /etc/services#提示:wc -l总行数 -L打印文件中最长行的字符数
工作中wc的使用
[root@oldboy oldboy]# ps -ef|grep "sshd"|grep -v "grep"root 1560 1 0 Dec13 ? 00:00:00 /usr/sbin/sshdroot 6120 1560 0 Dec14 ? 00:00:00 sshd: root@pts/1root 6378 1560 0 00:34 ? 00:00:00 sshd: root@pts/0[root@oldboy oldboy]# ps -ef|grep "sshd"|grep -v "grep"|wc -l3
#生产场景中会统计这个数字,若是大于等于1就证明这个服务已经启动
十、装完系统后,希望让网络文件共享服务NFS,仅在3 级别上开机自启动,该如何做?
解答:什么是开机自启动,杀毒软件我们都知道吧,例如:360等每次开机都会自动起来对吧,我们就需要360这样,在linux下软件服务随系统启动而启动的配置。
法一:把具体服务追加在配置文件/etc/rc.local中
[root@oldboy oldboy]# echo " /etc/init.d/sshd start " >> /etc/rc.local
法二:chkconfig
chkconfig --list :查看所有服务所有级别的启动状态chkconfig --list sshd:查看指定服务的启动状态chkconfig --level n [on|off]:指定级别开启或关闭,若不指定级别,默认是2,3,4,5chkconfig --level 345 onchkconfig --level 5 off原理:[root@oldboy ~]# chkconfig --list sshdsshd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭[root@oldboy ~]# ll /etc/rc.d/rc3.d/|grep sshdlrwxrwxrwx. 1 root root 14 11月 16 11:30 S55sshd -> ../init.d/sshd[root@oldboy ~]# chkconfig --level 3 sshd off[root@oldboy ~]# ll /etc/rc.d/rc3.d/|grep sshdlrwxrwxrwx. 1 root root 14 12月 15 05:56 K25sshd -> ../init.d/sshd
#注意,当chkconfig指定的运行界别开机启动,那么在相对应级别的脚本中会找到以S开头的服务文件,当关闭其开机启动,那么在相对应的脚本中就会发现以K开头的服务文件,即手动创建删除也可以,需要指定链接文件
#默认chkconfig启动是2345,启动S55,停止为K25这些都是在/etc/init.d/sshd配置文件中写的
[root@oldboy ~]# head /etc/init.d/sshd#!/bin/bash## sshd Start up the OpenSSH server daemon## chkconfig: 2345 55 25# description:
*****手动创建一个开机自启动的oldboyd服务
#首先要先在/etc/init.d/目录下创建对应的服务文件,oldboyd
[root@oldboy ~]# vim /etc/init.d/oldboyd[root@oldboy ~]# cat /etc/init.d/oldboyd#chkconfig: 35 57 27#description: oldboyd by rsqecho "This is my service!"
#添加oldboyd服务
[root@oldboy ~]# chkconfig --add oldboyd[root@oldboy ~]# chkconfig --list oldboyd #可以看到3,5级别都是处于启动状态oldboyd 0:关闭 1:关闭 2:关闭 3:启用 4:关闭 5:启用 6:关闭[root@oldboy ~]# ll /etc/rc.d/rc3.d/|grep oldboydlrwxrwxrwx. 1 root root 17 12月 15 06:31 S57oldboyd -> ../init.d/oldboyd[root@oldboy ~]# chkconfig oldboyd off[root@oldboy ~]# ll /etc/rc.d/rc3.d/|grep oldboydlrwxrwxrwx. 1 root root 17 12月 15 06:32 K27oldboyd -> ../init.d/oldboyd[root@oldboy ~]# ll /etc/rc.d/rc5.d/|grep oldboydlrwxrwxrwx. 1 root root 17 12月 15 06:32 K27oldboyd -> ../init.d/oldboyd
十一、描述Linux系统的启动过程?
开机BIOS自检
检查硬件是否正常MBR引导
硬盘0柱面0磁道1扇区的前446byte(一扇区共512byte,没有读取完毕)
后边还有66字节,其中64字节作为分区表使用,即4个分区表,还有2字节作为分区的结束grub引导菜单
cat /etc/grub.conf加载内核kernel
加载init进程,进程号为1,为第一个启动的进程。会读取/etc/inittab文件–>执行/etc/rc.d/rc.sysinit脚本(一些基本的设置)–>执行/etc/rc.d/rc脚本(/etc/rc*.d/ ,会选择对应的运行级别而后启动)–>启动mingetty进程
- Linux 企业面试题之一
- linux运维必会MySQL企业面试题
- android面试题,各大企业常见的android面试题之一
- ios面试题,各大企业常见的ios面试题之一
- php面试题,各大企业常见的php面试题之一
- 企业面试机试题
- JSP企业面试题
- Js企业面试题
- 总结企业面试题
- 微软面试题之一
- Java面试题之一
- 面试题之一
- java面试题之一
- java面试题之一
- 测试面试题之一
- 面试题系列之一
- 面试题集之一
- 面试题攻略之一
- Going Home
- Android布局优化之TextView、ImageView合二为一
- 刷题如需帮助,请发送源代码,题号和问题的简单描述,以便我更快更好地提供服务,非常感谢您的配合。
- OTSU 阈值计算在OpenCV中的实现
- 2017.12.23
- Linux 企业面试题之一
- 前端中常见的数组操作 indexOf push splice slice concat map、filter 等
- git-简易指南与总结汇总
- 统计学习方法概论
- 数据结构实验之排序七:选课名单
- 集成自己的番号+磁链库
- Neo4j 第一篇:在Windows环境中安装Neo4j
- php实现ckeditor编辑器添加水印及使用误区
- Bryce1010的linux课程设计