查看操作系统版本
#lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.6.1810 (Core)
Release: 7.6.1810
Codename: Core
永久修改主机名(centos7)
hostnamectl set-hostname host1 //修改所有三个主机名:静态、瞬态和灵活主机名,切换终端或注销即可生效
服务器基础安装(centos7)
yum install epel-release -y && yum makecache && yum install bash-completion net-tools lrzsz telnet htop iftop iotop atop vim wget -y //bash-completion centos7systemctl命令补全插件bash-completion
yum install ntp -y && systemctl enable ntpd && systemctl start ntpd //时间服务
yum install java java-1.8.0-openjdk-devel -y //安装java和jps
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun && systemctl enable docker &&systemctl start docker //官方镜像阿里云脚本安装
curl -sSL https://kuboard.cn/install-script/set_mirror.sh | sh -s https://registry.cn-hangzhou.aliyuncs.com //添加镜像加速
yum clean all && yum makecache && yum update
查看命令属于哪个软件包
yum provides "*/snmp"
运行上面命令的第二个参数
ls !$
!$(上次命令的第二个参数)
查看服务器公网IP
curl cip.cc
curl ifconfig.me
生成秘钥
ssh-keygen -t rsa #生成密钥对
cat id_rsa.pub > authorized_keys #创建公钥文件
chmod 600 authorized_keys #修改公钥文件权限
解压缩
##打包
tar -cvzf data.tar.gz /data ##打包data目录
zip -q -r data.zip /data //-r 目录
##解压
tar -xvzf data.tar.gz -C /tmp ##压缩到/tmp目录
unzip data.zip
普通用户运行程序
useradd redis && passwd redis
chown -R redis:redis /opt/redis-server
显示历史命令时间
echo "export HISTTIMEFORMAT='%F %T'" >> /etc/profile && source /etc/profile
后台运行脚本
nohup java -jar ${Jar_Name}.jar & //日志输出nohup.out文件
nohup java -jar ${Jar_Name}.jar >/dev/null 2>&1 & //日志标准输出到空
查看日志
打印日志匹配行的前后10行
grep -C 10 "error" egg-web.log //简写:grep -10 "error" egg-web.log
grep -R error logs/* //全目录索引 -i 忽略大小写 -R 包括目录
tail -n 100 egg-web.log |grep -10 "error"
查看某时间段日志
可以用grep,格式为:grep -E '起始时间|结束时间' 日志文件,如下:
grep -E '29/Jan/2019|30/Jan/2019' access.log
查看日志文件格式和编码
查看格式:
file /var/log/rsyncd.log
/var/log/rsyncd.log: ASCII text
查看编码
vim /var/log/rsyncd.log
:set fileencoding
fileencoding=utf-8
远程文件传输
scp -P 22 -r /root/123.txt root@192.168.1.1:/root (口令传输,-P 端口,-r 传输目录)
scp -P 47629 -i ~/.ssh/id_rsa_qa /etc/init.d/redis root@116.62.213.205:/etc/init.d (密钥传输,免密钥)
简写(使用别名):
echo "alias sscp='scp -P 47629 -i ~/.ssh/id_rsa_qa'" >> ~/.bashrc && source ~/.bashrc
sscp /etc/init.d/redis root@116.62.213.205:/etc/init.d
Busybox工具(推荐用于docker调试)
wget https://busybox.net/downloads/binaries/1.21.1/busybox-x86_64 \
&& chmod +x busybox-x86_64 \
&& mv busybox-x86_64 /usr/bin/busybox
获取PID,杀掉进程
PID=$(ps -ef |grep "server_name" |grep -v grep|awk -F " " '{print $2}')
ps -ef |grep "server_name" |grep -v grep|awk -F " " '{print $2}' |xargs kill
查看实际可有内存
[root@iZbp1f46qhpa7oddnz4ztbZ tomcat]# free -mh
total used free shared buff/cache available
Mem: 7.6G 2.1G 1.8G 1.9M 3.7G 5.2G
(实际可用内存=free+buffers+cached )
[root@xuegod63 ~]#echo 3 > /proc/sys/vm/drop_caches (手工清理缓存)
查看TCP连接状态
netstat -ant|awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
FIN_WAIT2 2
CLOSING 5
SYN_RECV 1
TIME_WAIT 6
ESTABLISHED 7022
LAST_ACK 15
SYN_SENT 7
FIN_WAIT1 6
ss -ant|awk 'NR>1{++S[$1]}END{for (a in S) print a,S[a]}' (推荐)
FIN_WAIT2 2
CLOSING 11
LISTEN 6
SYN_RECV 3
TIME_WAIT 3
ESTABLISHED 7017
LAST_ACK 16
SYN_SENT 5
FIN_WAIT1 8
SYN_RECV表示正在等待处理的请求数;
ESTABLISHED表示正常数据传输状态;
TIME_WAIT表示处理完毕,等待超时结束的请求数。
##只查连接数(tomcat)
netstat -nat|grep -i "7081"|wc -l
查找进程并关闭
ps -ef | grep java | grep tomcat | awk '{print $2}' | xargs kill -9 (print 用单引号;xargs 参数传递)
端口查看程序位置
ps -ef |grep ` netstat -lntp |grep 8804 |awk '{print $7}' |awk -F"/" '{print $1}'`
##grep -w 完全匹配,默认模糊匹配;awk -F"/" 自定义分隔符,默认空格
去掉配置文件的注释行和空行
grep -v "^#" httpd.conf | grep -v "^$" >> vsftpd.conf
去除空行方法:
1 sed '/^$/d' input.txt > output.txt #output file: output.txt
2 sed -i '/^$/d' input.txt #output file: input.txt
3 awk 'NF > 0' input.txt > output.txt #output file: output.txt
4 grep -v '^$' input.txt > output.txt #output file: output.txt
sed -i 's/maxmemory 7G/maxmemory 1G/g' 7001.conf 7002.conf ##替换内容,可以通知替换多个
查看错误的问题
批量替换目录所有文件
grep -Rl 'https://xl.sinaif.com' * | xargs sed -i "s+https://xl.sinaif.com+http://xl.sinaif.com+g”
-R 当前目录
-l 只列出匹配内容的文件列表
grep -Rl 'jdk-alpine' * | xargs sed -i "s+jdk-alpine+jdk-alpine-in+g”
Mac 上使用sed: sed -i '' 's/main/fun/g' 'Test.txt'
将DOS格式文本文件转换成UNIX格式
格式: dos2unix file1 file2 file3 //转换多个文件
格式:dos2unix oldfile newfile //保存新文件
vim全部替换
:1,$s/11/22/g
Vim 中如何去掉 ^M 字符?
dos2unix filename
删除行尾的^M:%s///g
网络测试工具
traceroute[-n]-T-p<目标端口号>Host
开启目录WEB服务(常用于一键安装脚本)
python -m SimpleHTTPServer 8888 (不指定端口,默认8000)
浏览器打开:http:ip:8888
nohup sh webserver.sh & //后台运行
#!/bin/bash
python -m SimpleHTTPServer 8888
rm反删除(排除)
开启命令:
shopt -s extglob
关闭命令:
shopt -u extglob
举例:
rm -fr !(file1);rm -rf !(file1|file2)
5个模式匹配操作符
?(pattern-list) - 所给模式匹配0次或1次;
*(pattern-list) - 所给模式匹配0次以上包括0次;
+(pattern-list) - 所给模式匹配1次以上包括1次;
@(pattern-list) - 所给模式仅仅匹配1次;
!(pattern-list) - 不匹配括号内的所给模式