Linux常用操作
红帽系系统安装和卸载epel源
CentOS7.x建议使用yum
yum -y install epel-release
yum remove epel-release
CentOS8.x建议使用dnf
dnf -y install epel-release
dnf remove epel-release
红帽系系统安装常用命令
CentOS7.x建议使用yum
yum -y install vim iotop nload zip unzip bind-utils ntp psmisc net-tools bzip2 wget screen p7zip nc telnet connect-proxy lsof bash-completion
CentOS8.x建议使用dnf
dnf -y install vim iotop nload zip unzip bind-utils chrony psmisc net-tools bzip2 wget screen p7zip nc telnet bash-completion
全部用户永久增加history操作时间记录
vim /etc/profile
底部增加一行
export HISTTIMEFORMAT="%F %T "
立即生效
source /etc/profile
服务器参数性能优化
vim /etc/sysctl.conf
修改配置文件,增加以下参数配置
net.ipv4.tcp_max_syn_backlog = 4096
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 2
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_fin_timeout = 5
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_time = 1200
net.core.somaxconn = 4096
vm.swappiness = 0
使配置的参数生效
sysctl -p
查看磁盘使用
df -hT
linux查看磁盘smart信息
yum install smartmontools
查看sda磁盘信息
smartctl -a /dev/sda
查看磁盘是否为固态硬盘
lsblk -d -o name,rota
在输出中,如果 rota 列的值为 0,则表示磁盘是固态硬盘;如果值为 1,则表示是机械硬盘
以树状图完整展示参数
lsblk -t -m
统计当前目录下文件夹大小并排序
对当前目录下文件夹统计显示
du -sh *
按统计从大到小排序
du -h --max-depth=1 | sort -hr
按统计从小到大排序
du -h --max-depth=1 | sort -h
find查找指定目录下指定后缀的指定内容文件
find . -type f -name "*.php" -exec grep -H "请使用正式域名访问" {} \;
find查找最近1天修改的文件
find . -type f -mtime -1
find命令查找最近新增的文件
查找指定目录下大于50M且在600分钟内新增的文件
find /www/wwwroot/ -type f -size +50M -mmin -600
linux下创建文件夹对应软链接
ln -s 原始文件夹 软连接名称
举例
ln -s /www/wwwroot/test /ruanlian
即把/www/wwwroot/test文件夹软链接到根目录/ruanlian文件夹
linux下压缩指定后缀文件
find . -name "*.png" | xargs tar -zcvf pngfiles.tar.gz
使用命令行查看文件hash签名
linux下
md5sum filename
sha1sum filename
sha256sum filename
sha512sum filename
windows下
certutil -hashfile filename md5
certutil -hashfile filename sha1
certutil -hashfile filename sha256
certutil -hashfile filename sha512
linux下删除无法删除的特殊乱码文件名文件
查看文件索引号
ls -i
详细可以使用
ls -lai
第一列即为索引号
查找索引号并执行删除操作,举例100985为索引号
find ./ -inum 100985 -exec rm {} \;
PS查看进行执行与运行时间
ps -eo pid,lstart,etime,cmd
linux下的特殊按键和快捷键
Ctrl + c ==> ^C的功能
Ctrl + v ==> ^V的功能
Ctrl + v + m ==> ^M的功能
Ctrl + v + n ==> ^N的功能
Ctrl + v 再按tab键 ==> 制表符
Ctrl + Insert ==> 复制
Shift + Insert ==> 粘贴
linux字符串查找备忘
grep OR功能 可用grep -E "aaa|bb" 或者 egrep "aaa|bb"
grep NOT功能 可用grep -v "aaa"
grep AND功能 可直接使用管道命令
linux下文件按行去重命令
文件按行去重-使用awk命令
hhhh.txt 为去重前文件
nnnn.txt 为去重后文件
awk '!x[$0]++' hhhh.txt>nnnn.txt
linux下使用tar打包压缩和解压文件
tar -cvf etc.tar /etc 仅打包,不压缩
tar -xvf etc.tar 解压打包
tar -zcvf etc.tar.gz /etc 以gzip压缩
tar -zxvf etc.tar.gz 以gzip解压缩
tar -jcvf etc.tar.bz2 /etc 以bzip2压缩
tar -jxvf etc.tar.bz2 以bzip2解压
tcp连接数查看统计
查看80端口连接数
netstat -ant | grep $ip:80 | wc -l
查看当前80端口连接数
netstat -ant | grep $ip:80 | grep EST | wc -l
常看tcp各连接状态数量
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
常用的三个状态是
ESTABLISHED 表示正在通信
TIME_WAIT 表示主动关闭
CLOSE_WAIT 表示被动关闭
CentOS查看修改DNS
cat /etc/resolv.conf
返回样例如下
# Generated by NetworkManager
nameserver 10.0.0.1
nameserver 180.76.76.76
如果修改可以使用nmtui修改后重启网络
使用find查找指定目录下超过200M的压缩文件
find /www/wwwroot -type f \( -name "*.zip" -o -name "*.rar" -o -name "*.tar.gz" \) -size +200M -exec ls -lh {} \;
find /www/wwwroot -type f \( -name "*.zip" -o -name "*.rar" -o -name "*.tar.gz" \) -size +200M -print0 | xargs -0 ls -l
#只取大小和文件路径(推荐)
find /www/wwwroot -type f \( -name "*.zip" -o -name "*.rar" -o -name "*.tar.gz" \) -size +200M -printf "%s\t%p\n"
对nginx日志文件提取第一列ip并去重统计数量排序取前20个
tail -10000 default.eshetuan.cn.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -20