运维初学者必备之centos7常用操作指令详解
简介:
CentOS(Community Enterprise Operating System)是Linux发布版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定发布的源代码所编译而成。由于出自同样的源代码,因此有些要求高度稳定性的服务器以CentOS替代商业版的Red Hat Enterprise Linux使用。
centos7发布时间:2016 年 12 月 12 日, CentOS 维护人员 Karanbir Singh 高兴的宣布,期待已久的基于 Red Hat Enterprise Linux 的 CentOS Linux 7 (1611) 系统发布。
centos6停止维护更新日期:2020年11月30日
centos7停止维护更新日期:2024年6月30日
centos8停止维护更新日期:2029年5月31日,后更改为2021年12月31日(正在使用的同学须留意)
centos官网:https://wiki.centos.org/About/Product
一、centos7常用工作指令:
1.centos6.x和centos7.x命令对比:
| centos6.x | centso 7.x | 详解 |
|---|---|---|
| ifconfig | ip addr | 查看网卡信息命令 |
| service network status | systemctl status network | 查看网络服务状态 |
| service network stop | systemctl stop network | 关闭网络服务 |
| service network start | systemctl start network | 开启网络服务 |
| service network restart | systemctl restart network | 重启网络服务 |
| service iptables restart | systemctl restart firewalld | 重启防火墙服务 |
| service crond restart | systemctl restart crond | 重启定时任务 |
| checkconfig iptables off | systemctl disable firewalld | 设置防火墙开启不启动 |
| chenckconfig iptables on | systemcctl enable firewalld | 设置防火墙开机启动 |
| hostname node -1 (临时修改) | hostnamectl set-hostname node-1(永久修改) | 修改主机名 |
2.网络服务命令:
| 命令 | 详解 |
|---|---|
| ip addr | 查看网卡信息命令 |
| systemctl status network | 查看网络服务状态 |
| systemctl stop network | 关闭网络服务 |
| systemctl start network | 启动网络服务 |
| systemctl restart network | 重启网络服务 |
| systemctl restart firewalld | 重启防火墙 |
| systemctl disable firewalld | 设置防火墙开机不启动 |
| systemctl enable firewalld | 设置防火墙开机启动 |
| hostnamectl set-hostname node-1 | 修改主机名(永久) |
3.文件与目录操作:
| 命令 | 详解 |
|---|---|
| cd - | 返回上次所在的目录 |
| cd …/… | 返回两级目录 |
| cp file1 file2 | 将file1复制为file2 |
| cp -a dir1 dir2 | 复制一个目录 |
| cp -a /tmp/dir1 | 复制一个目录到当前目录 |
| ls -lrt | 按时间显示文件 |
| mkdir -p /tmp/dir/dir1 | 创建一个目录树 |
| rm -f file1 | 删除”file1” |
| rm -rf dir1 | 删除’dir1’目录及其子目录内容 |
4.文本内容处理:
| 命令 | 详解 |
|---|---|
| grep str /tmp/test | 在文件’/tmp/test’ 中查找”str” |
| grep ^ str /tmp/test | 在文件’/tmp/test’ 中查找”str” 开始的行 |
| grep str -r /tmp/* | 在目录’/tmp’ 及其子目录里查找”str” |
| diff file1 flie2 | 找出两个文件的不同处 |
| sdiff file1 file2 | 以对比的方式显示两个文件的不同 |
5.find查询操作:
| 命令 | 详解 |
|---|---|
| find / -name file1 | 查询文件 |
| find / -user user1 | 查询属于“user”的文件和目录 |
| find /home/user1 -name *.bin | 查询/home/user里以‘.bin’结尾的文件 |
| find /user/bin -type f -atime +100 | 查找过去100天内未被使用过的执行文件 |
| find /user/bin -type f -mtime -10 | 查找10天内被创建或者修改的文件 |
| locate *.ps | 寻找以‘.ps’结尾的文件 |
6.文件压缩、解压操作:
| 命令 | 详解 |
|---|---|
| bzip2 file1 | 压缩file1文件 |
| bunzip file1.bz2 | 解压file1.bz2文件 |
| gzip file | 压缩file1文件 |
| gzip -9 file1 | 最大程度压缩file1文件 |
| gunzip flie1.gz | 解压file1.gz文件 |
| tar -cvf archive.tar file1 | 把file1打包成 archive.tar |
| tar -cvf archive.tar file1 dir1 | 把 file1,dir1 打包成 archive.tar |
| tar -xvf archive.tar -C /tmp | 把压缩包释放到 /tmp目录下 |
| tar -tf archive.tar | 显示一个包中的内容 |
| tar -zxvf archive.tar.gz | 解压缩一个包 |
| zip file1.zip file1 | 创建一个zip格式的压缩包 |
| zip -r file1.zip file1 dir1 | 把文件和目录压缩成一个zip格式的压缩包 |
| unzip file1.zip | 解压一个zip格式的压缩包到当前目录 |
| unzip test.zip -d /tmp/ | 解压一个zip格式的压缩包到 /tmp 目录 |
a.常用案例操作:
| 案例 | 详解 |
|---|---|
| bunzip2 file1.bz2 | 解压一个叫做 ‘file1.bz2’的文件 |
| bzip2 file1 | 压缩一个叫做 ‘file1’ 的文件 |
| gunzip file1.gz | 解压一个叫做 ‘file1.gz’的文件 |
| gzip file1 | 压缩一个叫做 ‘file1’的文件 |
| gzip -9 file1 | 最大程度压缩 |
| rar a file1.rar test_file | 创建一个叫做 ‘file1.rar’ 的包 |
| rar a file1.rar file1 file2 dir1 | 同时压缩 ‘file1’, ‘file2’ 以及目录 ‘dir1’ |
| rar x file1.rar | 解压rar包 |
| unrar x file1.rar | 解压rar包 |
| tar -cvf archive.tar file1 | 创建一个非压缩的 tarball |
| tar -cvf archive.tar file1 file2 dir1 | 创建一个包含了 ‘file1’, ‘file2’ 以及 ‘dir1’的档案文件 |
| tar -tf archive.tar | 显示一个包中的内容 |
| tar -xvf archive.tar | 释放一个包 |
| tar -xvf archive.tar -C /tmp | 将压缩包释放到 /tmp目录下 |
| tar -cvfj archive.tar.bz2 dir1 | 创建一个bzip2格式的压缩包 |
| tar -xvfj archive.tar.bz2 | 解压一个bzip2格式的压缩包 |
| tar -cvfz archive.tar.gz dir1 | 创建一个gzip格式的压缩包 |
| tar -xvfz archive.tar.gz | 解压一个gzip格式的压缩包 |
| zip file1.zip file1 | 创建一个zip格式的压缩包 |
| zip -r file1.zip file1 file2 dir1 | 将几个文件和目录同时压缩成一个zip格式的压缩包 |
| unzip file1.zip | 解压一个zip格式压缩包 |
7.yum指令操作:
| 命令 | 详解 |
|---|---|
| yum -y install [package] | 下载并安装一个rpm包 |
| yum localinstall [package.rpm] | 安装rpm包,仓库解决所有依赖关系 |
| yum -y update | 更新当前系统中安装的所有rpm包 |
| yum update [package] | 更新一个rpm包 |
| yum remove [package] | 删除一个rpm包 |
| yum list | 列出当前系统中安装的所有包 |
| yum search [package] | 在rpm仓库中搜寻软件包 |
| yum clean [package] | 清除缓存目录(/var/cache/yum)下的软件包 |
| yum clean headers | 删除所有头文件 |
| yum clean all | 删除所有缓存的包和头文件 |
8.网络显示相关操作:
| 命令 | 详解 |
|---|---|
| ifconfig etho | 显示一个以太网卡的配置 |
| ifdown eth0 | 禁止eth0网络设备 |
| ifup eth0 | 启动eth0网络设备 |
| iwconfig eth1 | 显示一个无线网卡配置 |
| iwlist scan | 显示无线网络 |
| ip addr show | 显示网卡的IP地址 |
| ip a | 显示网络信息 |
| cp index.html root@192.168.80.100:/root | 以用户root发送文件index.html至192.168.80.100服务器的root目录下 |
| hostname | 查看当前主机名称 |
| hostname abc.com | 修改当前主机名称为abc.com |
| ping www.baidu.com | 查看域名对应的IP |
| nslookup www.baodu.com | 查看域名对应的IP |
| ssh 192.168.80.100 | 登录192.168.80.100服务器 |
| Ifconfig | 查看IP地址 |
| route | 查看路由信息 |
| wget http://www.baidu.com/index.html | 下载文件 |
9.系统相关操作:
| 命令 | 详解 |
|---|---|
| su root | 切换至root用户 |
| shutdown -h now | 立即关机 |
| shutdown -r now | 立即重启 |
| pstree | 以树状显示程序 |
| passwd | 修改密码 |
| df -h | 磁盘使用 |
a.系统服务启动相关:
systemctl list-unit-files | grep enable 查看开机启动项
systemctl enable firewalld 添加防火墙开机启动项服务
systemctl disable firewalld 移除防火墙开机启动项服务
b.查看监听(Listen)的端口:
| 命令 | 详解 |
|---|---|
| cat /etc/resolv.conf | 查看服务器DNS配置 |
| iptables -L | 查看防火墙规则 |
| route -n | 查看路由表 |
| netstat -s | 查看网络端口统计信息 |
| netstat -lntp | 查看监听(Listen)的端口 |
二、centos7防火墙基础命令:
centos7中的防火墙改成了firewall,使用iptables无作用;
1.开放80端口的方法如下:
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload 重新载入配置
返回success为成功
2.删除8080端口开放如下:
firewall-cmd --zone=public --remove-port=80/tcp --permanent
firewall-cmd --reload 重新载入配置
返回success为成功
3.命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效
配置文件说明;
| 文件 | 详解 |
|---|---|
| /usr/lib/firewalld | 存放默认文件 |
| /etc/firewalld | 存放用户定义的数据,例如:servuce、rule |
| server | 已定义好的规则 |
| zones | 存储区域规则 |
| firewalld.conf | 默认配置文件,默认区域:public,对应zones目录下的public.xml |
4.指令操作:
重启防火墙:
systemctl restart firewalld
关闭防火墙:
systemctl stop firewalld
查看防火墙运行状态:
systemctl status firewalld
查看开放的端口:
firewall-cmd --list-ports 查看已开放的端口
firewall-cmd --list-all 查看所有端口策略设置
结尾:
练习操作时,可自行在本地VM虚拟机上操作或在测试服务器上操作学习。
版权声明:本文为weipaiyizhan原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。