Linux基础(笔记)

一、程序和进程

程序是存放在电脑上的文件,存储于磁盘里。

进程是正在运行的程序,存储于内存里。

守护进程为了处理一项任务或者提供服务,而持续运行的程序。

二、buffer和 cache的区别

buffer:

写入数据到内存里,这个数据的内存空间称为缓冲区(buffer),写入到内存buffer缓冲区,写缓冲

cache:

从内存读取数据,这个存数据的内存空间称为缓存区(cache),从内存cache读取缓存区,读缓存

三、存储优化方案

90%的人访问20%的数据(热数据),10%人的访问剩下的数据。

ssd(80G):存20%热数据,根据用户访问记录,动态调整20%的数据。

stat(500G):存80%的数据。

找到cpu、内存、硬盘、网卡IO瓶颈优化。

顺序存储比随机存储使用效率更高,消息队列把随机的变为顺序。

四、修改CentOS7的网卡名

进入"Install CentOS 7"选项时,先按tab键,然后在结尾加上空格,加上下面的参数后回车即可。

网卡名如:eth0
net.ifnames=0 biosdevname=0

网卡名如:enp5s2
net.ifnames=1 biosdevname=0

网卡名如:em1
net.ifnames=0 biosdevname=1

 五、CentOS7图像化安装

1、选择安装的语言:English

2、LOCALIZATION

        DATE&TIME:shanghai

        KEYBOARD:English(US)

        LANGUAGE SUPPORT:English(US)

3、SOFTWARE

        INSTALLATION SOURCE:默认

        SOFTWARE SELECTION:也可通过yum group install "包组"后补安装

 4、SYSTEM

        KDUMP:系统出错时打包当时的系统内核信息(按需打开)

        SECURITY POLICY:默认

        NETWORL & HOSTNAME

         INSTALLATION DESTINATION

LVM:逻辑卷管理,动态分配主机磁盘大小的工具。LVM从功能上讲很好,但是它是软件实现的动态调整。硬盘的上面增加了一层软件管理功能,问题在于高并发的时候会导致性能下降了5-10%。

先添加/boot分区,用来存放内核相关程序 ,它是可选的,CentOS5\6给200M,CentOS7给256M(工作中1-2G),永远没有需求调整,不需要LVM。

swap分区:虚拟分区,作用内存不够的时候,系统就会把swap当做内存用。防止因为内存不够死机。物理内存在8G以上,一般就给8G。物理内存小于8G,就按1.5倍内存来给。内存不够,swap分区来凑。没有需求调整,有调整也不用LVM。

/根分区 Linux系统必须要有的,相当于Windows的C盘,系统程序相关的。

 5、开始安装,安装过程可设置root密码和创建新用户

 六、yum安装

软件安装方法
 
1、编译安装,./config --> make --> make install
 
2、rpm安装,对编译安装的封包,需要解决依赖包问题
 
3、yum安装,即rpm包管理器,自动解决依赖包问题
更改yum源,官方的软件安装源
curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
 
epel源,第三方的软件安装源
curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
 
curl -s静默安装,不显示过程到屏幕上
curl -o将文件保存为命令行中指定的文件名的文件中
把系统软件更新到最新
yum update -y

查看命令对应的软件包
yum provides 命令(如locate)
 
查看id为2的详细安装包
yum history info 2
 
安装本地rpm包,同时上网自动下载安装依赖包
yum localinstall package
  
卸载,依赖包会一起卸载(少用)
yum remove 软件包 -y
 
清除包文件
yum clean all
#CentOS6和CentOS7都要安装的企业运维常用基础工具包
yum install tree nmap dos2unix lrzsz nc lsof wget tcpdump htop iftop iotop sysstat nethogs -y
 
上传:rz
下载:sz 文件名
 
#CentOS7要安装的企业运维常用基础工具包
yum install psmisc net-tools bash-completion vim-enhanced -y
查看软件包组
yum groups mark convert 
 
查看所有包组名称,包括已安装的和未安装
yum grouplist
 
安装软件包组
yum groupinstall "Development Tools" -y
yum groupinstall "Cinnamon" -y

删除某个组件的全部软件包
yum groupremove group
列出所有已安装的软件包
rpm -qa tree
 
列出rpm包的文件内容
rpm -ql tree
 
查看文件所属的安装包
rpm -qf /etc/httpd/conf.d/zabbix.conf
 
安装软件(安装的同时更新到最新版安装包)
rpm -ivh(-uvh)
 
只卸载软件,不卸载依赖包
rpm -e --nodeps
 
重新安装某个已经安装过的软件!如果妳要安装一堆RPM软件档案时,
可以使用 rpm -ivh *.rpm ,但若某些软件已经安装过了, 此时
系统会出现『某软件已安装』的信息,导致无法继续安装。
此时可使用这个选项来重复安装喔!
rpm --replacepkgs

七、SSH工具远程连接Linux主机故障排查

1、检查服务端

网卡、IP是不是启动和正确的
ip add或者ifconfig
查端口:ss -lntup|grep ssh(ss效率比netstat高)
查进程:ps -ef|grep ssh
如果进程不在systemctl start sshd 启动后再检查

2、检查客户端

ping 10.0.0.200
如果物理连接不可达:
    1、连接的IP输入错了
    2、物理链路有问题(VM环境调虚拟网络编辑器)
telnet 10.0.0.200 22
如果不可连接:
    1、服务端没有提供服务
    2、防火墙(6是iptables,7是firewalld)
       查看状态:systemctl status firewalld.service 
       关闭:systemctl stop firewalld.service
       启动:systemctl start firewalld.service
       开机不自动启动:systemctl disable firewalld.service

