平时实操 Linux 服务器时踩过不少坑开了很多窗口和ai解决问题,遇到各类报错后一边自己摸索解决方案,一边参考行业前辈分享的技术资料,汇总了一些实操可用、能直接复制复用的命令整理汇总成本文。内容覆盖文件操作、日志重定向管理、Tomcat 服务部署、用户权限配置、网络故障排查等运维全场景,每条知识点附带核心说明、可直接运行的示例命令与避坑注意事项,非常适合 Linux 新手快速查阅上手。文中若存在表述有误或不完善的地方,欢迎各位大佬批评指正。(文中有很多不错的科普文)

1. unzip unrar tar 解压到指定目录的方法

unzip src -d targetdir unrar x src targetdir tar -xf src -C targetdir

2. war包

以 user.war 为例,部署在tomcat8_9090/webapps/ 启动tomcat后会自动解压出 /user 目录 /user 目录下的 /WEB-INF/lib 目录通常是更新替换jar包的地方

3. mv -vf ./ ../

# 将当前目录的文件移动到上级目录
mv -vf ./ ../

     mv [选项]... [-T] 源文件 目标文件
     mv [选项]... 源文件... 目录
     mv [选项]... -t 目录 源文件...
         
-   `--backup[=CONTROL]` : 为每个已存在的目标文件创建备份
-   `-b` : 类似–backup 但不接受参数
-   `-f`, `--force` : **覆盖前不询问**
-   `-i`, `--interactive` : **覆盖前询问**
-   `-n`, `–nechoo-clobber` : 不覆盖已存在文件 如果您指定了-i、-f、-n 中的多个,仅最后一个生效。
-   `--strip-trailing-slashes` : 去掉每个源文件参数尾部的斜线
-   `-S`, `--suffix=SUFFIX` : 替换常用的备份文件后缀
-   `-t`, `--target-directory=DIRECTORY` : 将所有参数指定的源文件或目录 移动至 指定目录
-   `-T`,`--no-target-directory` : 将目标文件视作普通文件处理
-   `-u`, `--update` : 只在源文件文件比目标文件新,或目标文件不存在时才进行移动
-   `-v`, `--verbose` : 详细显示进行的步骤

4. linux比较两个配置文件的差异命令

cd /data/user/user/web/tomcat8_9090/conf

diff context.xml  zw_context.xml

vimdiff context.xml  zw_context.xml

5.添加sudo权限

visudo 
就是操作 /etc/sudoers 文件,但是是系统提供的功能,任何直接修改底层文件的操作都是不规范的
/ALL 文档中搜索ALL关键词,找到root用户,回车在下方添加普通用户
root    ALL=(ALL)       ALL
user    ALL=(ALL)       ALL

注:
chmod  
chown  
chgrp  
useradd  
gpasswd  
  
等这些命令实际上都是 操作 /etc/group /etc/passwd 这些文件
这些文件不到万不得已 系统炸了  
不要手动碰

6.清理Linux系统缓存

top  实时查看资源占用情况
free -h 按GB单位查看内存/缓存等使用情况

以xx项目主机为例:
1.停止tomcat
2.sudo -i 切换root权限
3.sync 该命令多执行几遍
4.echo 3 > /proc/sys/vm/drop_caches
5.启动tomcat

7.修改服务器时间

1.date -s "16:42:01" 修改系统时间
2.hwclock --show    查看硬件时间
3.hwclock --systohc 硬件时间与系统时间同步
4.hwclock --show

8.-的必要性

sudo -i
su - root
su - user

不带 - 是没切环境变量的切换用户
切之前是root用户和root的环境变量, 切成user后是user用户和root的环境变量

带了-后
切之前是root用户和root的环境变量, 切成user后是user用户和user的环境变量

9.服务器ssh跳转

#登录主机 192.168.0.10
ssh user@192.168.0.12
#输入user的密码即可跳转


文件传输
文件传输需要先上传到192.168.0.10:22,再用scp传输到42
拷贝回来 scp user@192.168.0.10:/home/user/apps/gcc-rpm.tar.gz ./
拷贝过去 scp ./tomcat8_9091.tar.gz user@192.168.0.10:/home/user/user/web

10.端口常见问题应用

netstat -lnpt

tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      23450/java 
可以看到该进程占用了8080端口
ll /proc/23450/
查看该进程的目录
10.1端口转发

端口转发的几种方法https://cloud.tencent.com/developer/article/1688152

10.2判断端口通不通的几种方法
telnet ssh wget nc

启动一个web服务器,提供端口.

[wyq@localhost ~]$ python -m SimpleHTTPServer 8080 Serving HTTP on 0.0.0.0 port 8080 ... # python3 python -m http.server 8081 
1 .使用telnet判断

telnet ip port

失败:

[wyq@localhost ~]$ telnet localhost 9000
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused

成功:

[wyq@localhost ~]$ telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
2 .使用ssh判断

ssh -v -p port username@ip

-v 调试模式(会打印日志).
-p 指定端口
username可以随意

