服务器运维
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的根目录授权给用户 myuser2chown -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 -lsfdisk -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 表示查看当前目录使用的总空间大小,0表示文件夹深度,0即为只查看当前目录du -h --max-depth=1 user 表示查看user目录下使用的总空间大小,1表示user目录下的文件夹深度,即为只查看两级目录df -TH 表示查看磁盘空间分配大小查看进程所在目录
1
2
ll /proc/19901/cwd19901 为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 AppAfter=network.target # 指定依赖(如网络就绪后启动) [Service]User=root # 以哪个用户运行(可选)Group=root # 用户组(可选)Type=forkingExecStart=/usr/local/elasticsearch/bin/elasticsearchRestart=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/bashecho "请输入maven根目录:"read mvn_pathecho "maven 根目录是:$mvn_path"ssh 终端远程链接服务器
1
2
3
4
// 默认22端口号ssh -t username@ip_address// 指定端口号ssh -t username@ip_address -p portfree 命令查看内存
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 -c1
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 d查找类型为目录(d)-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秒"