八、快捷键

Ctrl+a	光标回到命令行首
Ctrl+e	光标回到命令行尾
Ctrl+k	复制剪切(删除)光标处到行尾的字符
Ctrl+u	复制剪切(删除)光标处到行首的字符
Ctrl+y	粘贴Ctrl+u,Ctrl+k,Ctrl+w删除的文本
Ctrl+r	搜索命令行使用过的历史命令记录
Ctrl+l	清除屏幕所有内容,等同clear命令
Ctrl+s	锁定终端,使之无法输入内容
Ctrl+q	解锁执行Ctrl+s的锁定状态
Ctrl+z	暂停执行在终端运行的任务
!!	执行上一条命令
!pw	执行最近以pw开头的命令

九、常用命令

1、ls

[root@template ~]# ls -lhi
total 4.0K
33574979 -rw-------. 1 root root 1.4K Feb  2 00:30 anaconda-ks.cfg

第二列最后一个字符点号“.”是和selinux有关的一个标识

显示目录
[root@template ~]# ls -d
.
[root@template ~]# ls -ld test
drwxr-xr-x. 2 root root 6 Feb 12 23:18 test

目录结尾加斜线,斜线结尾(区分文件和目录)
[root@template ~]# ls -lp
total 4
-rw-------. 1 root root 1338 Feb  2 00:30 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Feb 12 23:18 test/

[root@template ~]# ls --time-style=long-iso -l
total 4
drwxr-xr-x. 2 root root    6 2022-02-12 23:18 test

少显示一个年份
[root@template ~]# ls --time-style=iso -l
total 4
drwxr-xr-x. 2 root root    6 02-12 23:18 test

2、cp

cp test.txt{,.bak}备份

cp -p 复制文件时保持源文件的所有者、权限信息以及时间属性
cp -d 如果复制的源文件是软连接,仅复制软连接本身,其保留软连接所指向的目标文件或目录
cp -r 递归复制目录
cp -a 等于-pdr

3、alias

查看当前的所有别名
alias

取消别名
unalias rm 

临时设置
alias rm='rm -i'
 
永久/etc/profile插入以上语句
source /etc/profile生效
 
\rm不使用别名、或者找到指令的全路径

[root@template ~]# cp test{1..3}.txt ./test
[root@template ~]# cp test{1..3}.txt ./test
cp: overwrite ‘./test/test1.txt’? y
cp: overwrite ‘./test/test2.txt’? y
cp: overwrite ‘./test/test3.txt’? y
[root@template ~]# \cp test{1..3}.txt ./test

4、echo

echo 输出和打印
-n 不换行输出
-e 支持转义字符

[root@template ~]# echo -n "test"
test[root@template ~]# echo -e "test\ntest"
test
test

[root@template ~]# echo `date`
Thu Feb 10 22:49:04 CST 2022
[root@template ~]# echo $(date)
Thu Feb 10 22:49:19 CST 2022
[root@template ~]# echo '`date`'
`date`
[root@template ~]# echo '$(date)'
$(date)
 
[root@template ~]# a=tom
[root@template ~]# echo $a
tom
[root@template ~]# echo "$ais a man"
 a man
[root@template ~]# echo "{$a}is a man"
{tom}is a man
[root@template ~]# echo "${a}is a man"
tomis a man

5、hostname

/etc/hostname           C7主机名配置文件
/etc/sysconfig/network  C6主机名配置文件
 
永久生效:
[root@template ~]# hostnamectl set-hostname template
[root@template ~]# cat /etc/hostname
template
 
临时改一下:
[root@template ~]# hostname test
[root@template ~]# cat /etc/hostname
template
 
返回本机ip
hostname -I
hostname -i

c6、c7也可通过修改配置文件,永久生效主机名

6、vi、vim

:set nu	    显示行号
:set nonu   关闭显示

:set invlist    显示不可见字符
:set nolist     关闭显示

多行复制           nyy
粘贴下一行         p(小写)
粘贴上一行         P(大写)
行尾插入           A
光标下一个字符插入   a 

7、tr 

从STDIN读取数据
 
将test文件中的w替换成9
tr "w" "9" < test.txt
 
批量替换
tr "a-d" "0-3" < test.txt
a替换0,b替换1,c替换2,d替换3

8、grep

grep -n 显示行号的过滤
grep -w 按单词为单位过滤
grep -E == egrep 扩展的正则过滤
grep -c 返回匹配的个数
 
#常用正则匹配时过滤
#只显示过滤的内容aaa
grep -o "aaa" test.txt 
 
grep 10 -C 5 test.txt
先找到10,再找到前后各5行

 9、重定向

#对和错的信息都存入文件
echo hello >> /data/test.txt 2>&1 
 
echo hello &> /data/test.txt
 
echo hello &>> /data/test.txt

cat >> /data/test.txt<<EOF
I
am
studying
linux
EOF        表示结束符

less +n test.txt  从第n行开始显示文本内容

[root@template ~]# seq 3 > test.txt
[root@template ~]# cat test.txt
1
2
3

10、file

纯文本文件
[root@template ~]# file test1.txt 
test1.txt: ASCII text

数据文件
[root@template ~]# file /var/log/lastlog 
/var/log/lastlog: data

二进制文件
[root@template ~]# file /bin/cat
/bin/cat: ELF 64-bit LSB executable......

11、ln


软链接的inode节点号不同
软链接只有inode,没有block,软链接文件名指向源文件

软链接的作用

