程序员の奇妙冒险

返回
服务器运维 Linux
2025-04-03 14:06 阅读:64 评论:0

Linux 常用命令以及操作集合

查看登录认证记录 CentOS 7为例,执行命令tail -f /var/log/secure可实时查看 查看iptables设置的规则 iptables -L INPUT -n --line-numbers 保存iptables设置的规则 service iptables save 清空iptab

查看登录认证记录

  • CentOS 7为例,执行命令tail -f /var/log/secure可实时查看

查看iptables设置的规则

  • iptables -L INPUT -n --line-numbers

保存iptables设置的规则

  • service iptables save

清空iptables规则

  • iptables -F INPUT

阻止指定IP访问

  • iptables -I INPUT -s 127.0.0.1 -j DROP 进行访问阻止

删除指定规则的行号

  • iptables -D INPUT 5,5即为行号,行号可以通过iptables -L INPUT -n --line-numbers命令查看

查看系统发行版本

1
cat /etc/os-release

同步系统时间

1
2
vi /var/spool/cron/root # 添加以下内容,/var/spool/cron目录下可能没有root文件,创建一个即可
0 * * * * /usr/sbin/ntpdate time1.aliyun.com >> /tmp/autontpdate 2>&1 # 每小时同步一次时间

tar.gz打包命令

1
tar -zcvf .tar.gz ./

tar.gz解压命令

1
tar -zxvf .tar.gz

指定用户运行脚本(这里使用 es 用户 运行sonar.sh脚本)

1
su - es /usr/local/sonarqube-7.6/bin/linux-x86-64/./sonar.sh start

立即重启

1
shutdown -r now

创建用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 创建用户
useradd myuser1
// 修改密码
passwd myuser1
 
useradd myuser2
// 修改密码
passwd myuser2
// 一行命令修改密码
echo "root:2023321" | chpasswd
// 将root用户密码修改为 2023321
 
// 将 myuser1的根目录授权给用户 myuser2
chown -R myuser2 myuser1/

删除文件夹

  • 命令:rm -rf

删除文件

  • 命令:rm -f

查找进程是否启动

1
2
// 查看HMaster 和 HRegionServer进程
jps | grep -E 'HMaster|HRegionServer'

删除文件内容并指定忽略文件夹

1
$ find /Users/wency_cai/soft/WebProjects/test -type f -not -path "/Users/wency_cai/soft/WebProjects/test/my/*" -exec rm {} \;

查看ip地址

1
2
3
4
5
// 命令
ifconfig
 
// 以下命令也能列出IP地址
ip add

查看硬盘

1
2
fdisk -l
sfdisk -s

查看当前目录下所有目录占有大小

1
du   -sh    *

查看指定目录剩余磁盘空间

1
df -hl

查看哪个目录占用空间最大

1
du -hsx * | sort -rh | head -10

查看目录占用磁盘空间大小

1
2
3
4
5
du -hd0
du -hd1
du -h --max-depth=0 使00
du -h --max-depth=1 user user使1user
df -TH

查看进程所在目录

1
2
ll /proc/19901/cwd
19901 PID

查看文件大小

1
du -sh

ubuntu 卸载软件

1
2
apt-get remove --purge 
apt-get autoremove --purge 

开机自动启动

  • 在/etc/systemd/system目录下创建elasticsearch.service文件
1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=elasticsearch App
After=network.target # 指定依赖(如网络就绪后启动)
 
[Service]
User=root # 以哪个用户运行(可选)
Group=root # 用户组(可选)
Type=forking
ExecStart=/usr/local/elasticsearch/bin/elasticsearch
Restart=on-failure # 失败时自动重启
 
[Install]
WantedBy=multi-user.target # 多用户模式启动时启用
1
2
3
sudo systemctl daemon-reload # 重新加载配置
sudo systemctl enable elasticsearch.service # 设置开机自启
sudo systemctl start elasticsearch.service # 立即启动服务

等待用户输入

1
2
3
4
#!/bin/bash
echo "请输入maven根目录:"
read mvn_path
echo "maven 根目录是:$mvn_path"

ssh 终端远程链接服务器

1
2
3
4
// 默认22端口号
ssh -t username@ip_address
// 指定端口号
ssh -t username@ip_address -p port

free 命令查看内存

1
2
3
4
5
6
7
8
9
10
11
total 
 
used 使
 
free 
 
shared 使
 
buff/cache  buffer  cache 使
 
available   使

top 命令查看进程

1
2
3
4
// 查看前20几个进程
top
// 指定查看某个进程
top -p pid

查看CPU核数

1
cat /proc/cpuinfo| grep "cpu cores"| uniq

查看逻辑CPU个数

1
cat /proc/cpuinfo| grep "processor"| wc -l

查看物理CPU个数

1
cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l

查看CPU型号

1
cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c
1
2
3
4
5
6
7
8
9
10
11
# 总核数 = 物理CPU个数 X 每颗物理CPU的核数
# 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数
 
# 查看物理CPU个数
cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l
 
# 查看每个物理CPU中core的个数(即核数)
cat /proc/cpuinfo| grep "cpu cores"| uniq
 
# 查看逻辑CPU的个数
cat /proc/cpuinfo| grep "processor"| wc -l

遍历删除指定文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
find . -name  target  -type d -print -exec rm -rf {} \;
.
target
-type dd
-print
-exec rm -rf {} \; find
find . -type d -empty  -print -exec rm -rf {} \;
find . -type d -empty
// 进入根目录 遍历删除所有 target 文件夹
find . -name 'target' -type d  | xargs rm -rf
 
 
#删除3天前打包压缩的备份数据
find -mtime +3 -name "*.tar.gz" -exec rm -rf {} \;

查找当天创建的文件

1
find -daystart -ctime 0 -print

查询开机时长

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
 
# 获取系统运行时间(秒)
uptime_seconds=$(cat /proc/uptime | cut -d. -f1)
 
# 转换为小时、分钟和秒
hours=$((uptime_seconds / 3600))
minutes=$(( (uptime_seconds % 3600) / 60 ))
seconds=$((uptime_seconds % 60))
 
# 打印开机时间
echo "系统已开机:$hours小时$minutes分钟$seconds秒"
评论 (0)
暂无评论 来抢沙发吧~

目录

SpringWonderland

探索 Spring 生态的奇妙世界

2025-04-03 14:06