logo
YourData+
玉东数据
首页应用解决方案协作MUI-Remix博客
登录

YourData's blog

CentOS常用操作

CentOS常用操作

远程连接

$ ssh root@*.*.*.* -p 22 / ******
$ ssh root@*.*.*.* / ******

文件、目录

  • 传输文件
$ scp <local filepath> root@<remote IP>:<remote filepath>
$ rsync -avz -e <local filepath> ssh root@<remote IP>:<remote filepath>
$ cp -rf [path/filename] [newpath/filename] (r表示递归,f表示强制)  //强制复制文件
  • 查找、删除文件
$ whereis <文件或文件夹> / which <可执行文件>
$ find /<path> -name "filename"
$ find ./ -size +3k -a -size -10k //找出当前目录[3KB,10KB]大小的文件
$ find ./ -size +1c //找出当前目录大于1个字节的文件
$ find ./ -size 0c //找出当前目录0字节的文件
$ rm -rf find ./ -size 0c //删除当前目录0字节的文件
$ find . -name ./ -size 0 -print0 | xargs -0 rm //删除当前目录0字节的文件
$ find ./ -name "*" -type f -size 0c | xargs -n 1 rm -f //删除当前目录0字节的文件
$ find . -name "*" -type f -size 1024c | xargs -n 1 rm -f // 删除固定1K的文件
  • 查看磁盘文件及目录
$ df -h
$ du -h --max-depth=1 //一级目录下的文件大小
$ du -sh
$ ls -Rl|wc -l  //统计目录下所有文件的总数量(包括子目录)
$ ll -h <FilePath> //查看文件属性
$ tail -n <row_num> /var/log/httpd/access_log //显示文件部分内容

免密码登录

  • 客户端
$ ssh-keygen -t rsa
$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@<IP>
  • 服务器端
$ mkdir -m=700 .ssh
$ chown root ~/.ssh/authorized_keys
$ chmod 644 ~/.ssh/authorized_keys
  • 密钥登录、发送文件
