非常实用的Linux命令
系统服务管理
systemctl
systemctl命令是Systemd系统和服务管理器的一部分,用于控制systemd系统和服务管理器。Systemd是大多数最新的Linux发行版使用的初始化系统和服务管理器,它用于启动守护进程并管理它们的运行。systemctl提供了一个命令行界面,允许你管理和查询系统状态和服务。
常用的命令
-  启动服务:启动一个或多个服务。 systemctl start <service_name>.service# 示例 systemctl start firewalld
-  停止服务:停止一个或多个正在运行的服务。 systemctl stop <service_name>.service# 示例 systemctl stop firewalld
-  重启服务:重启一个或多个服务。 systemctl restart <service_name>.service# 示例 systemctl restart firewalld
-  重新载入配置信息而不中断服务。 systemctl reload <service_name>.service# 示例 systemctl reload firewalld
-  ** 
-  显示服务状态:显示一个或多个服务的当前状态。 systemctl status <service_name>.service# 示例 systemctl status firewalld
-  启用服务:使服务在启动时自动启动。 systemctl enable <service_name>.service# 示例 systemctl enable firewalld
-  禁用服务:禁止服务在启动时自动启动。 systemctl disable <service_name>.service# 示例 systemctl disable firewalld
-  检查所有服务的状态:如你提到的, systemctl可以用来输出系统中各个服务的状态。systemctl list-units --type=service 
-  查看已启动的服务:列出当前已经启动的服务。 systemctl list-units --type=service --state=running

使用场景
- 服务管理:systemctl是管理Linux服务的主要工具,允许你启动、停止、重启和查看服务状态。
- 系统启动:通过systemctl可以配置哪些服务在系统启动时自动启动,以及在什么条件下启动。
- 故障诊断:通过查看服务的状态和日志(使用journalctl命令与systemctl配合),可以帮助你诊断系统问题。
systemctl提供的功能比旧的service和chkconfig命令更加强大和灵活,是现代Linux系统管理的核心工具之一。
文件管理
ls
列出指定目录下的所有文件,列出/目录下的文件:
ls -l /

pwd
获取目前所在工作目录的绝对路径:
pwd

cd
改变当前工作目录:
cd /usr/local

date
date '+%Y-%m-%d %H:%M:%S'

passwd
passwd root

su
改变用户身份(切换到超级用户):
su root

clear
用于清除屏幕信息
man
显示指定命令的帮助信息:
man ls

who
- 查询系统处于什么运行级别:
who -r 

- 显示目前登录到系统的用户:
who -buT

free
显示系统内存状态(单位MB):
free -m

ps
- 显示系统进程运行动态:
ps -ef

- 查看sshd进程的运行动态:
ps -ef | grep sshd

top
查看即时活跃的进程,类似Windows的任务管理器。

mkdir
创建目录:
mkdir /data/mydata

more
用于分页查看文件,例如每页10行查看boot.log文件:
 more -c -10 /usr/local/openresty/nginx/conf/nginx.conf

cat
用于查看文件,例如查看Linux启动日志文件文件,并标明行号:
cat -Ab /usr/local/openresty/nginx/conf/nginx.conf

touch
用于创建文件,例如创建text.txt文件:
touch text.txt

rm
- 删除文件:
rm text.txt
- 强制删除某个目录及其子目录:
rm -rf testdir/
cp
用于拷贝文件,例如将test1目录复制到test2目录
cp -r /mydata/test1 /mydata/test2
mv
用于移动或覆盖文件:
mv text.txt text2.txt
压缩与解压
tar
- 将/etc文件夹中的文件归档到文件etc.tar(并不会进行压缩):
tar -cvf /data/mydata/etc.tar /etc

- 用gzip压缩文件夹/etc中的文件到文件etc.tar.gz:
tar -zcvf /data/mydata/etc.tar.gz /etc

