第1章 软硬链接

1.1 硬链接

1.1.1 含义

多个文件拥有相同的inode号码
硬链接即文件的多个入口

1.1.2 作用

防止你误删除文件

1.1.3 如何创建硬链接

ln 命令,前面是源文件 后面是创建的链接文件

  1. [root@znix clsn]# ln clsn.txt clsn.txt-hard

查看两文件的inode号相同。

  1. [root@znix clsn]# ls -lhi clsn.txt clsn.txt-hard
  2. 151273 -rw-r--r-- 2 root root 607 Aug 30 09:13 clsn.txt
  3. 151273 -rw-r--r-- 2 root root 607 Aug 30 09:13 clsn.txt-hard

1.2 软连接

1.2.1 含义

为了快捷,省事,方便使用
软连接中存放的是源文件的位置

1.2.2 创建软连接

使用ln -s 命令创建软连接

  1. [root@znix clsn]# ln -s clsn.txt clsn.txt-soft
  1. 查看软硬链接的inode号不相同<br /> 但是同时指向的是同一文件
  1. [root@znix clsn]# ll -i clsn*
  2. 151273 -rw-r--r-- 2 root root 607 Aug 30 09:13 clsn.txt
  3. 132910 -rw-r--r-- 1 root root 607 Aug 30 09:14 clsn.txt.bak
  4. 151273 -rw-r--r-- 2 root root 607 Aug 30 09:13 clsn.txt-hard
  5. 132951 lrwxrwxrwx 1 root root 13 Aug 30 09:22 clsn.txt-soft -> clsn.txt

1.3 软连接与硬链接的区别

1.3.1 含义

软链接:
软连接相当于快捷方式
里面存放的是源文件的位置
硬链接:
在同一个分区中,多个文件拥有相同的inode号

1.3.2 创建方式不同

ln 创建硬链接
ln -s 软连接

1.3.3 不同的特点

1)软连接可以随意创建
2)不能对目录创建硬链接
3)对文件创建硬链接可以防止文件被误删除

1.3.4 如何删除

1)删除文件的硬链接,文件可以继续使用
2)只有把这个文件的所有硬链接都删除才可
3)只删除源文件软连接无法使用
4)只删除软连接对文件没有影响

第2章 文件删除原理

2.1 彻底删除一个文件

1.硬链接数为0 与这个文件有关的所有硬链接都被删除。
a)使用rm目录进行删除
2.进程调用数为0,没有人在使用这个文件才能释放磁盘空间。
a)使用lsof 查看谁在使用这文件
b)重启对应的软件/服务就能释放磁盘

2.2 查看某个文件是否总有人在用

使用lsof命令可以列出所有正在使用的文件和相应的进程

  1. [root@znix ~]# lsof /var/log/secure
  2. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  3. rsyslogd 1262 root 4w REG 8,3 1927 270856 /var/log/secure
  4. [root@znix ~]# lsof |grep message
  5. rsyslogd 1262 root 1w REG 8,3 1044 270855 /var/log/messages

2.3 重启对应的软件/服务

找到软件对应的管理地址,让软件重启,释放空间。

  1. [root@znix ~]# /etc/init.d/rsyslog
  2. Usage: /etc/init.d/rsyslog {start|stop|restart|condrestart|try-restart|reload|force-reload|status}

2.4 磁盘空间满了(三种情况)

