四、

一、过滤ip地址

[root@pyrene ~]# ifconfig eth0 |awk 'NR==2'

          inet addr:192.168.58.129  Bcast:192.168.58.255  Mask:255.255.255.0

 [root@pyrene ~]# ifconfig eth0 |awk 'NR==2 {print $1}'

Bcast:192.168.58.255

[root@pyrene ~]# ifconfig eth0 |awk 'NR==2 {print $1}'|awk -F ':' "{print $2}"

Bcast:192.168.58.255

[root@pyrene ~]# ifconfig eth0 |awk 'NR==2 {print $1}'|awk -F ":" '{print $2}'

192.168.58.255

上面步骤:

1、获取第二行的内容

2、默认以空格作为分割 获取第部分

3、以冒号作为分割,获取第二部分内容也就是ip地址

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

思路思想方法:

         1、ll查看结果中以d开头的就是目录

         2、ll输出结果中第二列输出结果大于1的时候就是目录(当文件中没有硬链接的时候是对的)

         3、通过find直接查找指定类型的文件(-d就是目录)

         4、ls –F以/结尾的都是目录

         5、ls –p 查看文件结尾/的目录

结果:

方法一

[root@pyrene oldboy]# ll |grep "^d"        打印以d开头的

drwxr-xr-x 3 root root 4096 12月  5 22:23 ext

drwxr-xr-x 2 root root 4096 12月  5 22:24 pyrene

drwxr-xr-x 2 root root 4096 12月  5 22:23 test

drwxr-xr-x 2 root root 4096 12月  5 22:23 xiaodong

  方法二

                   [root@pyrene oldboy]# ls -F|grep "/$"

ext/

pyrene/

test/

xiaodong/

  方法三

                   [root@pyrene oldboy]# find ./ -maxdepth 1 -type d    查看深度

./

./ext

./pyrene

./test

./xiaodong

                   [root@pyrene oldboy]# find ./ -maxdepth 1 -type d ! -name "."

./ext

./pyrene

./test

./xiaodong

  方法四

                   [root@pyrene oldboy]# ls -l |awk '{if($2>1) print $0}'

总用量 16

drwxr-xr-x 3 root root 4096 12月  5 22:23 ext

drwxr-xr-x 2 root root 4096 12月  5 22:24 pyrene

drwxr-xr-x 2 root root 4096 12月  5 22:23 test

drwxr-xr-x 2 root root 4096 12月  5 22:23 xiaodong

      

三、假如当前目录是

[root@pyrene oldboy]# pwd

/root/oldboy

现在因为需要进入到了/tmp目录下操作,执行的命令如下

[root@pyrene oldboy]# cd /tmp/

[root@pyrene tmp]# pwd

/tmp

操作完成后,希望快速回到上一次进入的目录,及/oldboy目录,该如何做到呢?(提示:不能用cd /oldboy命令)

解答:

         [root@pyrene tmp]# cd -

/root/oldboy

四、一个目录中有很多文件(ls查看时好多屏)想最快速度查看到最近更新的文件如何查看?

ls –lrt /etc/

这条命令依赖于当前时间

这里  -r是反转  -t是按照修改时间进行排序

 

五、已知apache服务的访问日志按天记录在服务器本地目录/app/logs下面,由于磁盘空间紧张,现在要求只能保存最近七天访问日志,请问如何解决?请给出解决办法或者配置或者处理命令

首先创建出来这些文件 (模拟)

mkdir /app/logs –p

cd /app/logs

for n in `seq 13` ;

do

date –s “2017/12/$n” ;

touch access_www_$(date +%F).log ;

done

date –s ‘2017/12/11’

下面是三种方法

[root@pyrene app]# find /app/logs/ -type f -mtime -7

/app/logs/access_www_2017-12-10.log

/app/logs/access_www_2017-12-08.log

/app/logs/access_www_2017-12-05.log

/app/logs/access_www_2017-12-06.log

/app/logs/access_www_2017-12-07.log