第一个用途:
安装软件:/application/nginx-1.10===>/application/nginx(让开发用)
过半年:  /application/nginx-1.20===>/application/nginx(让开发用)
#/application/nginx目录不要提前建立,创建软连接的时候自动创建
[root@template /application]# ls -ltr
total 0
drwxr-xr-x. 2 root root 6 Feb 14 23:03 nginx-1.1
[root@template /application]# ln -s /application/nginx-1.1/ /application/nginx
[root@template /application]# ls -ltr
total 0
drwxr-xr-x. 2 root root  6 Feb 14 23:03 nginx-1.1
lrwxrwxrwx. 1 root root 23 Feb 14 23:04 nginx -> /application/nginx-1.1/

第二个用途:
/etc/对应的分区要满了,没法放很多文件,但是程序还想通过/etc/目录访问文件。
此时,我们可以把文件放入/opt/test下,然后做一个到/etc/test的软链接。
root@template ~]# mkdir -p /opt/test
[root@template ~]# touch /opt/test/{1..3}.txt
[root@template ~]# ln -s /opt/test/ /etc/test
[root@template ~]# ls -ltr  /etc/test
lrwxrwxrwx. 1 root root 10 Feb 14 23:10 /etc/test -> /opt/test/
[root@template ~]# ls /etc/test
1.txt  2.txt  3.txt

具有相同inode节点号的文件互为硬链接(目录不能创建硬链接)
硬链接的作用备份,防止误删

12、find

find / -type f -name "ifcfg-eth0"			  
 
find / -type f -o -name "*txt"	
-a and 并且(默认不用写)
-o or  或者
!      取反
 
find /data -type f|xargs rm -f
find /data -type f -exec rm -f {} \;
 
find / -tpye d -mtime +3 3天前
find / -tpye d -mtime 3  之前第3天
find / -tpye d -mtime -3 最近3天

13、xargs从标准输入执行命令

[root@template ~]# echo {1..3} > test.txt
[root@template ~]# cat test.txt
1 2 3
[root@template ~]# xargs -n2 <test.txt
1 2
3

-d 指定分隔符,不指定默认是空格
[root@template ~]# xargs -d"." -n2 <test.txt
a b
c d

[root@template ~]#

[root@template ~]# xargs -n2 <test.txt
a.b.c.d
[root@template ~]#
[root@template ~/test]# touch test{1..3}.txt
[root@template ~/test]# ls
test1.txt  test2.txt  test3.txt

-i 选项告诉 xargs 用每项的名称替换 {}
-t 选项指示 xargs 先打印命令,然后再执行
[root@template ~/test]# ls | xargs -t -i mv {} {}.bak
mv test1.txt test1.txt.bak 
mv test2.txt test2.txt.bak 
mv test3.txt test3.txt.bak 
[root@template ~/test]# ls
test1.txt.bak  test2.txt.bak  test3.txt.bak

批量改回文件名
[root@template ~/test]# ls |xargs -n1|sed -rn 's#(.*txt)(.bak)#mv \1\2 \1#gp'|bash
[root@template ~/test]# ls
test1.txt  test2.txt  test3.txt

批量压缩:
[root@template ~/test]# ls|xargs tar zcvf 1.tar.gz

下载文本里面的所有链接:
cat url-list.txt | xargs wget –c

 14、date

修改系统时间
date -s "20221012 11:11:11"
date -s "1hour"
date -s "-1hour"
clock -w保存到BIOS中
查看时间
[root@template ~]# date +%F -d "-1day"
2022-02-13
[root@template ~]# date +%F -d "+1day"
2022-02-15
[root@template ~]# date +%F -d "+1month"
2022-03-14
[root@template ~]# date +%F -d "+1Y"
date: invalid date ‘+1Y’
[root@template ~]# date +%F -d "+1year"
2023-02-14

[root@template ~]# date +"%Y-%m-%d %H:%M:%S"
2022-02-14 23:36:21
[root@template ~]# date +"%F %T"
2022-02-14 23:36:30

15、tar

