L012-老男孩linux高端运维课程—linux系统文件属性知识深入详解
链接http://blog.51cto.com/oldboy/612351是删除原理的案例。
磁盘满案例:http://blogread.cn/it/article/6565?f=wb(inode占满的案例)
点击打开链接http://blogread.cn/it/article/6565?f=wb
磁盘报错“No space left on device",但是df -h查看磁盘空间没有满,请问为什么?
点击打开链接https://www.linuxidc.com/Linux/2017-09/146689.htm
调整apach的配置文件:
1)上述案例实战模拟:
安装htppd web服务:
yum install httpd -y
/etc/init.d/htppd start
/etc/init.d/iptables stop
cd /etc/htppd/conf编辑配置文件,让日志记录到/app/log下面。
2)创建一个小的文件系统,用于存放上述日志。
dd if=/dev/zero of=/dev/sdc bs=8K count=10(相当于创建一个小磁盘。)
ls -l /dev/sdc(查看小磁盘)
mkfs -t ext3 /dev/sdc(格式化)
mount /dev/sdc /app/log(挂载磁盘到/app/log里面去。)
其中会出现以下两行代码,若出现,请输入:mount -o loop /dev/sdc /app/log
[[email protected] conf]# mount /dev/sdc /app/log
mount: /dev/sdc is not a block device (maybe try `-o loop'?)
下面是代码:
[[email protected] hsp]# yum install httpd -y
已加载插件:fastestmirror, refresh-packagekit, security
设置安装进程
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* epel: mirrors.aliyun.com
* extras: centos.ustc.edu.cn
* updates: mirrors.cn99.com
base | 3.7 kB 00:00
epel | 4.7 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
解决依赖关系
--> 执行事务检查
---> Package httpd.x86_64 0:2.2.15-59.el6.centos will be 升级
---> Package httpd.x86_64 0:2.2.15-60.el6.centos.6 will be an update
--> 处理依赖关系 httpd-tools = 2.2.15-60.el6.centos.6,它被软件包 httpd-2.2.15-60.el6.centos.6.x86_64 需要
--> 执行事务检查
---> Package httpd-tools.x86_64 0:2.2.15-59.el6.centos will be 升级
---> Package httpd-tools.x86_64 0:2.2.15-60.el6.centos.6 will be an update
--> 完成依赖关系计算
依赖关系解决
=========================================================================================
软件包 架构 版本 仓库 大小
=========================================================================================
正在升级:
httpd x86_64 2.2.15-60.el6.centos.6 updates 836 k
为依赖而更新:
httpd-tools x86_64 2.2.15-60.el6.centos.6 updates 80 k
事务概要
=========================================================================================
Upgrade 2 Package(s)
总下载量:916 k
下载软件包:
(1/2): httpd-2.2.15-60.el6.centos.6.x86_64.rpm | 836 kB 00:00
(2/2): httpd-tools-2.2.15-60.el6.centos.6.x86_64.rpm | 80 kB 00:00
-----------------------------------------------------------------------------------------
总计 1.1 MB/s | 916 kB 00:00
运行 rpm_check_debug
执行事务测试
事务测试成功
执行事务
正在升级 : httpd-tools-2.2.15-60.el6.centos.6.x86_64 1/4
正在升级 : httpd-2.2.15-60.el6.centos.6.x86_64 2/4
清理 : httpd-2.2.15-59.el6.centos.x86_64 3/4
清理 : httpd-tools-2.2.15-59.el6.centos.x86_64 4/4
Verifying : httpd-2.2.15-60.el6.centos.6.x86_64 1/4
Verifying : httpd-tools-2.2.15-60.el6.centos.6.x86_64 2/4
Verifying : httpd-tools-2.2.15-59.el6.centos.x86_64 3/4
Verifying : httpd-2.2.15-59.el6.centos.x86_64 4/4
更新完毕:
httpd.x86_64 0:2.2.15-60.el6.centos.6
作为依赖被升级:
httpd-tools.x86_64 0:2.2.15-60.el6.centos.6
完毕!
You have new mail in /var/spool/mail/root
[[email protected] hsp]# /etc/init.d/iptables stop
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
You have new mail in /var/spool/mail/root
[[email protected] hsp]# getenforce
Enforcing
[[email protected] hsp]# /usr/sbin/sestatus -v
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: enforcing
Mode from config file: enforcing
Policy version: 24
Policy from config file: targeted
Process contexts:
Current context: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Init context: system_u:system_r:init_t:s0
/sbin/mingetty system_u:system_r:getty_t:s0
/usr/sbin/sshd system_u:system_r:sshd_t:s0-s0:c0.c1023
File contexts:
Controlling term: unconfined_u:object_r:user_devpts_t:s0
/etc/passwd system_u:object_r:etc_t:s0
/etc/shadow system_u:object_r:shadow_t:s0
/bin/bash system_u:object_r:shell_exec_t:s0
/bin/login system_u:object_r:login_exec_t:s0
/bin/sh system_u:object_r:bin_t:s0 -> system_u:object_r:shell_exec_t:s0
/sbin/agetty system_u:object_r:getty_exec_t:s0
/sbin/init system_u:object_r:init_exec_t:s0
/sbin/mingetty system_u:object_r:getty_exec_t:s0
/usr/sbin/sshd system_u:object_r:sshd_exec_t:s0
You have new mail in /var/spool/mail/root
[[email protected] hsp]# getenforce
Enforcing
[[email protected] hsp]# vim /etc/selinux/config (将SELinux status: enabled中的enabled改为disabled)
You have new mail in /var/spool/mail/root
[[email protected] hsp]# reboot
welcom To hsp Training Computer Service!
[[email protected] ~]$ su -
密码:
[[email protected] ~]# getenforce
Disabled
[[email protected] ~]# vim /etc/selinux/config
[[email protected] ~]# cd /etc/httpd/conf
[[email protected] conf]# ll
总用量 52
-rw-r--r--. 1 root root 34419 9月 19 2017 httpd.conf(表示已经增加了httpd.conf)
-rw-r--r--. 1 root root 13139 10月 20 2017 magic
You have new mail in /var/spool/mail/root
[[email protected] conf]# grep log httpd.conf
# Configuration and logfile names: If the filenames you specify for many
# with "/", the value of ServerRoot is prepended -- so "logs/foo.log"
# server as "/etc/httpd/logs/foo.log".
# configuration, error, and log files are kept.
LoadModule log_config_module modules/mod_log_config.so
LoadModule logio_module modules/mod_logio.so
#LoadModule log_forensic_module modules/mod_log_forensic.so
# ErrorLog: The location of the error log file.
# logged here. If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
ErrorLog logs/error_log
# LogLevel: Control the number of messages logged to the error_log.
# requires the mod_logio module to be loaded.
# The location and format of the access logfile (Common Logfile Format).
# If you do not define any access logfiles within a <VirtualHost>
# container, they will be logged here. Contrariwise, if you *do*
# define per-<VirtualHost> access logfiles, transactions will be
# logged therein and *not* in this file.
#CustomLog logs/access_log common
# If you would like to have separate agent and referer logfiles, uncomment
#CustomLog logs/referer_log referer
#CustomLog logs/agent_log agent
# For a single logfile with access, agent, and referer information
CustomLog logs/access_log combined
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
You have new mail in /var/spool/mail/root
[[email protected] conf]# cp httpd.conf httpd.conf.ori(一般修改配置文件之前先进行备份。)
[[email protected] conf]# sed -i '[email protected] logs/access_log [email protected] /app/log/access_log [email protected]' /etc/httpd/conf/httpd.conf(将CustomLog logs/access_log combined修改为CustomLog /app/log/access_log combined
You have new mail in /var/spool/mail/root
[[email protected] conf]# grep app httpd.conf
# /etc/sysconfig/httpd must be set appropriately if this location is
# All of these directives may appear inside <VirtualHost> containers,
# e-mailed. This address appears on some server-generated pages, such
# UserDir: The name of the directory that is appended onto a user's home
# a good value. If most of your content is binary, such as applications
# or images, you may want to use "application/octet-stream" instead to
# EnableMMAP: Control whether memory-mapping is used to deliver
CustomLog /app/log/access_log combined
# documents in the realname directory are treated as applications and
# The same rules about trailing "/" apply to ScriptAlias directives as to
# IndexOptions: Controls the appearance of server-generated directory
# default, and append to directory listings.
#AddType application/x-tar .tgz
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
# includes to substitute the appropriate text.
# You can modify the messages' appearance without changing any of the
# problem with Microsoft WebFolders which does not appropriately handle
[[email protected] conf]# mkdir /app/log -p
You have new mail in /var/spool/mail/root
[[email protected] conf]# dd if=/dev/zero of=/dev/sdc bs=8K count=10(相当于创建一个小磁盘。)
记录了10+0 的读入
记录了10+0 的写出
81920字节(82 kB)已复制,0.000286598 秒,286 MB/秒
You have new mail in /var/spool/mail/root
[[email protected] conf]# ls -l /dev/sdc
-rw-r--r-- 1 root root 81920 5月 19 21:20 /dev/sdc(表示已经创建完毕)
[[email protected] conf]# mkfs -t ext3 /dev/sdc
mke2fs 1.41.12 (17-May-2010)
/dev/sdc is not a block special device.
无论如何也要继续? (y,n) y
文件系统标签=
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
16 inodes, 80 blocks
4 blocks (5.00%) reserved for the super user
第一个数据块=1
1 block group
8192 blocks per group, 8192 fragments per group
16 inodes per group
正在写入inode表: 完成
文件系统小得无法记录日志
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
You have new mail in /var/spool/mail/root
[[email protected] conf]# mount /dev/sdc /app/log
mount: /dev/sdc is not a block device (maybe try `-o loop'?)
[[email protected] conf]# mount -o loop /dev/sdc /app/log
You have new mail in /var/spool/mail/root
[[email protected] conf]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_hsp-lv_root
8.3G 5.8G 2.5G 71% /
tmpfs 491M 72K 491M 1% /dev/shm
/dev/sda1 477M 36M 416M 8% /boot
/dev/sdc 73K 14K 55K 21% /app/log
You have new mail in /var/spool/mail/root
[[email protected] conf]# /etc/init.d/httpd restart
停止 httpd: [确定]
正在启动 httpd: [确定]
[[email protected] conf]# cd /app/log
You have new mail in /var/spool/mail/root
[[email protected] log]# cd /app/log
下面是历史记录:
在执行最后一步cd /app/log前,要进入到老男孩51cto进行网站ip的输入,然后会生成一篇日志。执行cd /app/log会看到。
for n in `seq 100000`;do curl -s 10.0.0.18 > /dev/null;done(这是一个循环可以占满空间)。
rm -f access_log :删除access_log。
df -h:查看内存之后依然有被占用。
lsof|grep del:过滤出来我们刚才删除的内容。
lsof -i : port:查看端口,list open files,还可以查看文件进程被占用情况。
下面一个图是解决方法:
也就是说重启httpd服务之后才能看出来。如下图:
清空文件命令:
1)> access_log
2)cat /dev/null > access_log
du -sh /*:查看/目录下所有文件的空间大小。
生产场景:最佳解决方案:
1)删除文件,重启。
2)清空文件,不删除。
3)不要删除当天的被进程占用的文件,删除以前的没事。
为什么是两个?
一个是它本身,一个是它里边的“.“。
如果在/oldboy/ett目录下再创建一个目录,那么/oldboy/ett的硬链接数是多少,为什么?
3个。
总结:
硬链接小结:
1.不同的文件名具有相同Inode节点号的文件是互为硬连接文件。
2.删除硬连接文件或者删除源文件之一,文件实体并未被删除。
3.只有删除了源文件及所有对应的硬链接文件,文件实体才会被删除。
4.当所有的硬连接文件及源文件被删除后,再存放新的数据会占用这个文件的空间,或者
磁盘fsck检查的时候,数据也会被回收。
5.硬连接文件是文件的又一个入口。
6.通过执行命令“ln 源文件 硬连接文件”,即可完成创建硬连接。
7.可以通过给文件设置硬链接文件,来防止重要文件被误删。
8.rm -f 硬链接
软连接小结:
查看软连接的value:readlink
1.软链接类似Windows里的快捷方式,软连接里存放的是源文件的路径,并且执行
源文件。
2.删除源文件,软连接文件依然存在,但是无法访问指向的路径内容了。
3.通过ln -s 源文件,软连接文件,即可创建软连接
4.软连接和源文件是不同的文件,也是不同的文件。inode号不同。
5.rm -f
ln命令不能对目录创建硬链接,但可以对目录创建软链接。
往源文件oldboyfile里写入内容后,它的软硬链接文件同样都有相同内容
当删除原始文件oldboyfile后,其硬链接文件oldboyfile_hard_link不受影响,对应的数据
依然存在,但是其对应的软链接文件oldboufile_soft_link失效了,找不到源文件了,此时,
可以删除软连接文件重新创建,或者创建一个oldboyfile_hard_link的硬连接文件oldboyfile
删除软连接文件对源文件及硬连接文件无任何影响
删除硬连接文件对源文件及软连接文件无任何影响
删除源文件,对硬连接没有影响,但是会导致软连接文件失效,白字红底闪烁。
同时删除源文件,硬连接文件,整个文件会真正的被删除
很多硬件设备中的快照功能,就是利用了硬连接的原理。
源文件和硬连接文件具有相同的索引节点号,可以认为是同一个文件或一个文件的多个入口
源文件和软连接文件索引节点号不同,是不同的文件,软连接相当于源文件的快捷方式。含有源文件的位置指向。
对于目录,不可以创建硬连接,但可以创建软连接。
对于目录的软连接是生产场景运维中常用的技巧
目录的硬链接不能跨越文件系统(从原理可以理解)
每个目录下面都有一个硬链接“.”号,和对应上一级目录的链接“..”。
再目录里创建一个目录,父目录的链接数增加 1(子目录里都有..来指向父目录)。
但是在父目录里创建文件,父目录的链接数就不会增加。
描述linux下软连接和硬连接的区别。(面试题)
解答:
在linux系统中,连接分两种,一种被称为硬连接(hard link),另一种被称为符号连接或
软连接(Symbolic Link)
1。默认不带参数情况下,ln命令创建的是硬连接,带-s参数的ln命令创建的是软连接
2。硬连接文件与源文件的inode节点号相同,而软链接文件的inode节点与源文件不同
3。ln命令不能对目录创建硬链接,但可以创建软连接,对目录的软连接会经常被用到。
4。删除软连接文件,对源文件及硬连接文件无任何影响。
5。删除文件的硬连接文件,对源文件及软连接无任何影响
6。删除链接文件的源文件,对硬连接文件无影响,会导致其软链接失效(红底白字闪烁)
7。同时删除源文件及其硬链接文件,整个文件才会被真正的删除
8。很多硬件设备中的快照功能,使用的就是类似硬连接的原理。
9。软练级额可以跨文件系统,硬链接不可以跨文件系统。
20:53:15
16-林 2018-5-22 20:53:15
1)磁盘分区格式化文件系统后,会分为inode和block两部分内容
2)inode存放文件的属性以及指向文件实体的指针,文件名不再inode里,一般上级目录的
block。
3)访问文件,通过-->inode-->blocks。
4)inode一般情况默认大小256B,block大小1 2 4K,默认是4k,注意,引导分区等特殊分区
除外。
5)通过df -i查看inode的数量及使用情况,dumpe2fs /dev/sda3查看inode及blok的大小及数量
6)一个文件至少要占用inode及一个block,多个文件可以占用一个inode(硬链接)。
7)一个block只能被一个文件使用,如果文件很小block太大,
剩余空间良妃,无法继续被其他文件使用。
8)Block不是越大越好,要根据业务的文件大小进行选择,一般就是4K。
9)可以在格式化的时候改变Inode及Block的大小。
[[email protected] ~]# mkfs.ext4 --help
mkfs.ext4: invalid option -- '-'
Usage: mkfs.ext4 [-c|-l filename] [-b block-size] [-f fragment-size]
[-i bytes-per-inode] [-I inode-size] [-J journal-options]
1)安装系统最小化
2)开启程序服务最小化原则
3)操作最小化原则。rm -f test.txt
4)登陆最小化原则。平时没有需求不用root登陆,要有普通登陆
5)权限最小化
6)配置参数合理,不要最大化
type:Display information about command type.
UID UserIdentify:用户ID。
GID GroupIdentify:主ID。
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
uucp
operator
games
gopher
ftp
nobody
dbus
abrt
rpc
usbmuxd
rtkit
nscd
vcsa
rpcuser
nfsnobody
apache
saslauth
postfix
unbound
ntp
mysql
avahi-autoipd
hsqldb
haldaemon
gdm
pulse
sshd
nslcd
tcpdump
hsp
msnb
msn
msn1
除了root和自己创建的hsp,msnb,msn,msn1。其他的都是虚拟用户。大多数的虚拟用户都是登陆不了的(/sbin/nologin)。自己创建的都有bash解释器(/bin/bash)。包括root。
为什么虚拟用户会存在?
在linux中,一个用户诞生,它要属于一个组的,当一个文件诞生它要属于一个用户的,这个用户又指向一个组,同理,linux中有服务和进程,他们都要属于用户的,所以就有很多的虚拟用户诞生。
ps -ef:查看进程。
也就是说,执行这些httpd程序是需要用户的,所以会有这些虚拟用户。
人工添加的普通用户:
hsp:x:500:500:hsp:/home/hsp:/bin/bash
msnb:x:501:501::/home/msnb:/bin/bashmsn:x:502:502::/home/msn:/bin/bash
msn1:x:503:503::/home/msn1:/bin/bash
You have new mail in /var/spool/mail/root
下边是一个例子:(su - mysql1证明了/sbin/nologin)
对上总结:服务运行是需要用户角色的,可以不用登陆,因此,工作中我们需要运行mysql数据水库,可以创建以上用户。
对于上图各个字段的解释:
stat a.txt:查看文件a.txt的属性。
对于上图:绿色部分在文件的inode里面,红色部分在上一级目录的block里面。
在虚拟机开机的时候按esc可以看开机过程。,再按退出。
vim /etc/selinux/config:进入到这个文件可以修改selinux。
将SELINUX=enforcing改为SELINUX=disabled
重启机器即可。
课后复习题:
1-15题答案:
一:
可以用stat 命令可直接查看文件的权限(为数字),如上图的0644,其实文件的权限是有12位的,就有四个数字代表。
二:
如果在/oldboy/ett下又创建一个目录test,硬链接数是几个?为什么?
更详细的介绍:
"test",".",".."这三个表示硬链接文件。
三:
四:
五(企业案例:企业的网站遭受木马攻击了,导致网站目录下的所有文件被篡改了。):
对于上图:' '里边的1表示第一行,i表示插入,oldboy表示内容。
xargs:把前面得到的文件名作为后边的处理目标。
不要忘了亡羊补牢,寻找问题来源,补好漏洞。以下两点:
1.网站目录权限降低。
2.上床附件的入口做详细过滤。
其中的xargs可以换掉。详细如下:
六:
七:
八:
-n:不换行输出。
-e:转义字符。制定特殊字符(\n,\t等)。
九:
增加:
生产环境举例:
每天打一个包,每天打一个包。
十:
1)执行全路径。
2)把全路径加到/etc/profile中让其全局生效。
十一:
十二:
十三:
十四:
1)系统日志文件。
2)安全文件或者说是显示登陆信息的文件。
3)邮件临时文件。
4)查看中断文件。
5)系统开机自动挂载磁盘的文件。
6)一般把环境变量放进去的文件。也就是全局的环境变量存放的一个文件。
十五:
where -b:-b查看二进制命令。
locate只能查updatedb库里的内容。
updatedb命令用来创建或更新slocate命令所必需的数据库文件。updatedb命令的执行过程较长,因为在执行时它会遍历整个系统的目录树,并将所有的文件信息写入slocate数据库文件中。 补充说明:slocate本身具有一个数据库,里面存放了系统中文件与目录的相关信息。 参数: -o<文件>:忽略默认的数据库文件,使用指定的slocate数据库文件; -U<目录>:更新指定目录的slocate数据库; -v:显示执行的详细过程。
十七:
除了以上给出的还有last,lastlog。不过最常用的还是who和w。
附加题:
十八:
十九:
二十:
本次涉及到的命令:
以下的命令需要总结:
预习内容: