Centos 05 系统目录讲解

本节内容

  1、linux目录结构

  2、主目录功能简介

  3、重要子目录 

linux目录结构

在linux里面,逻辑上所有目录只有一个顶点,根是所有目录的起点

根下面是类似一个倒挂的树一样的层次结构

可以通过tree命令查看

[root@centos7 ~]# tree / -L 1
/
├── bin -> usr/bin
├── boot
├── dandy
├── dev
├── etc
├── home
├── lib -> usr/lib
├── lib64 -> usr/lib64
├── media
├── mnt
├── opt
├── proc
├── root
├── run
├── sbin -> usr/sbin
├── srv
├── sys
├── tmp
├── usr
└── var

20 directories, 0 files

关于分区

linux分区不可以直接写入数据,需要有分区入口(挂载点)(/var/)才能往里面写数据,不同目录可以对应不同的分区。甚至/var下面的目录还可以挂载其他分区。

挂载

mount /dev/sdb1 /usr 挂载     

系统的挂载文件

[root@centos7 ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Wed Aug 22 21:00:24 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=174e96cf-3f83-4624-89e7-ca8a0d202e40 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

linux的目录是有规律的,按照类别组织的,比如

应用程序             /usr/bin
数据文件跟帮助       /usr/share
配置文件             /etc
启动命令             /etc/init.d

小结

    1、linux的所有目录结构是一个层次的倒挂着的树形目录结构
    2、根/是所有目录的顶点
    3、目录结构和分区设备是没有关系的,也就是不同的目录可以跨越不同的磁盘设备或分区
    4、所有的目录都是按照一定的类别有规律的组织和命名的

 

主目录功能简介

/bin

常用二进制命令所在目录cp mkdir cat 

/boot 

linux内核及引导系统程序所需的文件目录

/dev

设备文件目录
比如声卡,磁盘,光驱  /dev/null(黑洞)
/dev/null的用法 cat /dev/null > /dandy/dandy.txt     读取黑洞,清空文件 也可以省略前面的直接>/dandy/dandy.txt

/etc

1、二进制安装包(yum,rpm)的配置文件默认路径  
2、服务启动命令存放的目录/etc/init.d/
/etc/init.d/(sshd/network/iptables...) restart 

这里单独说一下/etc/motd文件,作用是用来在登陆的时候的显示信息
[root@centos7 ~]# cat /etc/motd  #这个文件可能不存在,需要自己创建
centos7 for dandy test

修改显示信息

echo 'centos7 for dandy test'>/etc/motd 

/home

普通用户家目录

/lib

库文件存放目录;linux正常工作需要很多库文件,在内核和应用程序之间做一个承上启下的作用

/lost+found

在ext3文件系统中,当系统意外崩溃或机器意外关机,会产生文件碎片在这里。系统开机启动的过程fsck工具会检查这里,修复损坏文件系统。
系统发生问题,可能会有问题被一道这个目录,可能需要手工方式修复,或移动到文件原来位置。

/mnt

一般是用于临时挂载存储设备的挂载目录,比如有cdrom,u盘。挂载点 --> 文件系统的(设备)入口
fdisk -l 查看磁盘
df -h 查看磁盘及使用情况
mkfs.ext4 /dev/sdb 格式化新盘
mount /dev/sdb /mnt 挂载 
umount /mnt 取消挂载
mount -t ext4 /dev/sdb /mnt 带类型的挂载

给个示例,这个示例需要先关闭虚拟机并给当前虚拟机添加一块新的磁盘。

 

[root@centos7 ~]# fdisk -l

磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000b142c

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

磁盘 /dev/sdb106 MB, 106954752 字节,208896 个扇区  # 这里已经添加进新的磁盘
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-root:18.2 GB, 18249416704 字节,35643392 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
[root@centos7 ~]# mkfs.ext4 /dev/sdb  # 格式化磁盘
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
无论如何也要继续? (y,n) y
文件系统标签=
OS type: Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
26208 inodes, 104448 blocks
5222 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
2016 inodes per group
Superblock backups stored on blocks: 
    8193, 24577, 40961, 57345, 73729

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成 
[root@centos7 ~]# mount /dev/sdb /mnt  # 挂载磁盘
[root@centos7 ~]# df -h  # 查看磁盘信息
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  1.3G   16G    8% /
devtmpfs                 476M     0  476M    0% /dev
tmpfs                    488M     0  488M    0% /dev/shm
tmpfs                    488M  7.7M  480M    2% /run
tmpfs                    488M     0  488M    0% /sys/fs/cgroup
/dev/sda1               1014M  159M  856M   16% /boot
tmpfs                     98M     0   98M    0% /run/user/0
/dev/sdb                  95M  1.6M   87M    2% /mnt
[root@centos7 ~]# touch /mnt/test.txt
[root@centos7 ~]# ls /mnt/
lost+found  test.txt

/opt

表示可选的意思,自定义软件包会被安装在这个目录

/proc

内核和进程信息的虚拟文件系统;
/proc目录伪装的文件系统,并不是真正的文件系统
cat /proc/meminfo    查看内存信息
cat /proc/cpuinfo     查看cpu信息
cat /proc/loadavg     查看负载信息

/sbin

大多是设计系统管理的命令的存放,是超级权限用户root的可执行命令存放处跟/usr/sbin;/usr/X11R6/sbin或/usr/local/sbin目录相似    

/tmp

临时文件存放

/usr

系统存放程序的目录,比如命令,帮助文件,安装Linux发行版官方提供的软件包,大多安装在这里
是文件系统的第二个主要部分;
存放可执行文件的目录;自己写的命令可以放在 /usr/local/sbin下面 
[root@centos7 ~]# echo 'echo "test usr local sbin"'>/usr/local/sbin/test111  # 写入命令文件
[root@centos7 ~]# chmod +x /usr/local/sbin/test111  # 命令文件开通权限
[root@centos7 ~]# test111  # 测试命令
test usr local sbin

/var

目录内容经常变动;/var/log存放系统日志的目录;
系统日志路径 /var/log/messages
系统安全信息的日志 /var/log/secure  
 1 [root@centos7 ~]# tail /var/log/messages
 2 Sep 25 14:08:05 centos7 kernel: EXT4-fs (sdb): mounted filesystem with ordered data mode. Opts: (null)
 3 Sep 25 14:08:08 centos7 kernel: sdb: WRITE SAME failed. Manually zeroing.
 4 Sep 25 14:10:01 centos7 systemd: Started Session 3 of user root.
 5 Sep 25 14:10:01 centos7 systemd: Starting Session 3 of user root.
 6 Sep 25 14:20:01 centos7 systemd: Starting Cleanup of Temporary Directories...
 7 Sep 25 14:20:01 centos7 systemd: Started Session 4 of user root.
 8 Sep 25 14:20:01 centos7 systemd: Starting Session 4 of user root.
 9 Sep 25 14:20:01 centos7 systemd: Started Cleanup of Temporary Directories.
10 Sep 25 14:30:01 centos7 systemd: Started Session 5 of user root.
11 Sep 25 14:30:01 centos7 systemd: Starting Session 5 of user root.
12 
13 [root@centos7 ~]# tail /var/log/secure
14 Sep 25 14:04:31 centos7 polkitd[652]: Loading rules from directory /etc/polkit-1/rules.d
15 Sep 25 14:04:31 centos7 polkitd[652]: Loading rules from directory /usr/share/polkit-1/rules.d
16 Sep 25 14:04:31 centos7 polkitd[652]: Finished loading, compiling and executing 2 rules
17 Sep 25 14:04:31 centos7 polkitd[652]: Acquired the name org.freedesktop.PolicyKit1 on the system bus
18 Sep 25 14:04:35 centos7 sshd[1040]: Server listening on 0.0.0.0 port 22.
19 Sep 25 14:04:35 centos7 sshd[1040]: Server listening on :: port 22.
20 Sep 25 14:04:41 centos7 login: pam_unix(login:session): session opened for user root by LOGIN(uid=0)
21 Sep 25 14:04:41 centos7 login: ROOT LOGIN ON tty1
22 Sep 25 14:05:03 centos7 sshd[1244]: Accepted password for root from 10.0.2.14 port 51490 ssh2
23 Sep 25 14:05:03 centos7 sshd[1244]: pam_unix(sshd:session): session opened for user root by (uid=0)
View Code

关于开机,关机的一些小命令

halt 关机
init 0 关机
reboot 重启
init 6 重启
shutdown -r now(重启) -h now(关机)

 

重要的子目录

1、/etc目录下

/etc/sysconfig/network-scripts/ifcfg-ens33 网卡配置文件
    重启所有网卡/etc/init.d/network restart
    关闭网卡ifdown ens33 && 打开网卡ifup ens33
/etc/resolv.conf linux本地客户端的DNS配置文件
/etc/sysconfig/network 修改机器名及网卡启动,网关等配置
/etc/hosts 设定用户ip与名字的对应解析表,相当于本地的DNS或局域网的DNS
/etc/fstab 记录开机自动挂载的文件
/etc/rc.local 存放开机自启动程序命令的文件 (chkconfig只能管理yum跟rpm包)
/etc/inittab 设定系统启动时,init进程将系统设置成什么样的runlevel及加载相关的启动文件设置
/etc/exports 设定NFS网络文件系统用的配置文件
/etc/profile 系统全局环境变量配置路径 可以放置alias跟PATH
/etc/profile.d 加在系统登录程序的一个目录,命令或文件,独立存在  
/etc/issue 记录用户登录前显示的信息,本地终端登录时显示
/etc/motd 每次用户登录时,/etc/motd文件的内容会显示在用户的终端,比如系统更新版本,何时更新等
/etc/redhat-release 查看系统版本
uname -r 内核版本
/etc/group 设定用户的组名与相关信息  chattr+i锁文件
/etc/passwd 账号信息文件
/etc/shadow 密码信息文件
    useradd msn 会修改/etc/passwd /etc/shadow /etc/group /etc/gshadow
/etc/sudoers 可以执行使用sudo命令的配置文件
/etc/securetty 设定哪些终端可以让root登陆
/etc/login.defs 所有用户登陆时的缺省配置
/etc/rsyslog.conf 版本日志设置文件;centos5的syslog.conf

2、/usr目录下

/usr/local 一般用来存放用户自编译安装软件的存放目录,一般是通过源码包安装的软件,相当于c:\Program files
/usr/src 内核源码存放目录

3、/var目录下

/var 日志文件
/var/log 各种系统日志存放地
/var/log/messages 系统信息默认日志文件,按周轮询
/var/log/secure 记录登入系统存取信息的文件
/var/log/wtmp 记录登陆者信息的文件,是二进制文件,需要借助last -f /var/log/wtmp来查看
查看系统登陆信息 last
查看所有系统用户的最后一次登陆 lastlog
/var/spool
/var/spool/cron/root 定时任务crontab默认路径,按用户名命名的文件
/var/spool/clientmqueue sendmail临时邮件文件目录。偶尔会因为这个目录文件太多,/var分区的inode数量被消耗尽,无法写入文件的情况。
                        centos6默认不安装sendmail,所以不会有这个问题
                        df -i 查看剩余inode
                        df -h 查看磁盘空间
 1 [root@centos7 ~]# ls /var/log/messages*
 2 /var/log/messages  /var/log/messages-20180826  /var/log/messages-20180903  /var/log/messages-20180923
 3 [root@centos7 ~]# tail /var/log/messages-20180923 
 4 Sep 23 09:01:01 centos7 systemd: Started Session 9 of user root.
 5 Sep 23 09:01:01 centos7 systemd: Starting Session 9 of user root.
 6 Sep 23 09:10:01 centos7 systemd: Started Session 10 of user root.
 7 Sep 23 09:10:01 centos7 systemd: Starting Session 10 of user root.
 8 Sep 23 09:20:01 centos7 systemd: Started Session 11 of user root.
 9 Sep 23 09:20:01 centos7 systemd: Starting Session 11 of user root.
10 Sep 23 09:30:01 centos7 systemd: Started Session 12 of user root.
11 Sep 23 09:30:01 centos7 systemd: Starting Session 12 of user root.
12 Sep 23 09:40:01 centos7 systemd: Started Session 13 of user root.
13 Sep 23 09:40:01 centos7 systemd: Starting Session 13 of user root.
14 [root@centos7 ~]# tail /var/log/secure
15 Sep 25 14:04:31 centos7 polkitd[652]: Loading rules from directory /etc/polkit-1/rules.d
16 Sep 25 14:04:31 centos7 polkitd[652]: Loading rules from directory /usr/share/polkit-1/rules.d
17 Sep 25 14:04:31 centos7 polkitd[652]: Finished loading, compiling and executing 2 rules
18 Sep 25 14:04:31 centos7 polkitd[652]: Acquired the name org.freedesktop.PolicyKit1 on the system bus
19 Sep 25 14:04:35 centos7 sshd[1040]: Server listening on 0.0.0.0 port 22.
20 Sep 25 14:04:35 centos7 sshd[1040]: Server listening on :: port 22.
21 Sep 25 14:04:41 centos7 login: pam_unix(login:session): session opened for user root by LOGIN(uid=0)
22 Sep 25 14:04:41 centos7 login: ROOT LOGIN ON tty1
23 Sep 25 14:05:03 centos7 sshd[1244]: Accepted password for root from 10.0.2.14 port 51490 ssh2
24 Sep 25 14:05:03 centos7 sshd[1244]: pam_unix(sshd:session): session opened for user root by (uid=0)
25 
26 [root@centos7 ~]# last -f /var/log/wtmp
27 root     pts/0        shuais-mbp       Tue Sep 25 14:05   still logged in   
28 root     tty1                          Tue Sep 25 14:04   still logged in   
29 reboot   system boot  3.10.0-862.11.6. Tue Sep 25 14:04 - 15:02  (00:57)    
30 root     tty1                          Tue Sep 25 14:02 - 14:03  (00:01)    
31 reboot   system boot  3.10.0-862.11.6. Tue Sep 25 14:02 - 14:03  (00:01)    
32 root     pts/0        10.0.2.14        Tue Sep 25 11:46 - 13:38  (01:52)    
33 root     pts/1        shuais-mbp       Tue Sep 25 10:23 - 11:46  (01:22)    
34 root     pts/0        192.168.1.5      Mon Sep 24 22:11 - 10:28  (12:17)    
35 root     pts/1        192.168.1.5      Mon Sep 24 17:13 - 18:02  (00:48)    
36 root     pts/0        192.168.1.5      Mon Sep 24 14:27 - 17:14  (02:46)    
37 root     pts/1        192.168.1.5      Mon Sep 24 10:05 - 14:27  (04:21)    
38 root     tty1                          Sun Sep 23 23:46 - 13:39 (1+13:53)   
39 root     pts/0        192.168.1.5      Sun Sep 23 23:03 - 10:06  (11:02)    
40 reboot   system boot  3.10.0-862.11.6. Sun Sep 23 22:46 - 13:40 (1+14:53)   
41 root     pts/0        192.168.1.5      Sun Sep 23 22:30 - down   (00:16)    
42 root     pts/0        192.168.1.5      Sun Sep 23 22:09 - 22:30  (00:20)    
43 root     pts/1        192.168.1.5      Sun Sep 23 21:55 - 22:09  (00:13)    
44 root     pts/0        192.168.1.5      Sun Sep 23 16:54 - 21:55  (05:00)    
45 root     pts/0        192.168.1.5      Sun Sep 23 16:43 - 16:54  (00:10)    
46 root     pts/1        192.168.1.5      Sun Sep 23 15:04 - 16:43  (01:39)    
47 root     pts/0        192.168.1.5      Sun Sep 23 14:24 - 15:05  (00:40)    
48 root     tty1                          Sun Sep 23 14:23 - 22:46  (08:22)    
49 reboot   system boot  3.10.0-862.11.6. Sun Sep 23 14:23 - 22:46  (08:22)    
50 root     pts/0        192.168.1.5      Sun Sep 23 13:45 - down   (00:38)    
51 root     pts/0        192.168.1.5      Sun Sep 23 10:08 - 13:45  (03:36)    
52 root     pts/0        192.168.1.5      Sun Sep 23 09:00 - 10:08  (01:07)    
53 root     pts/0        192.168.1.5      Sat Sep 22 22:48 - 23:07  (00:18)    
54 root     tty1                          Sat Sep 22 22:34 - 14:23  (15:48)    
55 reboot   system boot  3.10.0-862.11.6. Sat Sep 22 22:34 - 14:23  (15:49)    
56 root     tty1                          Sat Sep 22 22:16 - 22:34  (00:17)    
57 reboot   system boot  3.10.0-862.11.6. Sat Sep 22 22:16 - 22:34  (00:18)    
58 root     tty1                          Sat Sep 22 22:10 - 22:15  (00:05)    
59 reboot   system boot  3.10.0-862.11.6. Sat Sep 22 22:10 - 22:34  (00:24)    
60 root     pts/0        shuais-mbp       Tue Sep  4 10:35 - 13:14  (02:38)    
61 root     tty1                          Tue Sep  4 10:34 - 22:09 (18+11:35)  
62 reboot   system boot  3.10.0-862.11.6. Tue Sep  4 10:34 - 22:09 (18+11:35)  
63 dandy    pts/1        172.16.54.1      Tue Sep  4 09:48 - 10:32  (00:43)    
64 root     pts/0        172.16.54.1      Mon Sep  3 21:06 - 09:48  (12:41)    
65 root     pts/1        172.16.54.1      Sat Sep  1 14:45 - 21:06 (2+06:21)   
66 root     pts/0        172.16.54.1      Wed Aug 29 22:58 - 15:09 (2+16:11)   
67 root     tty1                          Tue Aug 28 23:40 - 10:34 (6+10:53)   
68 reboot   system boot  3.10.0-862.11.6. Tue Aug 28 23:40 - 10:34 (6+10:53)   
69 root     tty1                          Tue Aug 28 23:17 - 23:40  (00:22)    
70 reboot   system boot  3.10.0-862.11.6. Tue Aug 28 23:16 - 10:34 (6+11:17)   
71 root     tty1                          Tue Aug 28 22:44 - 23:16  (00:31)    
72 reboot   system boot  3.10.0-862.11.6. Tue Aug 28 22:44 - 10:34 (6+11:50)   
73 dandy    pts/0        172.16.54.1      Tue Aug 28 20:50 - 22:43  (01:52)    
74 dandy    pts/1        172.16.54.1      Tue Aug 28 11:35 - 13:29  (01:54)    
75 dandy    pts/1        172.16.54.1      Tue Aug 28 11:33 - 11:34  (00:00)    
76 dandy    pts/2        172.16.54.1      Tue Aug 28 10:27 - 11:05  (00:38)    
77 dandy    pts/0        172.16.54.1      Tue Aug 28 10:16 - 13:29  (03:13)    
78 dandy    pts/1        172.16.54.1      Sun Aug 26 17:43 - 10:35 (1+16:51)   
79 dandy    pts/0        172.16.54.1      Sun Aug 26 14:18 - 19:45  (05:26)    
80 dandy    pts/1        172.16.54.1      Sun Aug 26 09:33 - 15:36  (06:03)    
81 dandy    pts/1        172.16.54.1      Sat Aug 25 22:30 - 23:42  (01:12)    
82 dandy    pts/0        172.16.54.1      Thu Aug 23 20:40 - 10:00 (2+13:20)   
83 dandy    pts/0        172.16.54.1      Wed Aug 22 23:05 - 23:28  (00:22)    
84 dandy    pts/0        172.16.54.1      Wed Aug 22 21:11 - 23:05  (01:54)    
85 root     tty1                          Wed Aug 22 21:08 - 22:43 (6+01:35)   
86 reboot   system boot  3.10.0-862.el7.x Wed Aug 22 21:07 - 22:43 (6+01:35)   
87 
88 wtmp begins Wed Aug 22 21:07:51 2018
View Code

这里提到了clientqueue跟inode,关于小文件过多,inode数量被消耗,无法写入文件的问题,请查看文章

4、/proc目录下

/proc 虚拟目录,是内存的映射,内核和进程的虚拟文件系统目录
/proc/version 内核版本
/proc/sys/kernel 
/proc/sys/net/ipv4
    cat /proc/sys/net/ipv4/tcp_tw_reuse ==> 1
    cat /proc/sys/net/ipv4/tcp_max_tw_buckets ==> 36000
    1、可以通过/etc/sysctl.conf进行修改,并用sysctl -p让它生效,效果是一样的
    2、也可以echo 1 >/proc/sys/net/ipv4/tcp_tw_reuse追加到/etc/rc.local结尾,这样每次开机都会生效
    3、修改文件本身也可以echo 1 >/proc/sys/net/ipv4/tcp_tw_reuse
/proc/cpuinfo 处理器信息
/proc/meminfo 系统内存信息
/proc/devices 当前运行内核所配置的所有设备清单 
/proc/dma 当前正在使用的DMA通道
/proc/filesystems 当前运行内核所配置的文件系统
/proc/interrupts 正在使用的中断,和曾经有多少个中断
/proc/ioports 当前正在使用的I/O端口
/prov/loadavg 系统负载平均值信息,uptime的结果,值不要超过CPU的盒数

 

posted @ 2018-09-25 11:03  dandyzhang  阅读(512)  评论(0编辑  收藏  举报