--exclude 排除打包:
tar zcvf 1.tar.gz ./* --exclude={4,5}.txt
tar zcvf 1.tar.gz ./* --exclude={1..4}.txt

参数X,通过文件排除
[root@template ~/test]# cat paichu.txt
1.txt
2.txt
3.txt
[root@template ~/test]# tar zcvfX 1.tar.gz paichu.txt ./*
./1.tar.gz
./4.txt
./5.txt
./paichu.txt

date用作目录变量时,不能以空格分隔和冒号分隔
tar zcvf $(date +"%F-%H-%M-%S").tar.gz ./*
 
tar zcvfh 打包软连接文件

-C解压到指定目录

16、stat

[root@template ~]# stat -f /etc/hosts
  File: "/etc/hosts"
    ID: 80300000000 Namelen: 255     Type: xfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 4977920    Free: 4448413    Available: 4448413
Inodes: Total: 9960960    Free: 9887596
[root@template ~]# stat  /etc/hosts
  File: ‘/etc/hosts’
  Size: 158       	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 16778307    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:net_conf_t:s0
Access: 2022-02-14 22:48:14.817999579 +0800
Modify: 2013-06-07 22:31:32.000000000 +0800
Change: 2022-02-02 00:27:08.413355465 +0800
 Birth: -

17、selinux

getenforce  #查看selinux状态
      enforcing   #selinux正在运行
      permissive  #selinux临时关闭
      disable     #selinux彻底关闭
 
setenforce  #设置selinux状态 
setenforce 0(permissive)   #临时关闭selinux 
 
永久关闭selinux
修改文件/etc/selinux/config中数据SELINUX=disable

18、防火墙

查看防火墙状态
systemctl status firewalld.service
 
开启防火墙
systemctl start firewalld.service
 
开机自启动防火墙
systemctl enable firewalld.service

19、通配符

?匹配单个字符,有且只有一个
 
?在正则中表示0或1个字符
 
双引号能解析特殊字符,单引号所见即所得
 
``和$()一样,表示优先执行
 
{}
1、生成序列,echo {1..10}
2、${a}11,引用变量a

20、cut

截取以空格分隔的1到5列的数据
cut -d" " -f1-5 test.txt
 
截取以冒号分隔的1,3,5列的数据
cut -d":" -f1,3,5 test.txt
 
对文件的每一行,从第3个字符取到第5个字符
cut -c3-5 test.txt
 
对文件的每一行,从第1个字符取到第6个字符
及第8个字符到最后列字符的值
cut -c1-6,8- test.txt

20、sed

sed 
-n 取消命令的默认输出
-i 直接修改文件内容,不输出到终端
-e 多条件执行
-r(让sed支持扩展正则)
 
打印2到3行的数据
sed -n '2,3p' test.txt
 
打印包含test字符的那行数据
sed -n '/test/p' test.txt
 
删除包含test字符的那行数据
sed '/test/d' test.txt
 
替换test字符为111,不修改文件
sed 's/test/111/g' test.txt
 
替换test字符为111,修改文件
sed -i 's/test/111/g' test.txt
 
替换a为b,c为d,不修改文件
sed -e 's/a/b/g' -e 's/c/d/g' test.txt
 
在第二行后追加内容,内容直接属于第三行,修改文件
sed -i '2a I teach linuc' test.txt
 
最后一行追加,从下一行开始输入,必须用单引号
sed -i '$a I teach \nlinuc' test.txt
 
在第二行前插入内容,内容直接属于第二行,修改文件
sed -i '2i I teach linuc' test.txt
 
\n可插入多行
sed -i '2i I teach li\nnu\nc' test.txt
 
删除5到8行
sed -i '5,8d' test.txt
 
插入行行首空一个tab
sed -i "5i\ \tThis is a tab. " test.txt
去除ifconfig指令结果中的IP
 
方法1:
ifconfig eth0|awk 'NR==2{print $2}'
 
方法2:
ifconfig eth0|sed -nr '2s/^.*inet (.*) netmask.*$/\1/gp'
-r(让sed支持扩展正则)

21、awk

以冒号分隔,取整行,最后一列及最后第二列的数据
awk -F ":" '{print $0,$NF,$(NF-1)}' test.txt
 
取第2、3行的内容
awk 'NR>1&&NR<4' test.txt
 
过滤出含有root字符串的行
awk '/root/' test.txt
 
获取非r字符开头的值
awk '/^[^r]/' test.txt
 
取文件的第一列、第三列和最后一列内容,并打印行号
awk -F ":" '{print NR,$1,$3,$NF}' test.txt
 
过滤文件中第一列内容匹配root的字符串,把符合的行的最后一列输出
awk -F ":" '$1~/root/ {print $NF}' test.txt 
~匹配
 
过滤下列t.txt文件中第三列内容分数大于79的人名和性别
cat > t.txt << EOF
张三 男 80
李四 女 90
王五 男 70
找刘 女 70
 
awk '$3>70 {print$1,$2}' t.txt
过滤文件空行
 
方法1:grep -v '^$' test.txt
 
方法2:awk '!/^$/' test.txt
  
方法3:sed '/^$/d' test.txt
通过正则过滤文件前两行:
test
teessst
 
方法1:egrep 'te+s+t' test.txt
 
方法2:awk '/te+s+t/' test.txt
 
方法3:sed -nr '/te+s+t/p' test.txt
取/etc/hosts文件的权限
 
方法1:stat /etc/hosts|awk -F "[(/]" 'NR==4{print $2}'
 
方法2:stat /etc/hosts|sed -nr '4s/^.*\(([0-9]+).*$/\1/gp'
 
方法3:stat -c%a /etc/hosts
打印行号及内容

生成文件内容
echo stu{01..03}|xargs -n1 >test.txt
结果:
stu01
stu02
stu03

方法1:cat -n test.txt

方法2:vi test.txt 进入后:set nu显示行号

方法3:grep -n "." test.txt

方法4:awk '{print NR,$0}' test.txt

方法5:sed '=' test.txt|xargs -n2
xargs -n2,指定输出时每行输出的列数(2列)
sed '=',打印当前行行号

十、目录结构

[root@template ~]# tree -L 1 /
/
├── bin -> usr/bin    存放命令相关文件
├── boot              引导分区,系统内核及引导程序
├── dev               Device设备文件所在目录
├── etc               二进制软件包配置文件所在路径
├── home              普通用户的家目录
├── lib -> usr/lib    库文件目录
├── lib64 -> usr/lib64库文件目录
├── media             媒体
├── mnt               临时挂载点
├── opt               存放自定义的软件包
├── proc              虚拟的文件系统,存放进程和内核信息的目录
├── root              超级用户家目录
├── run               正在运行进程相关信息的文件
├── sbin -> usr/sbin  管理员命令所在目录,普通用户执行不了
├── srv               为用户提供服务的数据所在目录
├── sys               虚拟的文件系统,存放进程和内核信息的目录
├── tmp               临时文件所在目录
├── usr               系统软件和用户相关程序所在目录
└── var               可变化的数据

1、网卡配置文件

网卡eth0配置文件
/etc/sysconfig/network-scripts/ifcfg-eth0
 
DNS配置文件,存放DNS解析地址,已基本废弃
/etc/resolv.conf

只要重启网卡systemctl restart network.service
网卡配置文件里设置的DNS地址,就会自动更新到/ect/resolv.conf
 
本地的DNS解析文件
/etc/hosts

域名解析顺序:
浏览器DNS缓存==》本地DNS缓存==》/etc/hosts==》
网卡配置的DNS服务器==》根(.)域名服务器==》
一级域名服务器(如.com)==》二级域名服务器(如baidu.com)==》
在授权DNS服务器(即二级域名服务器)中找到A记录信息==》
A记录会被发回网卡配置的DNS服务器,被保存记录后告知请求的主机==》
本地DNS缓存A记录==》浏览器DNS缓存A记录

[root@template ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet                                上网类型,目前基本都是以太网
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none                               启动协议,获取配置方式,有none|bootp|dhcp三个选项
DEFROUTE=yes                                 使用默认路由   
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0                                    第一块网卡逻辑设备名,第二块为eth1
UUID=ffdf9e7c-4df3-4f3a-8fb2-ab1d1c9a7685    通用唯一识别码
DEVICE=eth0                                  第一块网卡逻辑设备名,第二块为eth1  
ONBOOT=yes                                   要为yes,才能保证下次开机启动激活网卡设备 
IPADDR=10.0.0.200                            分配的ip地址
PREFIX=24                                    子网掩码位数
GATEWAY=10.0.0.254                           网关地址 
DNS1=10.0.0.254                              主DNS
IPV6_PRIVACY=no

2、开机自动挂载文件

/ect/fstab
第一列设备名,第二列挂载点
第三列挂载的文件系统类型
第四列挂载的参数
第五列是否备份
第六列是否开机自动检查

存放开机自启动命令的文件
/etc/rc.local  (CentOS7需要加执行权限)
 
配置系统的环境变量/别名等的文件
/etc/profile及/etc/bashrc
生效条件二选一:
1、重启系统
2、source 文件
 
配置文件生效顺序
/etc/bashrc==》~/.bashrc==》
~/.bash_profile==》/etc/profile

linux内核参数配置文件
/etc/sysctl.conf
linux系统内核优化用到
sysctl -p 让修改后的文件生效

3、登录终端前显示信息的文件

/etc/issue和/etc/issue.net
显示系统版本,内核版本
为了防止泄露服务器版本,一般会把它们都清空

/etc/motd
可编辑,一般编辑welcome等登录提示信息

4、查看系统版本文件

[root@template ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[root@template ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

5、/usr目录

/usr/local:源码编译安装软件默认的位置路径

/usr/src:存放源码文件的目录

6、/var目录

/var/log/message系统默认日志信息
/var/log/secure系统用户的登录信息,可查看是否有黑客登录攻击
/var/log/dmesg记录硬件信息加载情况的日志文件,常用dmesg查看
/proc/meminfo内存信息,常用free -h查看
/proc/cpuinfocpu信息,常用lscpu查看
/proc/loadavg系统的负载(系统繁忙程度),常用uptime查看
/proc/mounts显示系统挂载信息,常用df -h查看
[root@test ~]# w
 22:24:57 up 21 min,  1 user,  load average: 0.00, 0.01, 0.02
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.1.5      22:07    1.00s  0.03s  0.01s w

[root@test ~]# uptime
 22:25:12 up 21 min,  1 user,  load average: 0.00, 0.01, 0.02
当前时间  开机多久  哪些用户   负载  平均     1分钟  5分钟  15分钟
负载的数字和CPU核数相当的时候就是负载一个临界点,例如 一颗两核 临界负载就是2

十一、运行级别

CentOS6系统运行级别:
#   0  关机(请不要把系统运行级别设置为0)
#   1  单用户模式 (root用户密码忘记,可用此模式找回)  
#   2  没有NFS,多用户模式
#   3  命令行模式 文本模式(企业级服务器核心运行状态)          
#   4  未使用
#   5  图形化模式 桌面模式  X11(桌面个人版系统运行状态)
#   6  重启(不要把系统运行级别设置为6)

查看运行级别
[root@template ~]# runlevel
N 3
上次的运行级别    当前的运行级别

关机
init 0

重启
init 6

启用命令航模室
init 3
CentOS7系统运行级别:
[root@template ~]# ls -l /usr/lib/systemd/system|grep level|grep -v want
lrwxrwxrwx. 1 root root   15 Feb  2 12:32 runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root   13 Feb  2 12:32 runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root   17 Feb  2 12:32 runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root   17 Feb  2 12:32 runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root   17 Feb  2 12:32 runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root   16 Feb  2 12:32 runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root   13 Feb  2 12:32 runlevel6.target -> reboot.target

查看当前运行级别
[root@template ~]# systemctl get-default
multi-user.target

修改运行级别
systemctl set-default multi-user.target

十二、CentOS7系统启动流程

1、开机BIOS自检                  --> 检查CPU,硬盘等硬件信息

2、读MBR引导                     --> 确定服务器的启动方式【一般是硬盘启动,有时候也有CD_ROM启动】

3、加载GRUB菜单                  --> 确定加载某个系统【GRUB是GUN项目的多操作系统启动程序】

4、加载内核kernal信息            --> 【cat /proc/version或uname -a即可查看内核版本信息】

5、运行systemd进程               --> Linux启动的第一个进程【ps -ef|grep systemd】

6、读取/usr/lib/systemd/system/initrd.target,包含挂载文件系统等操作

7、读取/etc/systemd/system/default.target,设置运行级别并加载对应级别下的脚本

7、读取/usr/lib/systemd/system/sysinit.target,初始化系统

8、执行/etc/rc.d/rc.local,用户开机自启动程序

9、读取/usr/lib/systemd/system/getty.target,启动mingetty(终端登录进程),进入系统登陆界面

十三、用户属主属组

1、虚拟用户

文件存在必须属于对应用户

进程运行必须应用用户

实际存在的用户,但是又不允许它登录

满足文件或进程运行属主的要求。但是又不会带来管理风险

UID 1-499

[root@template ~]# cat /etc/passwd|grep nologin
bin:x:1:1:bin:/bin:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin

2、用户的增删改查

useradd
-u 指定uid
-s 指定登录的shell解释器
-M 不创建家目录
-g 指定所属的组
-c 添加用户说明
-d 指定家目录
-e 设定登录截止日期
 
useradd check -u 1000 -s /sbin/nologin -M
 
给开发等人员添加用户,尽量给截止时间
useradd check -c "just for test" -d /tmp -e '2020/10/20'
chage -l check 查看-e指定的日期
给用户testu添加组testg

方法1:
groupadd testg
useradd testu
usermod -g testg testu
id testu查看结果

方法2:
前提是没有testu用户
若有则只能用上面那种方法
useradd testu -g testg
/etc/default/useradd文件 useradd命令的配置文件
默认shell就是/bin/bash
为什么默认的家目录在home下
为什么默认的家目录/home/用户名下面有很多隐藏文件,从哪来的
就是/etc/default/useradd文件配置的
 
[root@]# cat /etc/default/useradd 
# useradd defaults file
GROUP=100
HOME=/home   指定用户家目录
INACTIVE=-1  是否启用用户过期停权,-1不启用
EXPIRE=      用户终止日期,不设置表示不启用
SHELL=/bin/bash  新用户默认所用的shell解释器
SKEL=/etc/skel   配置新用户家目录的默认环境变量文件存放路径。
当我们用useradd添加用户时,用户家目录下的隐藏环境变量文件,
都是从这复制过去的
CREATE_MAIL_SPOOL=yes 创建mail文件
 
useradd -D:表示修改/etc/default/useradd文件
            相当于vim编辑/etc/default/useradd文件
[root@]# useradd -D -s /bin/sh -e "2020/10/21"
[root@# cat /etc/default/useradd 
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=2020/10/21
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes


登录系统后,命令行出现如下提示:为什么?
[root@]# su - chenglong1
-sh-4.2$ 
-sh-4.2$ 
解答:用户的环境变量缺失导致的。
执行如下命令解决
-sh-4.2$ cp /etc/skel/.bash* .
 
如果环境变量还是不生效是因为useradd -s指定非/bin/bash导致的
每次都要手动执行以下语句,除非修改用户的shell解释器
-sh-4.2$ source ./.bash_profile ./.bashrc 
 
添加用户要用的文件 
/etc/login.defs(一些默认属性如umask uid)
/etc/skel /etc/default/useradd
用户和用户的家目录一起删除
userdel -r check
 
usermod参数类似useradd
usermod -u 1000 -s /bin/sh -M -g root
 
-d:单独使用时,只是把保存在/etc/passwd这个配置文件当中的源目录名改成指定的新目录名,
并不会把源目录下的内容移动到新目录下,如果要把源目录下的内容移动新目录下,
则要和-m选项一起使用,才会把源目录下的内容移动到新目录下
usermod -d /home/test -m  check
 
不用交互直接设置密码
echo 123456|passwd --stdin check
passwd --stdin check <pass.txt
 
chpasswd批量更改密码
批量改最后5个用户的密码为test
tail -5 /etc/passwd|awk -F":" '{print $1":test"}'|chpasswd
 
批量创建10个用户,test01-10,请设置和用户名相同的密码。
echo test{01..10}|xargs -n1|sed -rn 's#(.*)#useradd \1;echo 123456|passwd --stdin \1 #gp'|bash
只添加组
groupadd root
 
添加组id
groupadd -g gid
 
删除组
groupdel root
 
chage 查看和修改密码属性
-l 用户:显示用户的密码信息
-E:账号过期日期        
 
[root@~]# chage -l check
Last password change	: Oct 07, 2020 #最后一次密码变化时间
Password expires		: never #密码过期时间
Password inactive		: never #密码停权时间
Account expires			: never #账号过期时间
Minimum number of days between password change		: 0
Maximum number of days between password change		: 99999
Number of days of warning before password expires	: 7
 
[root@oldboyedu ~]# chage -E "2020/10/1" check
[root@oldboyedu ~]# chage -l check
Last password change					: Oct 07, 2020
Password expires					: never
Password inactive					: never
Account expires						: Oct 01, 2020
Minimum number of days between password change		: 0
Maximum number of days between password change		: 99999
Number of days of warning before password expires	: 7
查看用户登陆情况

方法1:w  显示系统负载信息,及用户登陆信息
 
方法2:last  显示用户登陆信息
 
方法3:lastlog 显示系统中所有用户最近一次的登陆信息

十四、磁盘空间不足

1、block满了

依次找出最大文件或目录
du -sh /*|grep G

2、inode满了

df -h    查看,发现block没满

df -i    查看inode是否满了

3、block文件没有被彻底删除

硬链接数为0,但被进程调用数不为0

lsof|grep delete
找到这个进程,然后重启该进程

 十五、删除原理

cp      文件名、inode、block都改变了

move    只是改变了文件名,指向的inode、block都不变

cp要重新生成inode和block,占用了IO,所以move快
rm原理:

1、静态文件(没有进程或程序正在访问的文件):所有硬链接为0

2、动态文件(有程序或进程访问的文件):所有硬链接为0,i_count(进程调用文件的数量)为0

进程在用文件时,就算使用了rm,还是没有删除,需要停进程。
磁盘清理原理:

      a.系统定时清理没有文件名的inode

      b.磁盘检查会清理没有文件名的inode

      c.增加新文件时优先占用没有文件名的inode

十六、权限

1、文件权限

对于文件,如果没有可读r,只有可写w,
那么使用vim编辑文件时会提示无法编辑,
但可强制编辑,也可以使用echo等命令进行
重定向或追加。
文件的可写w,只能对文件内容进行增删改,
文件的删除由上层目录是否可写控制
对于文件的可执行,需要注意一下3点:

1、首先文件的本身要能够执行(命令或脚本)

2、如果是普通用户,同时还需要具备可读r的权限才能执行文件

3、root用户只要有可执行x的权限就能执行文件

2、目录权限

可读r,具有浏览目录下文件和子目录内容的权限,
但如果没有可执行x的配合,则不能切换到指定目录里。

可写w,决定了该目录下的文件可否删除、修改,
但如果没有可执行x的配合,则不能删除、修改目录下的文件。

可执行x,表示具有进入目录的权限

3、企业案例

网站文件被恶意修改了。。。。打开网站后有弹窗广告
用户打开网站,报警。
原因:权限设置不到位。chmod -R 777 目录 开发人员习惯
 
解决方案:
1、备份
tar zcvf /opt/$(date +%F).tar.gz /
 
2、找到被修改的文件
find / -type f |xargs grep 'ddddddddddddd'

 
3、批量删除
find /root -type f |xargs sed -i '/ddddddddddddd/d'
 
4、找到文件被篡改来源,并优化调整。
亡羊补牢。
 
5、写总结 故障报告。

4、umask

文件权限安全临界点644
目录权限安全临界点755
 
umask基于文件从666计算,本质是rwrwrw
umask基于目录从777计算,本质是rwxrwxrwx

基于文件
当umask中存在奇数位的时候,在计算完毕,奇数位加1

666
011
-------------------
655
 11
-----------------
666

5、suid

suid功能是针对二进制命令(如passwd、ping等)或程序的,
不能用在shell等类似脚本文件上

任何用户可以在执行某个设置了suid位的命令或程序时,都相当于属主在执行
该用户首先要具有执行权限

suid是双刃剑,存在安全隐患。安全优化时,需要取消suid权限
[test@template ~]$ chmod 7644 test.sh 
[test@template ~]$ ls -ltr
total 4
-rwSr-Sr-T. 1 test test 12 Feb 27 20:20 test.sh
任何用户都能使用以下命令
[root@template ~/test]# find /usr/bin -type f -perm 4755|xargs ls -ltr
-rwsr-xr-x. 1 root root 32096 Oct 31  2018 /usr/bin/fusermount
-rwsr-xr-x. 1 root root 41936 Aug  9  2019 /usr/bin/newgrp
-rwsr-xr-x. 1 root root 78408 Aug  9  2019 /usr/bin/gpasswd
-rwsr-xr-x. 1 root root 73888 Aug  9  2019 /usr/bin/chage
-rwsr-xr-x. 1 root root 27856 Apr  1  2020 /usr/bin/passwd
-rwsr-xr-x. 1 root root 31984 Feb  3  2021 /usr/bin/umount
-rwsr-xr-x. 1 root root 32128 Feb  3  2021 /usr/bin/su
-rwsr-xr-x. 1 root root 44264 Feb  3  2021 /usr/bin/mount
-rwsr-xr-x. 1 root root 57576 Jan 14 00:52 /usr/bin/crontab
-rwsr-xr-x. 1 root root 27672 Jan 26 03:42 /usr/bin/pkexec
[root@template ~/test]# su - test
Last login: Sun Feb 27 20:20:19 CST 2022 on pts/0
[test@template ~]$ crontab -l
no crontab for test

6、sgid

sgid既可以针对文件,又可以针对目录设置

sigd的权限是针对用户组权限位的
对于文件来说,sgid的功能如下:

1、sgid仅对二进制命令及程序有效

2、二进制命令及程序,也要有可执行权限x配合

3、执行命令的任意用户可以获得该命令程序执行期间所属组的身份和权限
[root@template ~/test]# yum install mlocate -y

updatedb 更新locate查找内容对应的数据库
[root@template ~/test]# updatedb

locate搜索内容,默认从updatedb对应的数据库中查找
[root@template ~/test]# file /var/lib/mlocate/mlocate.db
/var/lib/mlocate/mlocate.db: data

[root@template ~/test]# echo "echo testabc" >/bin/testabc
[root@template ~/test]# chmod +x /bin/testabc
[root@template ~/test]# updatedb
[root@template ~/test]# locate testabc
/usr/bin/testabc

任何用户使用locate指令,都会有slocate组的权限
[root@template ~/test]# ls -ltr `which locate`
-rwx--s--x. 1 root slocate 40520 Apr 11  2018 /usr/bin/locate
对于目录(继承目录所属的组)来说,sgid的功能如下:

1、linux里默认情况所有用户创建文件,默认用户和组都是自身

2、sgid可以让用户在此目录下创建的文件和目录具有和此目录相同的用户组设置

sgid主要在目录中使用,为某个目录设置sgid后,在该目录新建的文件具有该目录
的所属组权限,而不是创建该文件的用户的默认所有者。这样,使得在多个用户之
间共享一个目录中的文件变的简单。

7、sticky

把一个目录的权限都打开,然后来共享文件,像/tmp目录,
只有目录的属主和root用户有权删除它。存在安全隐患,
生成环境应该禁止设置。(只用在目录)

十七、 定时任务

系统定时任务
不用管理员干预,系统自动执行。如系统日志、安全日志

用户定时任务
cron是定时任务的软件名
crond是服务进程名
crontab是定时任务命令
查看已经设置的定时任务
crontab -l

查看特定用户下的定时任务
crontab -l -u user
 
编辑定时任务
crontab -e
相当于在/var/spool/cron/目录下新建一个当前用户名的文件

root用户:
crontab -l 相当于 cat /var/spool/cron/root
crontab -e 相当于 vim /var/spool/cron/root
 
/etc/cron.deny记录哪些用户不能使用crontab命令
/etc/cron.allow记录用户允许使用crontab命令
/etc/cron.allow的优先级高于/etc/cron.deny
特殊符号:
"*" 表示每(每分每秒),第一位别用
"-" 表示连续区间
"," 表示列举
"/n" n是数字,表示每隔n单位时间
 
每天23:00执行cmd
00 23 * * * cmd
 
每天23:00开始每分钟执行cmd
* 23 * * * cmd
 
每天8:00到23:00之间的每个小时执行cmd
00 8-23 * * * cmd
 
每天的1:00,2:00,23:00执行cmd
00 1,2,23 * * * cmd
 
每隔10分钟执行cmd
*/10 * * * * cmd

分(0-59)	时(0-23)	日(1-31)	月(1-12)	周(0-6,0表示周日)
定时任务报错日志/var/log/cron
 
定时任务命令或脚本结尾加>/dev/null 2>&1
不要输出,不然会报错磁盘满了,因为inode满了
 
脚本中命令用全路径,或在开头export PATH="/..."
因为定时任务执行时未加载环境变量
也无法直接使用其他环境变量,因为未加载
需要手动定义,不然会调到NULL,报错
 
定时任务尽量用脚本文件实现
用/bin/sh不需要给脚本加执行权限
00 00 * * * /bin/sh /server/scripts/bak.sh &>/dev/null
运维规范:
脚本位置:/server/scripts
软件位置:/server/tools
编译位置:/application/软件名字-版本,然后要软链接
 
生产环境下的定时Cron书写要领
  要领1:为定时任务规则加必要的注释
  要领2:所有的定时任务尽量都以脚本的形式执行
  要领3:在执行的Shell脚本前加上/bin/sh
  要领4:定时任务中命令或脚本的结尾加>/dev/null 2>&1
  要领5:在指定用户下执行相关定时任务
  要领6:生产任务计划程序中不要随意打印输出信息,有输出的想法去掉。
  要领7:定时任务执行的脚本要存放到规范路径下
  要领8:配置定时任务要规范操作过程,减少出错
  要领9:定时任务脚本中程序命令及路径尽量用全路径
  要领10:时间变量%号要用反斜线转义(只有定时任务里是命令时需要)
  要领11:若脚本中调用了系统环境变量,要重新定义
  要领12:出错或无法执行,就检查/var/log/cron日志
