Linux常用命令

这篇博客详细介绍了Linux系统管理的各种实用命令和操作,包括资源管理、基础操作、网络配置、用户和组管理、软件包管理、服务管理、文件及目录操作、磁盘管理和防火墙设置。内容涵盖从查看系统信息到网络诊断、用户权限控制、软件安装及服务配置等全面的运维知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、资源管理
# 查看发行版信息
cat /etc/os-release 
# 查看CPU信息
lscpu 
# 查看USB设备
lsusb
# 查看PCI设备
lspci
# 查看内存信息
free -h 
# 查看资源占用
top 
# 查看进程
ps -ef
# 杀死进程
kill -9 PID

二、基础
# 关机
systemctl poweroff
init 0
# 重启
systemctl reboot
init 6
# 使环境变量生效
source /etc/profile
# 查找命令位置
which shutdown
# 查找命令位置和帮助文件
whereis shutdown
# 开机自动启动脚本
chmod +x /etc/rc.local
vi /etc/rc.local
# 定时任务  
# 分钟(0-59) 小时(0-23) 天(1-31) 月(1-12) 周几(0-6,0代表星期天) 用户 命令
# root用户每12小时执行date>/tmp/date.txt
# * 12 * * *   root date>/tmp/date.txt
vi /etc/crontab
# 别名
alias 'node-2' 'ssh root@192.168.1.1'
# 打印全部别名
alias -p
# -----SSH免密登录
# 生成密钥对
ssh-keygen
# 上传公钥至服务器
ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.25.53.123
三、网络相关
# 查看IP
ip addr
ifconfig
nmcli
# 查看端口
lsof -i
# 查看端口
netstat -nlp
# 查看nmcli管理的连接
nmcli con show
# 配置IP
nmcli con mod eth0 ip4 192.168.0.100/24 gw4 192.168.0.254
# 配置DNS
nmcli con mod eth0 ipv4.dns "223.5.5.5 223.6.6.6"
# 配置网卡为静态IP
nmcli con mod eth0 ipv4.method manual
# 配置网卡自动启动
nmcli con mod eth0 connection.autoconnect yes
# 配置IP地址 网关 DNS 静态IP 自动启动
nmcli con mod eth0 ip4 192.168.1.200/24 gw4 192.168.1.253 ipv4.dns "223.5.5.5 223.6.6.6" ipv4.method manual connection.autoconnect yes
# 访问网址
curl https://mirrors.aliyun.com/repo/Centos-8.repo
# 下载http链接
curl -O https://mirrors.aliyun.com/repo/Centos-8.repo
wget https://mirrors.aliyun.com/repo/Centos-8.repo
# 指定目标下载
curl https://mirrors.aliyun.com/repo/Centos-8.repo -o /tmp/CentOS-Base.repo 
wget https://mirrors.aliyun.com/repo/Centos-8.repo -O /etc/yum.repos.d/CentOS-Base.repo 
# 列出目标提供的共享文件夹
smbclient -L 192.168.2.231 -U 用户名
# 访问共享文件夹
smbclient //192.168.2.231/临时 -U 用户名
# 查看SELinux状态   dnf provides semanage 查看需要的包然后install
sestatus
# 查看SELinux端口上下文   dnf provides semanage 查看需要的包然后install
semanage port -l | grep ssh
# 添加SELinux上下文端口   dnf provides semanage 查看需要的包然后install
semanage port -a -t ssh_port_t -p tcp 10022

四、主机名、语言、时间日期
# 查看主机名
hostnamectl 
# 修改主机名
hostnamectl set-hostname NAME
# 修改临时主机名
hostnamectl --transient set-hostname NAME

# 查看语言环境
localectl status 
# 查看可用的语言环境
localectl list-locales 
# 设备语言环境为UTF-8
localectl set-locale LANG=zh_CN.UTF-8 


# 查看时间 时区
timedatectl 
# 修改时间
timedatectl set-time '2021-12-05 10:25:00' 

# 显示时区列表
timedatectl list-timezones
# 修改时区
timedatectl set-timezone Asia/Shanghai

# 修改NTP服务器配置文件 NTP=XXXX
vi /etc/systemd/timesyncd.conf 
# 开启NTP同步
timedatectl set-ntp yes 
# 显示NTP同步配置
timedatectl show-timesync 
# 显示NTP同步状态
timedatectl timesync-status 
五、用户和组
# 查看用户组
cat /etc/group
# 新建用户组
groupadd hausers
# 查看用户
cat /etc/passwd
# 新建用户并加入用户组  
# -m 自动创建用户主目录 
# -g 加入用户组
# -s 用户的登录shell
# -d 用户主目录
useradd hauser -m -g hausers -s /bin/bash -d /home/hauser
# 设置密码
passwd hauser
# 查看是否有sudo用户组
cat /etc/group | grep sudo
# 用户加入sudo组
usermod -aG sudo hauser
# 编辑sudo配置文件 增加一行 hauser ALL=(ALL) ALL
visudo
# 切换用户
su -l hauser
六、软件包
6.1 dnf(yum)、rpm包管理器
# 安装软件包
dnf install net-tools
# 显示yum仓库列表
dnf repolist
# 搜索包
dnf search postgresql
# 下载软件包
dnf download postgresql
# 下载软件包(包括未安装依赖)
dnf download --resolve postgresql
# 下载软件包(包括所有依赖)
dnf download --resolve --alldeps postgresql
# 查看已安装软件包
dnf list installed
# 删除软件包
dnf remove postgresql
# 清理缓存
dnf clean all
# 更新缓存
dnf makecache
# 查找命令所在包
dnf provides ifconfig
# 查看可用包组
dnf group list hidden
# 查看包组包含软件
dnf group info 'Development Tools'
# 安装软件包组
dnf group install 'Development Tools'
# 删除软件包组
dnf group remove 'Development Tools'
# 添加本地镜像源
dnf config-manager --add-repo file:///mnt/iso

# 查看已安装包
rpm -qa
# 安装rpm包
rpm -ivh net-tools-2.0-0.54.oe1.x86_64.rpm
# 删除软件包
rpm -e net-tools
# 查看软件包文件位置
rpm -ql net-tools
6.2 apt、deb包管理器
# 安装软件包
apt install net-tools
# 查看软件包数量
apt-cache stats
# 查看软件源
apt policy
# 搜索软件包
apt search postgresql-server
# 下载软件包
apt download mariadb-server
# 下载软件包(包含依赖) /var/cache/apt/archives
apt install mariadb-server -d
# 查看已安装包
apt list --installed
# 删除软件包
apt remove net-tools
# 清理缓存
apt clean
# 更新软件源
apt update
# 跳过签名更新软件源
apt update --allow-unauthenticated
# 查找命令所在包 apt install apt-file
apt-file search ifconfig
# 添加光盘源
apt-cdrom -d=/media/cdrom add