/app/logs/access_www_2017-12-09.log

[root@pyrene app]# find /app/logs/ -type f -mtime -7|xargs rm -rf^C

[root@pyrene app]# find /app/logs/ -type f -mtime -7 -exec rm -rf {} ;^C

[root@pyrene app]# rm -rf `find /app/logs/ -type f -mtime -7^C

六、调试系统服务的时候,希望能够实时查看系统日志/var/log/messages的更新,如何去做?

tail –f /var/logs/message   这里tail –f和tailf是一样的  -F可以有重试的效果即,如果这个文件删除了然后创建一个和这个文件相同名字的时候-F还可以继续监听这个文件

7、打印配置文件nginx.conf内容的行号和内容,要怎么做?

模拟数据

[root@pyrene oldboy]# seq 20 >nginx.conf

或者按照下面的模拟

[root@pyrene oldboy]# echo stu{01..20}|xargs -n 1 >nginx.conf

[root@pyrene oldboy]# cat nginx.conf

stu01

stu02

stu03

stu04

stu05

stu06

stu07

stu08

stu09

stu10

stu11

stu12

stu13

stu14

stu15

stu16

stu17

stu18

stu19

stu20

方法:

         cat –nnginx.conf

方法二

         [root@pyrene oldboy]# nl nginx.conf

方法三:

         vim 编辑文件里面  set nu。如果取消行号set nonu

8、中文乱码怎么解决?

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

[root@pyrene app]# cat /etc/sysconfig/i18n

LANG="zh_CN.UTF-8"

LANG="zh_CN.UTF-8"

[root@pyrene app]# source /etc/sysconfig/i18n    #生效

[root@pyrene app]# echo $LANG

zh_CN.UTF-8

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

1wc –l 查看文件行数

2 所有查行号的都可以类似cat –n file|tail -l

解答:

[root@pyrene app]# wc -l /etc/services

10774 /etc/services

10、装完系统之后,希望让网络文件共享服务NFS,仅在三级别上开机启动该如何做

第一种文件配置方法,可以把要启动的服务命令放到/etc/rc.local里面

如下:

[root@pyrene oldboy]# vim /etc/rc.local

[root@pyrene oldboy]# cat /etc/rc.local

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local

#sshd service startup scripts by pyrene

/etc/init.d/sshd start

方法二:

         [root@pyrene oldboy]# chkconfig --list|grep "3:on"

         chkconfig –level 3 sshd on

详细解析为什么chkconfig –list|grep “3:on“就启动?

1、首先看系统初始化脚本

/etc/rc.sysinit

2、运行级别对应的脚本如下

[root@pyrene oldboy]# ll /etc/rc.d/

total 60

drwxr-xr-x. 2 root root  4096 Nov 29 19:07 init.d

-rwxr-xr-x. 1 root root  2617 Oct  4 10:26 rc

-rwxr-xr-x. 1 root root   284 Dec 11 01:00 rc.local

-rwxr-xr-x. 1 root root 20199 Oct  4 10:26 rc.sysinit

drwxr-xr-x. 2 root root  4096 Nov 29 19:06 rc0.d

drwxr-xr-x. 2 root root  4096 Nov 29 19:06 rc1.d

drwxr-xr-x. 2 root root  4096 Nov 29 19:06 rc2.d

drwxr-xr-x. 2 root root  4096 Nov 29 19:06 rc3.d

drwxr-xr-x. 2 root root  4096 Nov 29 19:06 rc4.d

drwxr-xr-x. 2 root root  4096 Nov 29 19:06 rc5.d

drwxr-xr-x. 2 root root  4096 Nov 29 19:06 rc6.d

[root@pyrene oldboy]# ll /etc/rc.d/rc3.d/|grep sshd  查看sshd的启动

lrwxrwxrwx. 1 root root 14 Nov 27 10:11 S55sshd -> ../init.d/sshd

从上面可以看到s55服务启动指向了sshd这个服务