企业故障案例:No space left on device常见企业故障案例
磁盘满的报错,原因往往是inode被占满了。
df -i
因为定时任务没有加&>/dev/null,定时任务执行的时候把输出给系统root发邮件。
邮件服务器postfix服务默认不开,这些给系统发的邮件就会堆在临时目录(大量小文件存在)
/var/spool/clientmqueue/      centos6 
/var/spool/postfix/maildrop/  centos7 
企业故障案例:程序通过手动可以执行,放定时任务里不执行。
原因:没有export变量
 
定时任务在执行脚本的时候,多数情况没办法加载环境变量,特别是/etc/profile。
登录后怎么操作都对,但是就是放到定时任务不行。
 
命令行:bash登录方式
1.手工登录后,加载所有环境变量(~/.bash_profile,~/.bash_rc,/etc/profile,/etc/bashrc)
2.定时任务执行脚本属于非登录状态
实践检验,定时任务不能自动加载/etc/bashrc和/etc/profile文件。
 
所有运维都会把变量放到此文件/etc/profile,把这个文件里的变量定义
在执行的脚本中重新定义。
 
具体为crond执行Shell时只能识别为数不多的系统环境变量,普通环境变量一般是无法识别的,
如果在编写的脚本中需要使用变量,最好使用export重新声明下该变量,以确保脚本正确执行。
以后要将其作为一个开发基本规范写上。

