CentOS-常用命令(版本:7.x)

常用命令

注:centos命令不定期持续更新,希望能够帮到你~

修改主机名

$ hostnamectl set-hostname xxx

查看IP

$ vim /etc/sysconfig/network-scripts/ifcfg-eth33

查看系统版本

$ cat /etc/redhat-release

查看内存

$ free -kh

查看磁盘存储

$ df -kh [/home]

统计目录使用大小

$ du -sh [/home]

$ du -kh --max-depth 1 [/home]

查看本机出口IP

$ curl ipinfo.io
$ curl -L ip.cn

下载文件-wget

$ wget --spider http://xxxxx/file    #测试是否存在
$ wget --tries=3 -c http://xxxxx/file    #-c自动断点续传
$ wget --limit-rate=200k --tries=3 -b -c http://xxxxx/file    #-b=后台下载,--limit-rate=限速防止占满带宽

查看磁盘IO(yum install isysstat -y)

$ iostat -x 1 3

查看磁盘IO进程(yum install iotop -y)

$ iotop

$ iotop -oP

查看磁盘IO进程(yum install iftop -y)

$ iftop

查看进程前10(按内存排序)

$ ps -e -o 'pid,comm,args,pcpu,rsz,vsz,%mem,stime,user,uid'|sort -nrk6|head -10

查看端口占用

$ netstat -lnp|grep 80

查看日志

$ tail [-f] xxx.log

统计文件个数(-lR递归子目录统计)

$ ls -l | wc

$ ls -lR | grep "^-" | wc -l

统计文件夹个数

$ ls -l | grep "^d" | wc -l

指定目录查找内容

$ grep -r localhost /etc/nginx/conf.d/

操作时间

设置时区
$ timedatectl set-timezone Asia/Shanghai
修改时间
$ date -s '2020-02-02 00:00:00'

同步网络时间(yum -y install ntp ntpdate)
$ ntpdate cn.pool.ntp.org
写入硬件防
$ hwclock --systohc

根据操作时间查找文件

查找当天的文件
$ find ./ -mtime 0

查找3天内的文件
$ find ./ -mtime -3

查找近30分钟的文件
$ find ./ -mmin -30

查找7天前的数据

$ find /test/*.* -maxdepth 1 -mtime +7

$ find /test/xxx -maxdepth 1 -type f -mtime +7

查找并删除7天前的数据(慎用,注意不要删除目录)

$ find /test/*.* -maxdepth 1 -mtime +7 | xargs rm -rf

$ find /test/xxx -maxdepth 1 -type f -mtime +7 | xargs rm -rf

查找指定目录下的空目录

$ find /test/xxx -maxdepth 3 -type d -empty | wc -l

查找并删除指定目录下的空目录

$ find /test/xxx -maxdepth 3 -type d -empty | xargs rm -rf

清空日志文件(可配合cron使用)

$ cat /dev/null > /var/log/xxx.log

释放交换空间(禁用再启用)

$ sync
$ swapoff -a && swapon -a

清理内存缓存(慎用)

$ sync
$ echo 1 > /proc/sys/vm/drop_caches
$ echo 2 > /proc/sys/vm/drop_caches
$ echo 3 > /proc/sys/vm/drop_caches

压缩

压缩文件夹
$ tar -zcvf ./testDir.tar.gz ./testDir

或(yum install zip -y)

$ zip -r xxx.zip xxx/

解压缩

$ tar -xzvf ./testDir.tar.gz

$ unzip xxx.zip

测速(yum install python-setuptools && easy_install pip)

pip安装(安装过忽略即可)

$ yum -y install epel-release
$ yum install python-pip
$ pip install --upgrade pip

假如较早安装过,不能使用,则升级下即可(pip install -U speedtest-cli)

$ pip install speedtest-cli
$ speedtest

限制系统网速上传和下载(yum -y install epel-release 和 yum -y install wondersharper)

例子(网卡eth0)
限制下载和上传(Kb/s)
$ wondershaper eth0 1024 1024
清除限制
$ wondershaper clear eth0

设置静态IP

## 常见的名称为 ifcfg-ens33、ifcfg-eth0
$ vim /etc/sysconfig/network-scripts/ifcfg-ens33

BOOTPROTO # 获取IP的方式,默认为 dhcp(动态),需要改成 static(静态)
IPADDR # 设置为自己想要的IP地址,注意,保持和和虚拟机网络配置在同一子网下,不要与宿主主机冲突,例:192.168.1.100
BROADCAST # 广播地址,最后一位是255,以本文配置为例此应为:192.168.1.100
NETMASK # 子网掩码,和虚拟机网络配置一致:255.255.255.0
GATEWAY # 默认网关,与虚拟网络编辑器的NAT设置一致:192.168.1.1
DNS # 必须进行设置,不然域名无法进行解析,只能通过IP进行访问
ONBOOT # yes表示启动就执行该配置,需要改为yes

编辑文件异常退出(找到.swp文件删除即可)

vim 常见问题 - 打开文件提示Swap file ".xxx.swp" already exists!

$ ls ./ -a
$ rm -rf .xxx.wap

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值