查看内核/操作系统/CPU信息
cat /proc/version
uname -a
查看Linux系统版本的命令
lsb_release -a # 即可列出所有版本信息,这个命令适用于所有的Linux发行版,包括RedHat、SUSE、Debian…等发行版。
cat /etc/redhat-release # 这种方法只适合Redhat系的Linux
显示前十个文件
ls -1 | sort -u | head -10
FIND用法
#移动文件夹下符合条件的文件 把当前目录下面的file(不包括目录),移动到/opt/shell
find . -type f -exec mv {} /opt/shell \;
find . -type f | xargs -I '{}' mv {} /opt/shell
#搜索和删除指定文件夹,允许模糊查询
find . -type d -name .settings
find . -type d -name .sett*
find . -type d -name .settings | xargs -I '{}' rm -rf {}
#文件夹转成数组
find . -type f -name Dockerfile|sed 's/Dockerfile//'| tr "\n" " "
#查看当前所有文件夹大小
find . -type d -maxdepth 1 | xargs du -sh
#查看120天前的文件个数 mtime内容修改时间 atime访问时间 ctime属性修改时间
find . -mtime +120 | wc -l
#查看20天内的文件个数
find . -mtime -20 | wc -l
批量查找文件内容
cd etc
grep -rn "查找的内容" ./
文件替换处理
#批量替换目录下文件的内容
sed -i "s/查找的内容/替换后的内容/g" `grep -rl "查找的内容" ./`
#某行之前增加内容
sed -i "1i\echo sedtest \n" test.sh
查看端口占用
netstat -anp | grep 3306
查找并结束进程
ps -ef |grep $tomcat_home |awk {'print $2'} | sed -e "s/^/kill -9 /g" |sh -
新增用户及执行脚本
useradd nginx -s /sbin/nologin
#su -s /bin/bash -c "ls" nginx
sudo -u nginx systemctl start nginx
日志重定向
# 输出到指定日志文件
nohup java -jar app.jar >/tmp/java.log 2>&1 &
# 输出到空设备文件(全部丢弃)
nohup java -jar app.jar >/dev/null 2>&1 &
本来1----->屏幕 (1指向屏幕)
执行>log后, 1----->log (1指向log)
执行2>&1后, 2----->1 (2指向1,而1指向log,因此2也指向了log)
查找所有网络适配器
cat /proc/net/dev | awk '{i++; if(i>2){print $1}}' | sed 's/^[\t]*//g' | sed 's/[:]*$//g'
cat /proc/net/dev|awk '{i++; if(i>2){print $1}}'|sed 's/[:]*$//g'|grep -E "^en|^eth"
(1) /proc/net/dev是给用户读取或更改网络适配器及统计信息的方法;
(2) awk '{i++; if(i>2){print $1}}'命令是从第二行开始循环获取第一列数据;
(3) sed 's/^[\t]*//g'命令为去除行首的空格;
(4) sed 's/[:]*$//g'命令为去除行尾的":"字符.
(5) grep -E "^en|^eth" 筛选网卡
获取Linux服务器IP
# 主适配器IP,外网IP可使用一些网络API获取
ip -4 addr | sed -ne 's|^.* inet \([^/]*\)/.* scope global.*$|\1|p' | awk '{print $1}' | head -1
Linux系统启动脚本
sethost.sh
#! /bin/sh
# chkconfig: 345 99 10
case "$1" in
start)
# 在这里自定义我们执行的脚本逻辑
shost=`ip -4 addr | sed -ne 's|^.* inet \([^/]*\)/.* scope global.*$|\1|p' | awk '{print $1}' | head -1`
curl --location --request GET "http://wb.your-domain.com/serverlist?n=centos7local&u=root&h=${shost}&p=22" --silent >> info.log
#cd /myauto/sh
#sudo sh START_ALL.sh
;;
*)
;;
esac
exit 0
sethost.sh文件放置到/etc/init.d/
cd /etc/init.d/
chmod +x sethost.sh
chkconfig --add sethost.sh
创建Linux链接
# 在当前目录为 file.txt 创建硬链接 hlink.txt
ln file.txt hlink.txt
# 查看:硬链接与原文件 inode 相同
ls -li file.txt hlink.txt
# 创建指向 /data/logs 的软链接 ./logs
ln -s /data/logs ./logs
# 访问 ./logs 即等同于访问 /data/logs
# 若 hlink.txt 已存在,强制删除并重新创建硬链接
ln -f file.txt hlink.txt
加减乘除数学运算
echo $((10+2))
echo $(((10+2)*3-9/3))
服务器免密登录授权
#服务器生成私钥与公钥,会在登录用户的Home目录下生成一个.ssh 的目录
#此目录下存在 id_rsa 私钥与 id_rsa.pub 公钥。
ssh-keygen -t rsa
#用下面的命令将公钥发布到目标服务器上
ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 root@目标IP
#手动拷贝到目标服务器
scp -P 22 id_rsa.pub root@目标IP:/root/
#目标服务器上执行写入authorized_keys
cat id_rsa.pub ~/.ssh/authorized_keys
设置登录端口和重启sshd
echo "Port 622">>/etc/ssh/sshd_config
systemctl restart sshd
tail跟踪日志
根据关键字停止/限时10秒停止
exec timeout 10 tail -fn 0 --pid=`ps uxh|grep 'sed \/startup'|awk '{print $2}'|sort -nr|head -1` $tomcat_home/logs/catalina.out| sed '/startup in/q'
清理journal日志
journalctl --disk-usage
journalctl --vacuum-time=1w
journalctl --vacuum-size=100M
判断星期几
weeklist=("Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday")
if [[ $num =~ ^[0-9]+$ ]] && [ $num -ge 1 ] && [ $num -le 7 ]; then
echo "It's ${weeklist[num-1]}!"
else
echo "It's Noneday!"
fi
字符串处理
#移除一个字符/
result="${str%%/}"
假设我们定义了一个变量为:
file=/dir1/dir2/dir3/my.file.txt
可以用${ }分别替换得到不同的值:
${file%/}:删掉最后一个/ 如果最后一位是/
${file#/}:删掉第一个/ 如果第一位是/
${file#*/}:删掉第一个 / 及其左边的字符串:dir1/dir2/dir3/my.file.txt
${file##*/}:删掉最后一个 / 及其左边的字符串:my.file.txt
${file#*.}:删掉第一个 . 及其左边的字符串:file.txt
${file##*.}:删掉最后一个 . 及其左边的字符串:txt
${file%/*}:删掉最后一个 / 及其右边的字符串:/dir1/dir2/dir3
${file%%/*}:删掉第一个 / 及其右边的字符串:(空值)
${file%.*}:删掉最后一个 . 及其右边的字符串:/dir1/dir2/dir3/my.file
${file%%.*}:删掉第一个 . 及其右边的字符串:/dir1/dir2/dir3/my
记忆方法:
# 是 去掉左边(键盘上#在 $ 的左边)
%是去掉右边(键盘上% 在$ 的右边)
单一符号是最小匹配;两个符号是最大匹配
${file:0:5}:提取最左边的 5 个字节:/dir1
${file:5:5}:提取第 5 个字节右边的连续5个字节:/dir2
也可以对变量值里的字符串作替换:
${file/dir/path}:将第一个dir 替换为path:/path1/dir2/dir3/my.file.txt
${file//dir/path}:将全部dir 替换为 path:/path1/path2/path3/my.file.txt
利用 ${ } 还可针对不同的变数状态赋值(沒设定、空值、非空值):
${file-my.file.txt} :假如 $file 沒有设定,則使用 my.file.txt 作传回值。(空值及非空值時不作处理)
${file:-my.file.txt} :假如 $file 沒有設定或為空值,則使用 my.file.txt 作傳回值。 (非空值時不作处理)
${file+my.file.txt} :假如 $file 設為空值或非空值,均使用 my.file.txt 作傳回值。(沒設定時不作处理)
${file:+my.file.txt} :若 $file 為非空值,則使用 my.file.txt 作傳回值。 (沒設定及空值時不作处理)
${file=my.file.txt} :若 $file 沒設定,則使用 my.file.txt 作傳回值,同時將 $file 賦值為 my.file.txt 。 (空值及非空值時不作处理)
${file:=my.file.txt} :若 $file 沒設定或為空值,則使用 my.file.txt 作傳回值,同時將 $file 賦值為 my.file.txt 。 (非空值時不作处理)
${file?my.file.txt} :若 $file 沒設定,則將 my.file.txt 輸出至 STDERR。 (空值及非空值時不作处理)
${file:?my.file.txt} :若 $file 没设定或为空值,则将 my.file.txt 输出至 STDERR。 (非空值時不作处理)
${#var} 可计算出变量值的长度:
${#file} 可得到 27 ,因为/dir1/dir2/dir3/my.file.txt 是27个字节
Linux菜单登录
servers.sh
#!/bin/bash
### varibles
cLine=0
cMin=7
cMax=7
### server list
#servers=("aliyunecs|47.96.228.158|622|root||ssh" "awsvps|52.11.207.98|22|ec2-user|crt:./.svcfg/aws-ed25519-kensad.pem|ssh")
servers=(`cat ./.svcfg/server.list| grep -v "^#"| tr "\n" " "`)
### clear the screen
tput clear
### Move cursor to screen location X,Y (top left is 0,0)
tput cup 5 15
### Set reverse video mode and init serverlist
tput rev
echo "S E R V E R - L I S T"
tput sgr0
for server in ${servers[@]}; do
tput cup ${cMax} 15
# echo "${server%%|*}"
echo "${server}"|awk -F "|" '{printf "%-25s\n", $1}'
let cMax++
done
let cMax--
### set bold mode
tput bold
tput cup $(($cMax+2)) 15
echo "press Up/Down to choice server, press enter to login"
tput cup $(($cMax+3)) 15
echo "press ESC(2 times) or type :q to exit"
tput sgr0
### init the first option
tput cup ${cMin} 15
cLine=${cMin}
### disable ctrl+c
trap "" INT QUIT TSTP
### set default key
akey=(0 0 0)
### set ecs key
cESC=`echo -ne "\033"`
### monitor of keyboard
while :
do
### read and set key arrays
read -s -n 1 key
akey[0]=${akey[1]}
akey[1]=${akey[2]}
akey[2]=${key}
#echo ${akey[0]} ${akey[1]} ${akey[2]}
if [[ ${key} == "q" && ${akey[1]} == ":" ]]
then
### :q
tput clear
tput sgr0
tput rc
exit 0
elif [[ ${key} == ${cESC} && ${akey[1]} == ${cESC} ]]
then
### ESC
tput clear
tput sgr0
tput rc
exit 0
elif [[ -z $key ]]
then
### ENTER
tput clear
tput sgr0
tput rc
#sh .${servers[cLine-cMin]}.sh
serverarr=(${servers[cLine-cMin]//|/ })
serverinfo=${serverarr[0]}
serveruser=$(echo "$serverinfo" | awk -F'[@:]' '{print $1}')
serverport=$(echo "$serverinfo" | awk -F'[@:]' '{print $3}')
if [ -z "$serverport" ]; then
serverport=22
fi
serverip=$(echo "$serverinfo" | awk -F'[@:]' '{print $2}')
authinfo=${serverarr[1]}
### if authinfo variables and end with pem
if [ -n "${authinfo}" ] ;then
if [[ "${authinfo}" =~ ^.*crt: ]] ;then
### certfile
certfile=${authinfo##*:}
chmod 600 ${certfile}
ssh -o StrictHostKeyChecking=no -p ${serverport} ${serveruser}@${serverip} -i ${certfile}
else
### if sshpass installed?
sshpassinstalled=`which sshpass`
if [ -n "${sshpassinstalled}" ] ;then
sshpass -p ${authinfo##*:} ssh -o StrictHostKeyChecking=no -p ${serverport} ${serveruser}@${serverip}
else
ssh -o StrictHostKeyChecking=no -p ${serverport} ${serveruser}@${serverip}
fi
fi
else
ssh -o StrictHostKeyChecking=no -p ${serverport} ${serveruser}@${serverip}
fi
exit 0
elif [[ ${akey[0]} == ${cESC} && ${akey[1]} == "[" ]]
then
if [[ ${key} == "A" && ${cLine} -gt ${cMin} ]];then
### UP
let cLine--
tput cup ${cLine} 15
elif [[ ${key} == "B" && ${cLine} -lt ${cMax} ]];then
### DOWN
let cLine++
tput cup ${cLine} 15
elif [[ ${key} == "D" ]];then tput cup ${cLine} 15
elif [[ ${key} == "C" ]];then tput cup ${cLine} 15
fi
fi
done
.svcfg/servers.list
# server list
# |分割 格式:连接IP:PORT|用户|鉴权信息
# 免密方式(keygen) 鉴权信息留空,需要提前完成免密设置
# 证书方式 鉴权信息以crt:开头后跟证书路径
# sshpass方式 鉴权信息以pwd:开头跟密码
root@47.96.228.158:622|
root@43.156.211.113|pwd:xxx******
root@192.168.56.3|pwd:ken******
root@47.96.228.158:30022|pwd:xxx******
ec2-user@52.11.207.98|crt:./.signcfg/aws-***.pem
安装sshpass
yum install sshpass -y
apt-cyg install sshpass -y
servers2.sh
#!/bin/bash
### server list
servers=("root@47.96.228.158:622|" "root@43.156.211.113:22|pwd:ken******" "ec2-user@52.11.207.98|crt:./aws-***.pem")
#servers=(`cat servers.list| grep -v "^#"| tr "\n" " "`)
### Set reverse video mode and init serverlist
echo "S E R V E R - L I S T"
appIndexNo=1
for server in ${servers[@]}; do
printf "$appIndexNo: "
#echo "${server%%|*}"|awk -F ":|@" '{printf "%-18s%-12s%-18s%-5s\n",$4,$1,$2,$3}'
echo "${server%%|*}"
let appIndexNo++
done
read -p 'choose the server(default:1):' serverNo
if [ -z "$serverNo" ]; then
serverNo=1
fi
psi=${servers[serverNo-1]##*|}
srv=${servers[serverNo-1]%%|*}
array=(${srv//:/ })
### if psi variables and end with pem
if [ -n "${psi}" ] ;then
if [[ "${psi}" =~ ^.*crt: ]] ;then
### certfile
ssh -o StrictHostKeyChecking=no -p ${array[1]} ${array[0]} -i ${psi##*:}
else
### if sshpass installed?
sshpassinstalled=`which sshpass`
if [ -n "${sshpassinstalled}" ] ;then
sshpass -p ${psi##*:} ssh -o StrictHostKeyChecking=no -p ${array[1]} ${array[0]}
else
ssh -o StrictHostKeyChecking=no -p ${array[1]} ${array[0]}
fi
fi
else
ssh -o StrictHostKeyChecking=no -p ${array[1]} ${array[0]}
fi