linux学习笔记
一、文件目录
1.1 重要目录
/bin 所有二进制命令所在的目录
/etc 是二进制安装包配置文件默认路径,
/etc/resolv.conf 配置本地客户端DNS文件
/etc/inittab 设置系统启动级别
/etc/profile 设置全局环境变量配置文件
/etc/rc.local 存储开机启动程序的命令文件
/var 用于存放经常变动的文件的目录,比如系统的启动日志,安全记录等
/var/log/dmesg 系统核心启动日志文件
/var/log/messages 系统报错日志文件
/var/log/wtmp 系统用户登陆信息的文件
1.2 DNS配置文件
[root@#localhost ~]# vi /etc/resolv.conf
nameserver 202.100.64.68
1.3 全局变量配置文件
/etc/profile
1.4用户相关的配置文件
[root@#localhost ~]# vi /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shu
用户密码配置文件
[root@#localhost ~]# cat /etc/shadow root:$6$ywXOZb7yZ1Pw8zz5$C944t9YjPW2y5sBvrnufaNqAzu4AYhgFHFOn.BrqWLokZiCVQoUJ0UvXkKO1B0FIBCSBc.9El.Te/elRMcAmc1::0:99999:7::: bin:*:17834:0:99999:7::: daemon:*:17834:0:99999:7::: adm:*:17834:0:99999:7::: lp:*:17834:0:99999:7::: sync:*:17834:0:99999:7::: shutdown:*:17834:0:99999:7:::
1.5.ps 命令
[root@#localhost ~]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 18:18 ? 00:00:06 /usr/lib/systemd/systemd --switched-root --system --d root 2 0 0 18:18 ? 00:00:00 [kthreadd] root 4 2 0 18:18 ? 00:00:00 [kworker/0:0H] root 5 2 0 18:18 ? 00:00:00 [kworker/u256:0] root 6 2 0 18:18 ? 00:00:00 [ksoftirqd/0] root 7 2 0 18:18 ? 00:00:00 [migration/0] root 8 2 0 18:18 ? 00:00:00 [rcu_bh]
UID : 使用此进程的用户ID PID: 该进程的进程ID PPID:该进程的父进程ID C:运行此进程CPU占用率 TTY:开启此进程的终端 TIME: 此进程运行的总使劲啊 CMD;正在执行的命令
1.6.find命令
[root@#localhost ~]# find /root -name "t*" -type f
[root@#localhost ~]# find /root -name "t*" -type d
查找10天以前的日志并且删除:
[root@localhost ~]# find /var/log/ -name "*.log" -mtime +10 | xargs rm -rf
第二种解决办法exec:
[root@localhost ~]# find /var/log -name "*.log" -mtime +10 -exec rm -rf {} \;
1.7. tar命令用户对文件或者目录里创建归档
[root@#localhost ~]# tar -cf test.tar test2 test4
打成tar包然后用gzip方式压缩
[root@#localhost ~]# tar -zcf test.tar.gz test2 test4
1.8.zip命令
[root@#localhost ~]# zip test.zip test2 test4
unzip命令
[root@#localhost ~]# unzip test.zip -d /opt Archive: test.zip creating: /opt/test2/ creating: /opt/test4/
1.9.du命令用于显示文件或者目录磁盘占用情况
[root@#localhost ~]# du -sh test4 0 test4
1.10.wget命令
从网络上下载指定的软件
[root@#localhost ~]# wget http://mirrors.aliyun.com/repo/Centos-7.repo
1.11 curl命令
Curl命令在linux操作系统中经常来测试网络和url的联通性,模拟正常的网络访问,当然除了这个作用之外,作为y一款强大的工具,curl还支持包括HTTP、HTTPS、ftp等众多协议,还支持POST、cookies、认证、从指定偏移处下载部分文件等功能。
[root@#localhost ~]# curl -I http://www.baidu.com HTTP/1.1 200 OK Accept-Ranges: bytes Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform Connection: keep-alive Content-Length: 277 Content-Type: text/html Date: Fri, 08 Jul 2022 13:37:19 GMT Etag: "575e1f72-115" Last-Modified: Mon, 13 Jun 2016 02:50:26 GMT Pragma: no-cache Server: bfe/1.0.8.18
1.12top命令
[root@#localhost ~]# top top - 06:51:32 up 6 min, 1 user, load average: 0.02, 0.40, 0.30 Tasks: 183 total, 1 running, 182 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 2.6 sy, 0.0 ni, 97.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 1863104 total, 1051580 free, 437708 used, 373816 buff/cache KiB Swap: 4194300 total, 4194300 free, 0 used. 1265356 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2409 root 20 0 162020 2224 1528 R 4.8 0.1 0:00.06 top 1 root 20 0 193784 6940 4196 S 0.0 0.4 0:06.33 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:00.03 kthreadd 4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H 6 root 20 0 0 0 0 S 0.0 0.0 0:00.58 ksoftirqd/0 7 root rt 0 0 0 0 S 0.0 0.0 0:00.27 migration/0 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh 9 root 20 0 0 0 0 S 0.0 0.0 0:04.80 rcu_sched 10 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 lru-add-drain
top - 06:51:32 当前系统时间 up 6 min, 系统已经运行时间 1 user 当前一个用户登陆 load average: 0.02, 0.40, 0.30 系统1分钟,5分钟,15分钟的平均负载
Tasks: 183 total 总进程数量 1 running 正在运行的进程数量 182 sleeping 休眠的进程数量 0 stopped 停止的进程数量 0 zombie 冻结的进程数量
%Cpu(s): 0.0 us 用户占用CPU的百分比 2.6 sy 内核占用CPU的百分比
1.13iostat命令
iostat用于监控系统输入/输出设备和CPU的使用情况。
[root@#localhost ~]# iostat -c Linux 3.10.0-1062.el7.x86_64 (#localhost.localdomain) 2022年07月09日 _x86_64_ (2 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 1.52 0.00 2.68 1.04 0.00 94.76 [root@#localhost ~]# iostat -x /dev/sda1 Linux 3.10.0-1062.el7.x86_64 (#localhost.localdomain) 2022年07月09日 _x86_64_ (2 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 1.50 0.00 2.65 1.02 0.00 94.82 Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util sda1 0.00 0.00 0.94 0.88 22.49 1.75 26.68 0.01 2.94 5.39 0.31 2.01 0.37
1.14 ifstat 命令
用于检测网络接口
[root@#localhost ~]# ifstat #kernel Interface RX Pkts/Rate TX Pkts/Rate RX Data/Rate TX Data/Rate RX Errs/Drop TX Errs/Drop RX Over/Rate TX Coll/Rate lo 545 0 545 0 47920 0 47920 0 0 0 0 0 0 0 0 0 ens33 694 0 413 0 86081 0 52648 0 0 0 0 0 0 0 0 0 virbr0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1.15rpm 命令
rpm命令是RPM软件包的管理工具
[root@#localhost ~]# rpm -qa | grep tree tree-1.6.0-10.el7.x86_64 [root@#localhost ~]# ls anaconda-ks.cfg initial-setup-ks.cfg test4 test.tar.gz tree-1.8.0 tt 模板 图片 下载 桌面 Centos-7.repo test2 test.tar test.zip tree-1.8.0.tgz 公共 视频 文档 音乐 [root@#localhost ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm 获取http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm 警告:/var/tmp/rpm-tmp.o8EFxP: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:zabbix-release-3.4-2.el7 ################################# [100%]
二、文件系统
在linux系统中,所有的设备、目录统称为文件。
2.1普通文件
(1)纯文本文件:以ASCII码形式存储在计算机
[root@#localhost ~]# file tt
tt: ASCII text
(2)二进制文件:以文本的二进制形式存储在计算机中,自带命令就是其中一种
[root@#localhost ~]# file -L /usr/bin/ls /usr/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32,
BuildID[sha1]=aaf05615b6c91d3cbb076af81aeff531c5d7dfd9,
stripped
(3)数据文件:有些程序在运行过程中会读取某些特定格式的文件,这写特定格式的文件被称为数据文件。
[root@#localhost ~]# file /var/log/wtmp /var/log/wtmp: data
2.2目录文件
目录文件就是linux中的目录,以d开头的就是目录文件
[root@#localhost ~]# ll 总用量 88 -rw-------. 1 root root 1876 3月 23 04:29 anaconda-ks.cfg -rw-r--r--. 1 root root 2523 12月 26 2020 Centos-7.repo -rw-r--r--. 1 root root 1924 3月 23 04:51 initial-setup-ks.cfg drwxr-xr-x. 5 root root 45 7月 8 18:56 test2 drwxr-xr-x. 2 root root 6 7月 8 18:57 test4
2.3块设备文件
块设备文件是一些存储数据以供系统访问的接口设备,如磁盘,软盘等;
2.4字符设备文件
字符设备文件是一些串行设备的接口设备,如键盘,鼠标等;
2.5套字节文件
数据接口文件,通常用于网络上数据的连接;
2.6管道文件
管道是一种特殊的文件类型,主要用来解决多个程序同事访问一个文件早成的错误问题;
2.7连接文件
硬连接 ln 源文件名 链接文件名
软连接 ln -s 源文件名 链接文件名
三、用户权限
3.1文件属性
[root@#localhost ~]# ls -li 总用量 88 67145315 -rw-------. 1 root root 1876 3月 23 04:29 anaconda-ks.cfg 68622330 -rw-r--r--. 1 root root 2523 12月 26 2020 Centos-7.repo 67364675 -rw-r--r--. 1 root root 1924 3月 23 04:51 initial-setup-ks.cfg 67872585 drwxr-xr-x. 5 root root 45 7月 8 18:56 test2 33569128 drwxr-xr-x. 2 root root 6 7月 8 18:57 test4 68622326 -rw-r--r--. 1 root root 10240 7月 8 21:00 test.tar
67145315 : inode索引节点号 -文件类型 “-”表示普通文件 d 表示目录文件 b 表示块设备文件 c 表示字符设备文件 s 表示套子节文件 p 代表管道文件 l 表示连接文件
rw------- 表示权限 1 表示硬连接数量 root 表示所有者 root 表示所属组 1876表示文件大小
3.2用户分类
(1)超级管理员用户,超级管理员的UID和GID都是0
[root@#localhost ~]# head -1 /etc/passwd root:x:0:0:root:/root:/bin/bash
(2)系统用户,安装操作系统默认存在的,且不可登陆的用户,他们的UID和GID范围都是1~499
[root@#localhost ~]# head -5 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
(3)普通用户,由管理员创建的用户,UID和GID的范围是500~65535
[root@#localhost ~]# tail -1 /etc/passwd mysql:x:1001:1001::/home/mysql:/sbin/nologin
四、磁盘管理
RAID2.0技术:
RAID2.0技术将硬盘域中的硬盘空间切分成固定大小的物理空间-CK(Chunk64M),实现底层虚拟化,不同硬盘的多个CK组成存储池,相同类型的CK按照RAID策略组成(CKG),CKG将再次切分成更小粒度的逻辑空间Extent(大小512KB-64MB,默认4MB),Extent是存储池中申请空间、释放空间、数据统计和数据迁移的基本单位。再由这些逻辑空间Extent组成LUN(逻辑存储单元)。(Extent再划分为更小粒度的Gain,Thin LUN以Grain为单位映射到LUN),最终LUN映射给主机进行读写。
RAID2.0+与传统RAID优势
快速重构:由于热备空间也是按照Chunk分散在多个盘中,因此多个CKG的重构几乎是同时进行,避免了传统RAID发生故障时,写单个热备盘造成的性能瓶颈,大大减少了重构的时间。
硬盘负载均衡:LUN的数据被均匀的分布到阵列内所有的硬盘上,可以防止局部硬盘过热,提升可靠性,在参与业务读写过程中,阵列内硬盘参与度高,提升系统响应速度。
最大化硬盘资源利用率:性能上LUN基于资源池创建,不再受限于RAID组硬盘数量,LUN的随机读写性能可得到大大提升,容量上资源池中的硬盘数量不受RAID级别影响,免除传统RAID环境下有些RAID组空间利用率高有的低的状况,并借助智能精简配置,提升硬盘的容量利用率。
提升存储管理效率:基于RAID2.0技术,我们无需花费过多时间做存储预规划,只需简单讲硬盘组成存储池设置分层策略,从存储池划分LUN即可,后续扩容硬盘或者LUN时,系统会自动从存储池划分所需的空间并自动调整LUN数据分布,使得LUN数据更加均衡的到所有硬盘中。
五、正则表达式
5.1grep命令,grep是一个强大的文本搜索工具,与正则表达式配合,将匹配到的行输出到屏幕上。
[root@#localhost ~]# cat /etc/passwd | grep root root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@#localhost ~]# cat /etc/passwd | grep -c root 2
5.2 sed命令
查看第三行的内容:
[root@#localhost ~]# sed -n '3p' /etc/passwd daemon:x:2:2:daemon:/sbin:/sbin/nologin
查看三到六行的内容:
[root@#localhost ~]# sed -n '3,6p' /etc/passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
5.3 awk命令
awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。
[root@#localhost ~]# cat /etc/passwd |awk -F ':' '{print $1}'
其他命令学习:
tail 从尾部查看;
head: 从头部开始查看
wc : 查看文件有多少行;
六、其他命令
stat命令 查看文件的详细信息
[root@localhost network-scripts]# stat ifcfg-ens33 File: ‘ifcfg-ens33’ Size: 376 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768d Inode: 51077436 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: system_u:object_r:net_conf_t:s0 Access: 2023-06-14 03:53:17.008975847 -0400 Modify: 2023-06-14 03:53:17.008975847 -0400 Change: 2023-06-14 03:53:17.012976043 -0400 Birth: -
创建文件并设置其创建时间
[root@localhost ~]# touch -m -d "2020-03-30 00:00" a.txt