$ chmod 400 ~/*.pem
$ ssh -i ~/*.pem root@IP //登录
$ rsync -Pav -e "ssh -i ~/*.pem" ~/Downloads/* root@IP:/mnt //发送文件

用户、用户组

  • 用户及用户组
$ cat /etc/group 查看
$ groupadd <group_name> 创建
$ groupdel <group_name> 删除
$ groupmod -n <target_group_name> <source_group_name> 修改
$ useradd <user_name> -g <group_name> -p <password> 创建用户并分配用户组

// 删除用户
$ userdel <user_name>
$ rm -rf /home/<user_name>
$ rm -rf /var/spool/mail/<user_name>
$ cut -d : -f 1 /etc/passwd 查看用户
$ cut -d : -f 1 /etc/group 查看用户组

$ whoami //查看登录用户

授权

$ chown nginx:nginx <filepath/filename> 修改用户组和用户
$ chmod 755 <filepath/filename> 权限

系统

  • 版本
$ cat /etc/redhat-release 检查当前 CentOS 系统版本
$ uname -sr 检查当前 CentOS 系统内核版本
$ uname -a  显示64/32位
  • 进程
$ top 显示执行中的程序进程
// kill进程
$ kill -s 9 <pid>
$ killall <appname> 

//查看占用内存最高的10个进程的两种方法
$ ps aux | sort -nk 4 -r | head -n 10
$ ps aux --sort=-pmem | head -n 10
  • systemctl
$ systemctl enable *.service 开机运行服务
$ systemctl disable *.service 取消开机运行
$ systemctl start *.service 启动服务
$ systemctl stop *.service 停止服务
$ systemctl restart *.service 重启服务
$ systemctl reload *.service 重新加载服务配置文件
$ systemctl status *.service 查询服务运行状态
$ systemctl --failed 显示启动失败的服务
$ systemctl is-enabled *.service 查看服务是否运行
$ systemctl kill <service> 杀死服务
  • shell
$ shell cat /etc/shells  //显示系统
$ shell echo $SHELL  //查看当前
$ shell chsh -s /bin/bash  //切换
  • 查看php-fpm的运行情况:
$ netstat -antp|grep php-fpm
$ ps -aux|grep php-fpm
  • CPU
$ cat /proc/cpuinfo| grep "physical id" | sort| uniq | wc -l  //查看物理CPU个数
$ cat /proc/cpuinfo| grep "cpu cores"| uniq  //查看每个物理CPU中core的个数(即核数)
$ cat /proc/cpuinfo| grep "processor"| wc -l  //查看逻辑CPU的个数
  • 内存
$ free -m/-k(单位) 查看内存使用情况
$ cat /proc/meminfo 查看定值(内存)
  • 查看开机启动项
$ chkconfig <appname> on /chkconfig nginx on 开机启动
$ systemctl list-unit-files
$ systemctl list-unit-files | grep enable
  • 进程、端口
$ netstat -ntlp //查看所有端口
$ netstat -lnp|grep <port>  //查看某个端口
$ ps -ef|grep ssh //查找特定的进程
$ ps aux|grep ssh //显示内存中运行的程序
$ ps -ef | grep <task>  //查看进程
$ jobs -l //查看当前终端的进程
$ ps -ef|grep <task>| grep -v grep | awk '{print $2}' //查看某个任务进程的ID
$ lsof -i:<port> //查看某端口
$ netstat -ap|grep <port> //查看某端口号的进程
$ netstat -nap|grep <port> //查看系统某个端口占用情况
$ kill -9 <pid> //终止后台运行的进程

防火墙

  • firewalld
// 命令行
$ firewall-cmd --state 或 systemctl status firewalld 查看运行状态
$ systemctl start/restart/stop firewalld.service 启动/重启/停止
$ systemctl enable firewalld.service 设置防火墙开机启动
$ systemctl is-enabled firewalld.service 查看服务是否开机启动
$ systemctl list-unit-files|grep enabled 查看已启动的服务列表
$ firewall-cmd --list-all 查看规则
$ firewall-cmd --reload 更新规则
$ firewall-cmd --get-default-zone 查看默认域
$ firewall-cmd --zone=<zone> --list-ports 查看此区域上所有打开的端口
$ firewall-cmd --get-active-zones 查看区域信息
$ firewall-cmd --list-ports 查看开启了哪些端口
$ firewall-cmd --list-services 查看开启了哪些服务
$ firewall-cmd --zone=public --list-ports 查看所有打开的端口
$ firewall-cmd --get-zone-of-interface=eth0 查看指定接口所属区域
$ firewall-cmd --panic-on 拒绝所有包
$ firewall-cmd --panic-off 取消拒绝状态
$ firewall-cmd --query-panic 查看是否拒绝
$ firewall-cmd --zone=<zone> --add-port=<port>/tcp 加入一个端口到区域
$ firewall-cmd --permanent --add-port=<port>/tcp 加入端口
$ firewall-cmd --zone=public --query-port=<port>/tcp 查看端口
$ firewall-cmd --zone=public --remove-port=<port>/tcp --permanent 删除端口
$ firewall-cmd --permanent --zone=public --add-port=<start-port>-<end-port>/tcp 开放端口区间

// 名词解释
阻塞区域(block):任何传入的网络数据包都将被阻止。
工作区域(work):相信网络上的其他计算机,不会损害你的计算机。
家庭区域(home):相信网络上的其他计算机,不会损害你的计算机。
公共区域(public):不相信网络上的任何计算机,只有选择接受传入的网络连接。
隔离区域(DMZ):隔离区域也称为非军事区域,内外网络之间增加的一层网络,起到缓冲作用。只有选择接受传入的网络连接。
信任区域(trusted):所有的网络连接都可以接受。
丢弃区域(drop):任何传入的网络连接都被拒绝。
内部区域(internal):信任网络上的其他计算机,不会损害你的计算机。只有选择接受传入的网络连接。
外部区域(external):不相信网络上的其他计算机,不会损害你的计算机。只有选择接受传入的网络连接。
  • iptables

定时任务

  • crontab
// 命令行
$ systemctl start/stop crond.service 开启/关闭crond服务
$ systemctl enable/disable crond.service 设置/禁止服务自启动
$ ps -aux | grep crond 查询crond服务
$ systemctl status crond.service 查询crond服务状态
$ crontab -e 编辑定时任务
$ crontab -l 列出当前用户的定时任务
$ crontab -r 删除当前用户的定时任务
$ systemctl reload/restart crond.service 重载/重启定时任务

// 说明
标准格式 * * * * * command
*1 一小时中的第几分钟 0-59
*2 一天中的第几小时 0-23
*3 一月中的第几天 1-31
*4 一年中的第几月 1-12
*5 一周中的星期几 0-7

特殊符号说明: * 代表任何时间 , 代表任何时间 - 代表连续时间范围  */n 代表每隔多久执行

安装包管理

  • rpm
$ rpm -ivh <package.rpm> 安装
$ rpm _uvh <package.rpm> 升级
$ rpm -e <package.rpm> 卸载
$ rpm -qa | grep <package> 检查是否安装
$ rpm -qi <package.rpm> 查看包信息
$ rpm -ql <package.rpm> 查看安装了那些文件
  • yum
$ rpm -qa|grep <soft> 查看是否安装
$ yum list installed | grep <软件名>  查看已安装的软件包
$ yum -y install <包名>/yum -y remove <包名>  yum安装/删除
$ yum search <package> 搜索
$ yum install -y <package> 安装
$ yum remove <package> 卸载
$ yum info <package> 查看包信息
$ yum repolist 查看yum仓库
$ yum list 查看已安装包

//更新yum并安装epel源
$ yum update -y
$ yum install epel-release
$ yum update

//更改yum源
$ wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/repo/epel-7.repo
$ yum clean all
$ yum makecache

// 安装
$ yum install gcc gcc-c++ 安装编译工具
$ yum install bash-completion -y 安装命令补全工具
  • 源码
  1. 源码的安装一般由3个步骤组成:配置(configure)、编译(make)、安装(make install)
  2. configure 是用来检查环境变量以及配置编译选项 --prefix 选项是配置安装目录 --help 输出详细的选项列表
  3. make 是用来将源代码编译成二进制文件
  4. make install 则是将make编译出来的文件安装到指定位置
孙玉微信号: sunly-

玉东数据是一家专注于互联网应用开发的企业,由从业多年的技术专家组成,在应用开发、网络运维等方面有广泛的经验和实力。

技术涵盖最新的 Web、小程序、Flutter、Android、iOS等多个领域的前沿技术。

商务合作,请微信扫码联系我:

18919890888微信同号
陇ICP备2021001826号