[root@pyrene oldboy]# chkconfig --level 3 sshd off  把这个服务关闭

 [root@pyrene oldboy]# ll /etc/rc.d/rc3.d/|grep sshd

lrwxrwxrwx  1 root root 14 Dec 11 01:36 K25sshd -> ../init.d/sshd

这个时候再看这个文件就发现这个S55已经被删除了,然后出现了K25指向了这个服务

为什么上面55就是启动,然后25就是关闭呢?

 [root@pyrene oldboy]# head /etc/init.d/sshd

#!/bin/bash

#

# sshd                Start up the OpenSSH server daemon

#

# chkconfig: 2345 55 25      就是这里设置的

# description: SSH is a protocol for secure remote shell access.

#              This service starts up the OpenSSH server daemon.

#

# processname: sshd

# config: /etc/ssh/ssh_host_key

如果到时候自己写脚本,然后自启动怎么样?

1、在/etc/init.d/下面写脚本

2、在脚本里面加入

#chkconfig: 启动选项如2345   然后启动的服务如56   关闭服务如27

#description:  脚本名字

3、加入执行权限

chmod +x 后面是文件

手动启动

/etc/init.d/脚本文件 start

4、加入chkconfig

chkconfig –add 脚本文件

例子:

[root@pyrene oldboy]# vim /etc/init.d/pyrene 写入

[root@pyrene oldboy]# cat /etc/init.d/pyrene

#chkconfig:  35   57 27

#description: shiyan

i like bingbing

[root@pyrene oldboy]# /etc/init.d/pyrene start

-bash: /etc/init.d/pyrene: 权限不够

[root@pyrene oldboy]# chmod +x /etc/init.d/pyrene  添加执行权限

 [root@pyrene oldboy]# chkconfig --add pyrene    添加到启动选项

 [root@pyrene oldboy]# chkconfig --list pyrene    查看

pyrene            0:off 1:off 2:off 3:on 4:off 5:on 6:off

[root@pyrene oldboy]# ll /etc/rc.d/rc3.d/|grep pyrene       查看书写的号

lrwxrwxrwx  1 root root 16 Dec 11 01:54 S57pyrene -> ../init.d/pyrene

精简开机系统启动

企业环境有必要开机启动服务有下面

1、sshd  

远程连接linux服务器时需要用到这个服务程序,所以必须要开启

2、rsyslog

日志相关软件,。这是操作系统提供的一种机制,系统的守护程序通常会使用rsylog程序将各种信息写到各个系统日志文件中,centos6之前是syslog

3、network

系统启动时,若想激活关闭各个网络接口,必须考虑开启

4、crond

定时任务

5、sysstat

是一个软件包,包含检测系统性能及效率的一组工具,这些工具对于我们收集系统性能数据很有帮助,比如cpu使用率、应哦按和网络吞吐数据等,对这些数据的收集和分析,有利于系统运行是否正常,所以它是提高系统运行效率,安全运行服务器的得力助手

主要工具:

iostat工具提供cpu使用率及硬盘吞吐效率的数据

mpstat工具提供与单个或多个处理器相关的数据

sar工具负责收集,报告并存储系统活跃的信息

步骤:

1、关掉不需要的

2、然后开启五个

[root@pyrene ~]# chkconfig --list|grep "3:on"|egrep -v "crond|sshd|network|rsyslog|sysstat"|awk '{print "chkconfig",$1,"off"}'|bash

上面这里是先过滤出排除这五个的所有服务,然后用拼接的方法 制造出chkonfig 服务 off  然后再bash执行

[root@pyrene ~]# 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

步骤2

全部关闭,然后开启需要的

chkconfig –list|grep3:on|awk ‘{print “chkconfig”,$1,”off”}’|bash

chkconfig –list|egrep "crond|sshd|network|rsyslog|sysstat"|awk ‘{print “chkconfig”,$1,”on”}’|bash

原文地址:https://www.cnblogs.com/pyrene/p/8052658.html