# 查看已安装软件包
dpkg -l
# 安装软件包
dpkg -i mariadb-server_10.3.31-0+deb10u1_all.deb
# 删除软件包
dpkg -r mariadb-server
# 查看软件包相关目录
dpkg -L mariadb-server
七、服务管理
# 列出正在运行的服务
systemctl list-units --type service
# 列出所有服务
systemctl list-units --type service --all
# 显示服务状态
systemctl status rsyslog
# 启动服务
systemctl start rsyslog
# 停止服务
systemctl stop rsyslog
# 重启服务
systemctl restart rsyslog
# 开机自动启动服务
systemctl enable rsyslog
# 开机自动启动服务 并立刻启动
systemctl enable --now docker
# 关闭开机自动启动服务
systemctl disable rsyslog
# 重载服务配置文件
systemctl daemon-reload
八、文件和目录
# 创建文件或修改时间属性
touch xxxx.xx
# 创建目录
mkdir -p /xxxxx
# 移动或重命名
mv 源 目标
# 复制
cp -r 源 目标
# 删除
rm -rf 目标
# 创建软连接
ln -s 源 目标
# 显示文件内容
cat xxxx
# 编辑文件
vi xxxx
nano xxxx
# 搜索文件
find 目录 -name "文件名"
# 跟踪文件尾部
tail -f /var/log/messages
# 替换字符串
sed -i 's/目标/被替换/g' 文件名
# grep反向筛选
grep -v '正则'
# grep过滤注释
grep -v '#'
# grep过滤空行
grep -v '^$'
# 修改文件权限
# R=4 W=2 X=1
# + 增加 - 删除 -R 递归
chmod 775 xxxx
# 修改文件所属用户和组
chown -R 用户.用户组 xxxx
# 查看文件大小 只看总数
du -sh
# 查看文件或目录大小
# 参数 -d 目录深度
du -hd1
# 列出当前目录文件
ls -hla
# 解压xxx.tar.gz至/usr/local目录
tar -zxvf xxx.tar.gz -C /usr/local/
# /usr/local目录打包为xxx.tar.gz
tar -zcvf xxx.tar.gz /user/local
# 传输文件
scp 本地文件 用户名@主机:远程文件
# 传输目录
scp -r 本地目录 用户名@主机:远程目录
# 缓存写入硬盘
sync
# 搜索文件  dnf install mlocate 安装包后 updatedb 更新数据库
locate xxxx
九、磁盘管理
# 发现硬盘
# 查看scsi总线
ls /sys/class/scsi_host
# 查看结果 host0
# 扫描host0
echo '- - -' > /sys/class/scsi_host/host0/scan
# 查看磁盘列表
fdisk --list
# 磁盘分区
fdisk /dev/sdb
# 图形化分区工具
cfdisk /dev/sdb
# 格式化分区
mkfs.ext4 /dev/sdb1
# 检查分区
e2fsck -f /dev/sdb1
# 挂载分区
mount 分区 目标
# 查看分区UUID
blkid
# 开机自动挂载
# 增加一行UUID=xxxxxxxx /opt/vdisk0 ext4 defaults 0 0
# <文件系统> <挂载点> <分区类型> <选项> <dump备份> <检验扇区>
nano /etc/fstab
# 取消挂载分区
umount 目标
# 清空硬盘数据
dd if=/dev/zero of=/dev/sdb bs=8192K count=1
# 查看文件系统
df -h
# 扩容分区 
# 安装 dnf install cloud-utils-growpart
# 参数1 硬盘  参数2 分区序号
# xfs_growfs / 重新识别文件系统
# partprobe /dev/sda 刷新硬盘状态(容量等)
growpart /dev/sda 2
十、防火墙
10.1 firewalld防火墙
# 查看规则
firewall-cmd --list-all
# 查询服务
firewall-cmd --get-services
# 放行服务
firewall-cmd --zone=public --add-service=postgresql --permanent
# 放行端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 删除端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent
# 创建服务
firewall-cmd --permanent --new-service=myssh
# 服务添加端口
firewall-cmd --permanent --service=myssh --add-port=10022/tcp
# 查看服务端口
firewall-cmd --permanent --service=myssh --get-ports
# 允许一个或一段IP访问 服务
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.1" service name="myssh" accept"
# 允许一个或一段IP访问
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="3306" accept"
# 删除一个或一段IP访问
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="3306" accept"
# 重载配置
firewall-cmd --reload
10.2 ufw防火墙
# 安装
apt install ufw
# 放行ssh服务
ufw allow ssh
# 放行端口
ufw allow 80/tcp
# 放行一组端口
ufw allow 8000:8006/tcp
# 允许特定地址连接
ufw allow from 8.8.8.8
# 允许一个子网连接
ufw allow from 192.168.1.0/24
# 启用ufw(注意如果通过SSH操作启用前请检查是否打开了ssh端口)
ufw enable
# 查看规则
ufw status numbered
# 删除规则
ufw delete allow 80/tcp
# 重载
ufw reload
# 查看防火墙运行状态
ufw status verbose
10.3 iptable防火墙
# 查看规则
iptables -L
# 放行端口
iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
# 保存
service iptables save
十一、grub引导
# 自动修复引导
grub2-mkconfig -o /boot/grub2/grub.cfg
# 设置开启启动项顺序
grub2-set-default 0
# 查看开机启动项顺序
grub2-editenv list
# 查看引导项
grep 'menuentry ' /boot/grub2/grub.cfg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Halyace

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值