失败: "Connection refused"表示端口不可用

[wyq@localhost ~]$ ssh -v -p 9000 wyq@localhost OpenSSH_6.4, OpenSSL 1.0.1e-fips 11 Feb 2013 debug1: Reading configuration data /home/wyq/.ssh/config debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 51: Applying options for * debug1: Connecting to localhost [127.0.0.1] port 9000. debug1: connect to address 127.0.0.1 port 9000: Connection refused ssh: connect to host localhost port 9000: Connection refused

成功:“Connection established” 表示已经连上端口

[wyq@localhost ~]$ ssh -v -p 8080 wyq@localhost OpenSSH_6.4, OpenSSL 1.0.1e-fips 11 Feb 2013 debug1: Reading configuration data /home/wyq/.ssh/config debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 51: Applying options for * debug1: Connecting to localhost [127.0.0.1] port 8080. debug1: Connection established. debug1: identity file /home/wyq/.ssh/id_rsa type 1 debug1: identity file /home/wyq/.ssh/id_rsa-cert type -1 debug1: identity file /home/wyq/.ssh/id_dsa type -1 debug1: identity file /home/wyq/.ssh/id_dsa-cert type -1 debug1: identity file /home/wyq/.ssh/id_ecdsa type -1 debug1: identity file /home/wyq/.ssh/id_ecdsa-cert type -1 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_6.4 debug1: ssh_exchange_identification: <head> debug1: ssh_exchange_identification: <title>Error response</title> debug1: ssh_exchange_identification: </head> debug1: ssh_exchange_identification: <body> debug1: ssh_exchange_identification: <h1>Error response</h1> debug1: ssh_exchange_identification: <p>Error code 400. debug1: ssh_exchange_identification: <p>Message: Bad request syntax ('SSH-2.0-OpenSSH_6.4'). debug1: ssh_exchange_identification: <p>Error code explanation: 400 = Bad request syntax or unsupported method. debug1: ssh_exchange_identification: </body> ssh_exchange_identification: Connection closed by remote host

3 .使用wget判断

wget ip:port

失败:

[wyq@localhost ~]$ wget localhost:9000 --2014-08-22 13:36:42-- http://localhost:9000/ 正在解析主机 localhost (localhost)... 127.0.0.1 正在连接 localhost (localhost)|127.0.0.1|:9000... 失败:拒绝连接。

成功:

[wyq@localhost ~]$ wget localhost:8080 --2014-08-22 13:37:22-- http://localhost:8080/ 正在解析主机 localhost (localhost)... 127.0.0.1 正在连接 localhost (localhost)|127.0.0.1|:8080... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:2770 (2.7K) [text/html] 正在保存至: “index.html” 100%[======================================>] 2,770 --.-K/s 用时 0s 2014-08-22 13:37:22 (105 MB/s) - 已保存 “index.html” [2770/2770])

        提供端口服务,则使用了tcp协议,上面是以web服务器为例。如果服务器是更简单的tcp服务器,三个工具同样适用.

判断端口的方法
1、netstat –an
2、telnet ip port
3、ssh -v -p 9000 wyq@localhost
Connection established 成功
Connection refused 失败
4、wget ip:port

11.路径别名

        在 Linux 服务器上,可以使用符号链接(Symbolic Link)或者在配置文件中创建别名来将路径 /zhkcdata 设置为 /data 别名。
符号链接的方法如下:

ln -s /zhkcdata /data

上述命令将在当前目录下创建一个名为 /data 的符号链接,它指向 /zhkcdata 路径。现在,可以通过 /data 这个别名来访问 /zhkcdata 目录。
        如果希望这个别名在系统重启后仍然生效,可以将上述命令添加到启动脚本中,如 /etc/rc.local,或者为特定用户添加到自定义的启动脚本中。
        

12.环境变量

一文彻底搞懂linux全局环境变量生效顺序_51CTO博客_linux环境变量生效命令一文彻底搞懂linux全局环境变量生效顺序,请认准正版地址:http://blog.51cto.com/zpf666https://blog.51cto.com/zpf666/2334770

/etc/profile: 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行,并从/etc/profile.d目录的配置文件中搜集shell的设置.
/etc/bashrc: 为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.

//用户级别的环境变量,用户可以覆盖全局变量
~/.bash_profile: 每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件.
~/.bashrc: 该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该文件被读取.
~/.bash_logout: 当每次退出系统(退出bash shell)时,执行该文件.

/etc/profile中设定的变量(全局)的可以作用于任何用户,
而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是"父子"关系.

~/.bash_profile 是交互式、login 方式进入 bash 运行的
~/.bashrc 是交互式 non-login 方式进入 bash 运行的
通常二者设置大致相同,所以通常前者会调用后者。

13.追踪路由traceroute

linux
# traceroute安装和使用测试端口联通https://blog.csdn.net/ximenjianxue/article/details/100078747

windows
tracert ip
tracert 192.168.0.12

Logo

openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构

更多推荐