inode满了……查找出系统目录比较大(1M)
block满了……使用du -sh /* 一层一层找,把较大的文件删除
硬链接数为0,进程调用数不为0

  1. 使用 lsof |grep delete 查看占用的文件

2.5 故障案例

没有被彻底删除-硬链接数为0,进程调用数不为零

2.5.1 环境

向 /var/log/message中放入大量数据

  1. seq 100000000 >>/var/log/messages

2.5.2 查看此时此刻磁盘使用情况

  1. [root@znix apache]# df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. /dev/sda3 8.8G 3.9G 3.7G 59% /
  4. tmpfs 238M 0 238M 0% /dev/shm
  5. /dev/sda1 190M 40M 141M 22% /boot

2.5.3 删除文件

  1. [root@znix apache]# \rm -f /var/log/messages

2.5.4 检查空间没有被释放

  1. [root@znix apache]# df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. /dev/sda3 8.8G 3.9G 3.7G 59% /
  4. tmpfs 238M 0 238M 0% /dev/shm
  5. /dev/sda1 190M 40M 141M 22% /boot

2.5.5 查看被删除(硬链接数为0)但是还被进程调用的文件

  1. [root@znix apache]# lsof |grep delete
  2. rsyslogd 1168 root 1w REG 8,3 889335632 259962 /var/log/messages (deleted)

2.5.6 重启对应的服务

  1. [root@znix apache]# /etc/init.d/rsyslog restart
  2. Shutting down system logger: [ OK ]
  3. Starting system logger: [ OK ]
  1. 查看磁盘空间
  1. [root@znix apache]# df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. /dev/sda3 8.8G 1.5G 6.9G 18% /
  4. tmpfs 238M 0 238M 0% /dev/shm
  5. /dev/sda1 190M 40M 141M 22% /boot

第3章 找出某个文件的其他的硬链接

使用find命令 -inum参数找inode号码,找到相同的inode 互为硬链接。

  1. [root@znix ~]# ls -lhi test.txt
  2. 260141 -rw-r--r--. 2 root root 265 Aug 29 19:16 test.txt
  3. [root@znix ~]# find /* -type f -inum 260141
  4. /root/test.txt
  5. /root/test.txt-hard

第4章 三种时间戳

4.1 含义

  1. Modify mtime修改时间 (最常用) 文件的内容 增加 删除 修改改变
  2. Change ctime属性变更时间 文件属性发生改变时更改
  3. Access atime访问时间 查看文件的时间 (只有文件内容有修改时才会改变)

4.2 使用stat命令查看文件的信息

  1. [root@znix ~]# stat clsn.txt
  2. File: `clsn.txt'
  3. Size: 237 Blocks: 8 IO Block: 4096 regular file
  4. Device: 803h/2051d Inode: 16976 Links: 2
  5. Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
  6. Access: 2017-08-28 11:45:26.734849384 +0800
  7. Modify: 2017-08-28 11:45:26.734849384 +0800
  8. Change: 2017-08-30 11:30:27.783364422 +0800

4.3 修改mtime&change

  1. [root@znix ~]# echo "123">>clsn.txt
  2. [root@znix ~]# stat clsn.txt
  3. File: `clsn.txt'
  4. Size: 241 Blocks: 8 IO Block: 4096 regular file
  5. Device: 803h/2051d Inode: 16976 Links: 2
  6. Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
  7. Access: 2017-08-28 11:45:26.734849384 +0800
  8. Modify: 2017-08-30 11:40:57.384368932 +0800
  9. Change: 2017-08-30 11:40:57.384368932 +0800

4.4 修改ctime (属性变更时间)

  1. [root@znix ~]# ln clsn.txt clsn.txt-hard
  2. [root@znix ~]# stat clsn.txt
  3. File: `clsn.txt'
  4. Size: 241 Blocks: 8 IO Block: 4096 regular file
  5. Device: 803h/2051d Inode: 16976 Links: 3
  6. Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
  7. Access: 2017-08-28 11:45:26.734849384 +0800
  8. Modify: 2017-08-30 11:40:57.384368932 +0800
  9. Change: 2017-08-30 11:42:32.981364780 +0800

4.5 atime修改

  1. [root@znix ~]# tail -1 clsn.txt
  2. 123
  3. [root@znix ~]# stat clsn.txt
  4. File: `clsn.txt'
  5. Size: 241 Blocks: 8 IO Block: 4096 regular file
  6. Device: 803h/2051d Inode: 16976 Links: 3
  7. Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
  8. Access: 2017-08-30 11:43:15.288357930 +0800
  9. Modify: 2017-08-30 11:40:57.384368932 +0800
  10. Change: 2017-08-30 11:42:32.981364780 +0800

第5章 chkconfig命令相关

chkconfig命令实际上控制的是/etc/rc3.d/(3运行模式下)下的软连接。通过不通的软连接,实现不通的控制。实际受/etc/init.d/下脚本的关系。

5.1 执行chkconfig iptables on & chkconfig iptables off之后发生了什么?

执行chkconfig iptables on后/etc/rc3.d/下的/etc/init.d/iptables改为S08iptables

  1. [root@znix rc3.d]# chkconfig iptables on
  2. [root@znix rc3.d]# chkconfig |grep ipt
  3. iptables 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  4. [root@znix rc3.d]# ls -l /etc/rc3.d/ |grep ipt
  5. lrwxrwxrwx 1 root root 18 Aug 30 12:03 S08iptables -> ../init.d/iptables
  1. 执行chkconfig iptables off /etc/rc3.d/下的/etc/init.d/iptables软连接改为K92iptables
  1. [root@znix rc3.d]# chkconfig iptables off
  2. [root@znix rc3.d]# chkconfig |grep ipt
  3. iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off
  4. [root@znix rc3.d]# ls -l /etc/rc3.d/ |grep ipt
  5. lrwxrwxrwx 1 root root 18 Aug 30 12:04 K92iptables -> ../init.d/iptables

iptables 开机自启动 软连接 S开头
iptables 开机不自启动 软连接 K开头

5.2 让一个软件开机自启动

1)把脚本放入/etc/rc.local
2)通过chkconfig 管理命令或脚本,让他开机自启动

5.3 如何让一个服务或命令通过chkconfig管理

5.3.1 脚本必须放在/etc/init.d/目录下面

  1. [root@znix rc3.d]# echo "hostname" > /etc/init.d/clsnd

5.3.2 必须写出chkconfig格式

写出chkconfig 才能被chkconfig管理
hkconfig: 2345 99 99
默认在哪几个运行级别启动 开机顺序 关机顺序
自己创建的开机顺序一般写99 ,99为最后一个

  1. [root@znix rc3.d]# cat /etc/init.d/clsnd
  2. #!/bin/sh
  3. #
  4. #
  5. # chkconfig: 2345 99 99
  6. #
  7. # description: print hostname
  8. hostname

5.3.3 给这个脚本添加上执行的权限

使用chmod命令修改文件权限
需要有可执行的权限,不然无法执行

  1. [root@znix rc3.d]# chmod +x /etc/init.d/clsnd
  2. [root@znix rc3.d]# ll /etc/init.d/clsnd
  3. -rwxr-xr-x 1 root root 9 Aug 30 12:22 /etc/init.d/clsnd

5.3.4 添加脚本到chkconfig管理

  1. [root@znix rc3.d]# chkconfig --add clsnd

5.3.5 查看状态

改为开机自启动

  1. [root@znix rc3.d]# chkconfig |grep old
  2. clsnd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

5.3.6 查看/etc/rc3.d/目录生成相对应的软连接

  1. [root@znix rc3.d]# ls -l /etc/rc3.d/ |grep old
  2. lrwxrwxrwx 1 root root 17 Aug 30 12:38 S99clsnd -> ../init.d/clsnd