12个Linux系统高频率命令行和shell小脚本

简介:
原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://dgd2010.blog.51cto.com/1539422/1584952

以下是在部署OpenStack过程中摘录的一些较为常用的命令行或shell脚本,仅供参考。

1.杀死所有存在的僵尸进程

1
2
ps  -ef |  grep  defunc |  grep  - v  grep  |  awk  '{print $3}'  |  xargs  kill  -9
#pkill dnsmasq

2.去掉配置文件中的#符号和空白行

1
2
3
4
5
6
7
8
9
cat  > /root/delsc .sh <<eof
#!/bin/bash
# delete all spaces and comments of specialized file, using with  filename
[[  "\$1"  ==  ''  ]] &&  echo  "delete all spaces and comments of specialized file, using with \$@ filename"  &&  exit  1
grep  - v  \ # \$1 | grep -v ^$
eof
cat  /root/delsc .sh
chmod  +x  /root/delsc .sh
ln  -s  /root/delsc .sh  /usr/local/bin/delsc

3.CentOS7安装vmtools

1
2
3
4
5
6
7
8
9
# mount /dev/cdrom /mnt/
# cp /mnt/VMwareTools-9.4.10-2092844.tar.gz /tmp/
# cd /tmp/
# tar zxf VMwareTools-9.4.10-2092844.tar.gz
# /tmp/vmware-tools-distrib/vmware-install.pl
yum  install  open -vm-tools -y
systemctl  enable  vmtoolsd.service
systemctl start vmtoolsd.service
systemctl status vmtoolsd.service

4.修改Linux系统时区

1
2
3
4
mv  /etc/localtime  /etc/localtime ~
ln  -s  /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
chown  -h --reference= /etc/localtime ~  /etc/localtime
chcon -h --reference= /etc/localtime ~  /etc/localtime

5.中国大陆常用时间服务器列表

1
2
3
4
5
6
7
cat  >  /etc/ntp .conf <<eof
server 2.cn.pool.ntp.org iburst
server 3.asia.pool.ntp.org iburst
server 0.asia.pool.ntp.org iburst
restrict -4 default kod notrap nomodify
restrict -6 default kod notrap nomodify
eof

6.配置时间同步

1
2
3
4
5
6
7
8
9
10
11
12
13
rpm -qa |  grep  ntp || yum  install  -y ntp
ntpdate -u pool.ntp.org || ntpdate -u  time .nist.gov || ntpdate -u  time -nw.nist.gov
date
cat  >> /etc/rc . local <<EOF
ntpdate -u pool.ntp.org || ntpdate -u  time .nist.gov || ntpdate -u  time -nw.nist.gov
hwclock -w
EOF
# Recommoned do
touch  /etc/cron .daily /ntpdate
cat  >> /etc/cron .daily /ntpdate <<EOF
ntpdate -u pool.ntp.org || ntpdate -u  time .nist.gov || ntpdate -u  time -nw.nist.gov
hwclock -w
EOF

7.对配置文件更改前先备份配置文件

1
2
3
4
5
operationfile= /etc/keystone/keystone .conf
bakoperationfile=$operationfile$( date  +-%F-%H-%M-%S) "~"
cp  $operationfile $bakoperationfile
chown  -R --reference=$operationfile $bakoperationfile
chcon -R --reference=$operationfile $bakoperationfile

8.创建计划任务

1
( crontab  -l -u keystone 2>&1 |  grep  -q token_flush) ||  echo  '@hourly /usr/bin/keystone-manage token_flush >/var/log/keystone/keystone-tokenflush.log 2>&1'  >>  /var/spool/cron/keystone

9.不切换用户但以此用户的身份执行命令

1
su  -s  /bin/sh  -c  "glance-manage db_sync"  glance

10.获取路由IP

1
ip=$( ifconfig  `route |  grep  default |  awk  '{print $8}' ` |  grep  inet |  grep  - v  inet6 |  awk  '{print $2}' )

11.判断CPU是否支持虚拟化

1
2
3
4
5
6
7
8
9
if  [[ $( egrep  -c  '(vmx|svm)'  /proc/cpuinfo ) == 0 ]]; then
     defaultnum=` grep  -n  "^\[libvirt\]$"  $operationfile |  awk  -F  ':'  '{print $1}' `
     sedoperation=$defaultnum "a"
     sed  -i  "$sedoperation  virt_type = qemu"  $operationfile
else
     defaultnum=` grep  -n  "^\[libvirt\]$"  $operationfile |  awk  -F  ':'  '{print $1}' `
     sedoperation=$defaultnum "a"
     sed  -i  "$sedoperation  virt_type = kvm"  $operationfile
fi

12.获取指定网卡名所对应的IP地址

1
2
ext_ens=ens160
local_ip=$( ifconfig  `route |  grep  $ext_ens |  awk  '{print $8}' ` |  grep  inet |  grep  - v  inet6 |  awk  '{print $2}' )

end

补充:

1.查找并删除文件

1
find  /tmp  -name core - type  f -print0 |  xargs  -0  /bin/rm  -f

2.查找并列出文件类型

1
find  . - type  f - exec  file  '{}'  \;

3.查找大于1GB以上的文件,并列出

1
find  / -size +1000M - exec  ls  -alh  '{}'  \;

4.测试磁盘性能

1
time  dd  if = /dev/zero  of= /tmp/testfile  bs=4k  count=80000


本文出自 “通信,我的最爱” 博客,请务必保留此出处http://dgd2010.blog.51cto.com/1539422/1584952

目录
相关文章
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
1313 3
Linux系统禁用swap
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
1897 3
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
1382 3
Linux系统初始化脚本
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
1218 9
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
1289 2
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
1470 1
|
监控 安全 Linux
Linux系统提权之计划任务(Cron Jobs)提权
在Linux系统中,计划任务(Cron Jobs)常用于定时执行脚本或命令。若配置不当,攻击者可利用其提权至root权限。常见漏洞包括可写的Cron脚本、目录、通配符注入及PATH变量劫持。攻击者通过修改脚本、创建恶意任务或注入命令实现提权。系统管理员应遵循最小权限原则、使用绝对路径、避免通配符、设置安全PATH并定期审计,以防范此类攻击。
1757 1
|
Ubuntu Linux Shell
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
【Linux操作系统】探秘Linux奥秘:shell 编程的解密与实战
406 0
|
Shell Linux C++
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
495 0
|
运维 Shell Linux
Linux 之大数据定制篇-Shell 编程
Linux 之大数据定制篇-Shell 编程
540 0