文章目录
环境说明
- CentOS Linux release 7.6.1810 (Core)
- Linux VM-4-2-centos 3.10.0-1160.11.1.el7.x86_64 #1 SMP Fri Dec 18 16:34:56 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
SSH配置
- 客户端与服务端心跳机制配置
sed -i 's/#ClientAliveInterval [0-9]\{1,9\}/ClientAliveInterval 20/g' /etc/ssh/sshd_config
sed -i 's/#ClientAliveCountMax [0-9]\{1,9\}/ClientAliveCountMax 9999/g' /etc/ssh/sshd_config
systemctl restart sshd
ClientAliveInterval:
每隔多少秒,服务器端向客户端发送心跳。
ClientAliveCountMax:
发送最大心跳次数。超过此值就认为客户端已断开。
磁盘占用空间
文件占用磁盘空间逆序排列
du -sh * | sort -hr
选出排在前面的10个
du -s * | sort -hr | head
选出排在后面的10个
du -s * | sort -hr| tail
磁盘动态扩容
注意备份数据,本方法会对缩减目录格式化
- 卸载/home分区
umount /home
- 将/dev/mapper/centos-home的空间降低(降低多少,就在后面扩容多少)
lvreduce -L -100G /dev/mapper/centos-home
- 重新格式化这个分区
mkfs.xfs /dev/mapper/centos-home -f
- 进行挂载/home分区
mount /dev/mapper/centos-home /home
- 扩容根分区
lvextend -L +100G /dev/mapper/centos-root
- 修改生效
xfs_growfs /dev/mapper/centos-root
- 验证
添加新磁盘
# 使用fdis给磁盘分区
# 先确认磁盘在/dev目录下的名称,例如这里叫vda
fdisk -l
fdisk /dev/vda
mkdir -p /data
mkfs -t xfs /dev/vda1
mount /dev/vda1 /data
echo '/dev/vda1 /data xfs defaults 1 2' >> /etc/fstab
cat /etc/fstab
init 6
网络
网络工具
yum install telnet
yum install net-tools
yum install nc
网络配置
问题: Centos7 IP
类型是DHCP
且可以正常联通外网;修改成static
,虽然可以联通局域网其他ip
但是无法连接外网。
方案: 添加DNS
为当前局域网的网关或者是8.8.8.8
,114.114.114.114
。
网络监控
- 查看各个进程的流量情况
# yum -y install nethogs
# nethogs
# nethogs eth0 监控网卡eth0
Shell文本乱码
方法1
shell
文档在windows
环境编辑后,在linux
执行会出现$'\r': command not found
。操作系统换行符不同。
windows: \r\n
Linux: \n
打开此文件设置如下信息
:set ff=unix
:wq
方法2
:
sed -i 's/\r//' xx.sh
方法3
:
mac
, UNIX
, Linux
系统换行符LF
windows
系统换行符LFCR
脚本处理多行输出
Linux
内部字段分隔符IFS(internal field separator)
。默认情况下,shell
会将下面的字符当做字段分隔符:空格、制表符、换行符。
shell文件修改方式如下:
IFS=$'\n'
配置系统时间
- 查看当前系统时间
date
- 修改系统时区/时间
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'Asia/Shanghai' > /etc/timezone
- 打开
ntp
yum -y install ntpd
systemctl start ntpd
systemctl enable ntpd
ntpdate 0.asia.pool.ntp.org
- 系统时间同步到硬件
hwclock --systohc
- 手动指定系统软硬时间
timedatectl set-time "2022-01-14 13:17:10"
https://www.cnblogs.com/yogurtwu/p/9343213.html
手动修改文件修改时间
- 使用touch修改文件修改/访问时间
touch
创建文件时如果文件已经存在,则该文件的3个时间(atime/ctime/mtime
)都会更新为当前时间。若该文件不存在,则会主动建立一个新的空文件。
touch的相关参数如下:
-a : 仅修改access time。
-c : 仅修改时间,而不建立文件。
-d : 后面可以接日期,也可以使用 --date="日期或时间"
-m : 仅修改mtime。
-t : 后面可以接时间,格式为 [YYMMDDhhmm]
[root@VM-4-15-centos ~]# ll
total 0
[root@VM-4-15-centos ~]# touch test1
[root@VM-4-15-centos ~]# ll
total 0
-rw-r--r-- 1 root root 0 Aug 28 23:22 test1
[root@VM-4-15-centos ~]# echo "hello" > test1
[root@VM-4-15-centos ~]# ll
total 4
-rw-r--r-- 1 root root 6 Aug 28 23:22 test1
[root@VM-4-15-centos ~]# cat test1
hello
[root@VM-4-15-centos ~]# touch test1
[root@VM-4-15-centos ~]# cat test1
hello
[root@VM-4-15-centos ~]# ll
total 4
-rw-r--r-- 1 root root 6 Aug 28 23:22 test1
[root@VM-4-15-centos ~]# stat test1
File: ‘test1’
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 393641 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-08-28 23:22:58.974626502 +0800
Modify: 2022-08-28 23:22:57.009626526 +0800
Change: 2022-08-28 23:22:57.009626526 +0800
Birth: -
[root@VM-4-15-centos ~]# touch test1
[root@VM-4-15-centos ~]# stat test1
File: ‘test1’
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 393641 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-08-28 23:28:29.366616007 +0800
Modify: 2022-08-28 23:28:29.366616007 +0800
Change: 2022-08-28 23:28:29.366616007 +0800
Birth: -
[root@VM-4-15-centos ~]# touch -m -d "2022-08-01 11:11:11" test1
[root@VM-4-15-centos ~]# ll
total 4
-rw-r--r-- 1 root root 6 Aug 1 11:11 test1
[root@VM-4-15-centos ~]# stat test1
File: ‘test1’
Size: 6 Blocks: 8 IO Block: 4096 regular file
Device: fd01h/64769d Inode: 393641 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-08-28 23:28:29.366616007 +0800
Modify: 2022-08-01 11:11:11.000000000 +0800
Change: 2022-08-28 23:29:20.522613980 +0800
Birth: -
[root@VM-4-15-centos ~]#
配置yum国内源
- 下载仓库文件
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
- 文件中
CentOS-Base.repo
中的http
更改为’https’ yum clean all
yum makecache
服务器安装Centos7
- 进入安装界面"Install Centos7",按键
e
进入启动项编辑模式
vmlinuz initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet
改为
vmlinuz initrd=initrd.img linux dd quiet
- 查看
U
盘对应名称,例如我的U
盘就是sdb1
- 重启,重新进入步骤
1
,将如下信息
vmlinuz initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 rd.live.check quiet
改为
vmlinuz initrd=initrd.img inst.stage2=hd:/dev/sdb1 quiet
- 按键
ctrl + x
,开始安装
ssh客户端更改显示语言(需要重启)
sed -i 's/ LANG / /g' /etc/ssh/sshd_config
sed -i 's/ LANG / /g' /etc/ssh/ssh_config
systemctl restart sshd
# 关闭当前会话窗口并重新登录即可生效
防火墙策略
添加
firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效)
重新载入(添加,删除后要重新载入生效)
firewall-cmd --reload
查看是否生效
firewall-cmd --zone=public --query-port=80/tcp
删除
firewall-cmd --zone=public --remove-port=80/tcp --permanent
查看所有打开的端口
firewall-cmd --zone=public --list-ports
环境变量全局生效
echo 'export https_proxy=xxx' >> /etc/profile
source /etc/profile
cat /etc/profile
tar解压缩
tar
解压错误
# tar -zxf xx.tar.gz
gzip: stdin: not in gzip format
- 解决办法
这个压缩包没有用gzip
格式压缩,不需要添加参数z
SSH登录卡顿缓慢
/etc/ssh/sshd_config
GSSAPIAuthentication no
UseDNS yes
以下场景仅在CentOS7测试通过
sed -i '/^#UseDNS/c UseDNS yes' /etc/ssh/sshd_config
sed -i '/^GSSAPIAuthentication/c GSSAPIAuthentication no' /etc/ssh/sshd_config
关闭SELINUX
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config