十八、su、sudo

用su管理,必须知道root密码,安全隐患
 
把当前用户切换为check
su - check :-表示携带check用户环境变量登录
 
以check身份执行指令,但不切换用户
su - check -c pwd
sudo允许指定用户执行某命令时拥有root权限
它是一个提权命令,对应权限通过读取/etc/sudoers实现
配置/etc/sudoers可以使用visudo命令
或vi /etc/sudoers(不推荐)
 
给check用户的ls命令设置所有权限
visudo
100gg
添加check  ALL=(ALL)  /usr/bin/ls

sudo -l 查看获得的权限

#visudo配置完,首次使用要输入check用户的密码
sudo ls /root 必须加sudo才能实现
 
以下这么设置,check就是root,并且不要密码
check  ALL=(ALL)  NOPASSWD: ALL
sudo useradd test
sudo su -    直接登录root,无需输入密码
 
给普通用户test增加useradd、userdel权限
visudo
100gg
添加test   ALL=(ALL)  /sbin/useradd,/sbin/userdel
wq
sudo useradd sss
sudo userdel -r sss
 
SUID 针对命令
     任何用户执行命令都有root身份
     任何用户执行某个命令:模糊

SUDO 针对用户
     给某个用户以root身份执行某个命令
     指定用户执行某个命令:具体

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值