- 用bzip2压缩文件夹/etc到文件/etc.tar.bz2:
tar -jcvf /data/mydata/etc.tar.bz2 /etc

- 分页查看压缩包中内容(gzip):
tar -ztvf /data/mydata/etc.tar.gz |more -c -10

- 解压文件到当前目录(gzip):
tar -zxvf /data/mydata/etc.tar.gz
- 解压文件到指定目录(gzip):
tar -zxvf /data/mydata/etc.tar.gz -C /mydata/etc
磁盘和网络管理
df
查看磁盘空间占用情况:
df -hT

dh
查看当前目录下的文件及文件夹所占大小:
du -h --max-depth=1 ./*

ifconfig
显示当前网络接口状态:
ifconfig

netstat
- 查看当前路由信息:
netstat -rn

- 查看所有有效TCP连接:
netstat -an

- 查看系统中启动的监听服务:
netstat -tulnp

- 查看处于连接状态的系统资源信息:
netstat -atunp

wget
wget 是一款广泛使用的命令行工具,它允许用户通过HTTP、HTTPS和FTP协议下载网络上的文件。这里有一些基础和高级的使用示例:
基础示例
-  下载单个文件: wget http://example.com/file.zip
-  下载并保存为不同的文件名: wget -O newfilename.zip http://example.com/file.zip
-  下载整个网站: wget --mirror --convert-links --adjust-extension --page-requisites --no-parent http://example.com
高级示例
-  限制下载速度: 
 使用--limit-rate选项来限制下载速度,防止占用过多带宽。wget --limit-rate=100k http://example.com/file.zip
-  断点续传: 
 如果下载过程中断,可以使用-c或--continue选项从中断的地方继续下载文件。wget -c http://example.com/file.zip
-  下载多个文件: 
 创建一个包含多个URL的文本文件(例如urls.txt),然后使用-i选项从该文件读取并下载。wget -i urls.txt
-  使用代理服务器下载: 
 如果你需要通过代理服务器下载,可以配置wget来使用代理。export http_proxy="http://proxyserver:port/" export https_proxy="https://proxyserver:port/" wget http://example.com/file.zip
-  只下载网站的HTML文件而不下载任何其他资源: wget --execute robots=off --recursive --no-parent --no-clobber --html-extension --domains website.com --no-directories http://website.com
文件上传下载
- 安装上传下载工具lrzsz;
yum install -y lrzsz
- 上传文件,输入以下命令XShell会弹出文件上传框;
rz
- 下载文件,输入以下命令XShell会弹出文件保存框;
sz fileName
软件的安装与管理
rpm
RPM是
Red-Hat Package Manager的缩写,一种Linux下通用的软件包管理方式,可用于安装和管理.rpm结尾的软件包。
- 安装软件包:
rpm -ivh nginx-1.12.2-2.el7.x86_64.rpm
- 模糊搜索软件包:
rpm -qa | grep nginx
- 精确查找软件包:
rpm -qa nginx
- 查询软件包的安装路径:
rpm -ql nginx-1.12.2-2.el7.x86_64
- 查看软件包的概要信息:
rpm -qi nginx-1.12.2-2.el7.x86_64
- 验证软件包内容和安装文件是否一致:
rpm -V nginx-1.12.2-2.el7.x86_64
- 更新软件包:
rpm -Uvh nginx-1.12.2-2.el7.x86_64
- 删除软件包:
rpm -e nginx-1.12.2-2.el7.x86_64
yum
Yum是
Yellow dog Updater, Modified的缩写,能够在线自动下载RPM包并安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,非常方便!
- 安装软件包:
yum install nginx
- 检查可以更新的软件包:
yum check-update
- 更新指定的软件包:
yum update nginx
- 在资源库中查找软件包信息:
yum info nginx*
- 列出已经安装的所有软件包:
yum info installed
- 列出软件包名称:
yum list nginx*
- 模糊搜索软件包:
yum search nginx



















