Linux 企业面试题之一

一、如何过滤出当前目录下oldboy中的所有一级目录(提示:不包含oldboy目录下面目录的子目录及隐藏目录,即只能是一级目录)

这里写图片描述

1、ls -p:只给目录加斜线

[root@oldboy data]# ls
1.txt 2.txt 3.txt 4444 test test1 test2

[root@oldboy data]# ls -p --color | grep '/$'
4444
test1
test2

2、ls -F:给不同的目录加不同的符号

[root@oldboy data]# ls -F --color | grep '/$'
4444
test1
test2

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 28	
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

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 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	

[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 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

[root\@oldboy data]\# ls -F | awk '/\/$/'	
4444/	
test1/	
test2/

二、假设所在当前目录为/oldboy,若切换到/tmp目录下后想要回到原来的/oldboy目录,但不能使用cd /oldboy,如何操作?

[root@oldboy oldboy]# pwd	
oldboy

[root@oldboy oldboy]# cd /tmp	
[root@oldboy tmp]# cd -	
/oldboy

[root@oldboy oldboy]# pwd
/oldboy

cd - 的原理

#相当于有个跟屁虫一直记录你上一个目录的位置信息,即环境变量,当输入cd -就会直接切入到\$OLDPWD所指定的目录下了。

[root@oldboy oldboy]# env | grep -i oldpwd	
OLDPWD=/tmp

[root@oldboy oldboy]# cd /tmp	
[root@oldboy tmp]\# env | grep -i oldpwd	
OLDPWD=/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]# date	
Fri 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/logs	
cd /app/logs	
for n in `seq 14` ;	
do	
date -s "2017/12/$n" ;	
touch access_www_$(date +%F).log ;	
done	
date -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/messages	
23	
24	
25	
26	
27	
28	
29	
30	
31	
32

法二:

[root@oldboy logs]# tailf /var/log/messages	
91	
92	
93	
94	
95	
96	
97	
98	
99	
100

法三:-F有个重试的功能,就是文件不存在了,会继续重试

[root@oldboy logs]# tail -F /var/log/messages		
98	
99	
100	
1	
2	
3
4	
5	
6	
7	
8

六、打印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.conf	
1 stu01	
2 stu02	
3 stu03	
4 stu04	
5 stu05	
6 stu06	
7 stu07	
8 stu08	
9 stu09	
10 stu10

法二:cat -n nginx.conf

[root@oldboy oldboy]# cat -n nginx.conf	
1 stu01	
2 stu02	
3 stu03	
4 stu04	
5 stu05	
6 stu06	
7 stu07	
8 stu08	
9 stu09	
10 stu10

法三:grep -n “.” nginx.conf 不匹配空白行

grep -n ".*" nginx.conf 可以把空白行也给匹配出来

[root@oldboy oldboy]# grep -n . nginx.conf	
1:stu01	
2:stu02	
3:stu03	
4:stu04	
5:stu05	
6:stu06	
7:stu07	
8:stu08	
9:stu09	
10:stu10

法四:vim末行模式:set nu

法五:awk ‘{print NR,$0}’ nginx.conf

[root@oldboy oldboy]# awk '{print NR,$0}' nginx.conf	
1 stu01	
2 stu02	
3 stu03	
4 stu04	
5 stu05	
6 stu06	
7 stu07	
8 stu08	
9 stu09	
10 stu10

法六:sed = nginx.conf | sed 'N;s/\n/ /'

[root@oldboy oldboy]# sed = nginx.conf\| sed 'N;s/\n/ /'	
1 stu01	
2 stu02	
3 stu03	
4 stu04	
5 stu05	
6 stu06	
7 stu07	
8 stu08	
9 stu09	
10 stu10

法七:less -N nginx.conf

[root@oldboy oldboy]# less -N nginx.conf

七、如何解决中文乱码问题?

法一:临时生效

[root@oldboy oldboy]# echo $LANG

en_US.UTF-8

[root@oldboy oldboy]# export LANG="zh_CN.UTF-8" #此法执行后,仅在当前窗口生效

[root@oldboy oldboy]# echo $LANG

zh_CN.UTF-8

#提示:要注意字符集的大小写

法二:永久生效

[root@oldboy oldboy]# echo 'LANG="zh_CN.UTF-8"' >/etc/sysconfig/i18n

[root@oldboy oldboy]# source /etc/sysconfig/i18n #生效配置文件

八、如何优化linux系统?

  1. 不用root,添加普通用户,通过sudo 授权管理。
  2. 更改默认的远程连接SSH 服务端口及禁止root 用户远程连接。
  3. 定时自动更新服务器时间。
  4. 配置yum 更新源,从国内更新源下载安装rpm 包。
  5. 关闭selinux 及iptables (iptables 工作场景如果有wan ip一般要打开,高并发除外)。
  6. 调整文件描述符的数量,进程及文件的打开都会消耗文件描述符。
  7. 定时自动清理/var/spool/clientmquene/ 目录垃圾文件,防止inodes节点被占满(c6.4 默认没有sendmail,因此,可以不配)。
  8. 精简开机自启动服务只开启下面几个服务(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 off
chkconfig abrtd off	
chkconfig acpid off	
chkconfig atd off	
chkconfig auditd off	
chkconfig blk-availability off	
chkconfig cpuspeed off	
chkconfig haldaemon off	
chkconfig ip6tables off	
chkconfig iptables off	
chkconfig irqbalance off	
chkconfig kdump off	
chkconfig lvm2-monitor off	
chkconfig mdmonitor off	
chkconfig messagebus off	
chkconfig netfs off	
chkconfig postfix off	
chkconfig rdma off	
chkconfig 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:off	
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off	
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off	
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off	
sysstat 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
  1. linux 内核参数优化/etc/sysct1.conf,执行sysctl-p 生效。

  2. 更改字符集,支持中文,但建议还是用英文字符集,防止乱码问题,不要使用中文。

  3. 锁定关键系统文件。
    chattr +i /etc/passwd /etc/shadow /etc/group/etc/gshadow /etc/inittab
    处理以上内容后把chattr,lsattr改名为oldboy,这样就安全多了。

  4. 清空/etc/issue ,去除系统及内核版本登录前的屏幕提示

  5. 清除无用的默认系统账户或组(非必须)

更多优化细节详见博客: http://oldboy.blog.51cto.com/2561410/988726

九、如何查看/etc/services文件有多少行

[root@oldboy oldboy]# wc -l /etc/services	
10774 /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/sshd	
root 6120 1560 0 Dec14 ? 00:00:00 sshd: root@pts/1
root 6378 1560 0 00:34 ? 00:00:00 sshd: root@pts/0	
[root@oldboy oldboy]# ps -ef|grep "sshd"|grep -v "grep"|wc -l	
3

#生产场景中会统计这个数字,若是大于等于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,5	
chkconfig --level 345 on	
chkconfig --level 5 off

原理:

[root@oldboy ~]# chkconfig --list sshd	
sshd 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭	
[root@oldboy ~]# ll /etc/rc.d/rc3.d/|grep sshd	
lrwxrwxrwx. 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 sshd	
lrwxrwxrwx. 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 rsq	
echo "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 oldboyd	
lrwxrwxrwx. 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 oldboyd	
lrwxrwxrwx. 1 root root 17 12月 15 06:32 K27oldboyd -> ../init.d/oldboyd	
[root@oldboy ~]# ll /etc/rc.d/rc5.d/|grep oldboyd	
lrwxrwxrwx. 1 root root 17 12月 15 06:32 K27oldboyd -> ../init.d/oldboyd

十一、描述Linux系统的启动过程

  1. 开机BIOS自检
    检查硬件是否正常
  2. MBR引导
    硬盘0柱面0磁道1扇区的前446byte(一扇区共512byte,没有读取完毕)后边还有66字节,其中64字节作为分区表使用,即4个分区表(每个分区表16字节),还有2字节(55AA标识)作为分区的结束
  3. grub引导菜单
    cat /etc/grub.conf
  4. 加载内核kernel
  5. 加载init进程,进程号为1,为第一个启动的进程。会读取/etc/inittab文件–>执行/etc/rc.d/rc.sysinit脚本(一些基本的设置)–>执行/etc/rc.d/rc脚本(/etc/rc*.d/ ,会选择对应的运行级别而后启动)–>启动mingetty进程
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RSQ博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值