掌握核心命令如Linux的ls/cd/grep及Windows的powershell,熟练运用管道、权限管理(chmod)、进程监控(ps/
服务器操作系统命令行基础
命令行核心概念
术语 | 说明 |
---|---|
Shell | 命令行解释器(如Bash、Zsh、PowerShell),负责解析用户输入指令 |
终端/控制台 | 用户与服务器交互的界面(如SSH客户端、Web终端、物理控制台) |
PATH环境变量 | 系统搜索可执行文件的路径集合,影响命令直接调用 |
基础操作命令
Linux/Unix系统:
echo "Hello World"
:输出文本到终端date
:显示当前日期时间hostname
:查看服务器主机名uname -a
:查看系统内核版本信息history
:查看最近执行过的命令历史
Windows Server:
echo Hello World
:需配合powershell
或cmd.exe
systeminfo
:查看系统详细信息hostname
:显示当前计算机名ver
:查看Windows版本号powershell Get-History
:查看PowerShell命令历史
文件与目录管理
核心命令对比表
功能 | Linux/Unix | Windows (CMD) | Windows (PowerShell) |
---|---|---|---|
查看目录 | ls -l | dir | Get-ChildItem |
切换目录 | cd /var/www | cd C:inetpubwww | Set-Location |
创建目录 | mkdir logs | md logs | New-Item -ItemType Directory |
复制文件 | cp source.txt dest/ | copy source.txt dest | Copy-Item |
删除文件 | rm -f error.log | del /f error.log | Remove-Item |
文件权限 | chmod 755 script.sh | 无直接对应(通过ACL) | Set-Acl |
高级文件操作
- 查找文件:
- Linux:
find /var/log -name *.log
- Windows:
dir /s /b C:WindowsLogs*.log
- Linux:
- 压缩解压:
- Linux:
tar -czvf archive.tar.gz /home/user
- Windows:
Compress-Archive -Path .data -DestinationPath .data.zip
- Linux:
- 查看:
head -n 10 access.log
(查看前10行)tail -f error.log
(实时追踪日志)less /etc/nginx/nginx.conf
(分页查看配置文件)
系统监控与维护
资源监控命令
功能 | Linux | Windows |
---|---|---|
CPU使用率 | top /htop | wmic cpu get loadpercentage |
内存使用 | free -h | systeminfo | findstr /C:"总的物理内存" |
磁盘使用 | df -h | wmic logicaldisk get size,freespace,caption |
网络状态 | ifconfig /ip a | ipconfig |
进程列表 | ps aux | tasklist |
服务管理
- Linux系统:
- 查看服务状态:
systemctl status nginx
- 重启服务:
sudo systemctl restart mysqld
- 启用开机自启:
systemctl enable httpd
- 查看服务状态:
- Windows服务:
- 查看服务:
sc query State
- 启动服务:
net start W3SVC
- 停止服务:
net stop W3SVC
- 查看服务:
网络配置与诊断
IP配置命令
平台 | 查看IP | 修改DNS | 测试连通性 |
---|---|---|---|
Linux | ip addr show | nmcli con mod "eth0" ipv4.dns "8.8.8.8" | ping -c 5 google.com |
Windows | ipconfig | netsh interface ip set dns "以太网" static 8.8.8.8 | ping -n 5 www.baidu.com |
端口检测工具
- Linux:
- 查看监听端口:
netstat -tulnp
- 检查端口占用:
lsof -i :8080
- 查看监听端口:
- Windows:
- 查看TCP连接:
netstat -ano
- 终止端口进程:
taskkill /PID 1234 /F
- 查看TCP连接:
权限与用户管理
用户操作命令
功能 | Linux | Windows |
---|---|---|
创建用户 | useradd tom | net user tom /add |
修改密码 | passwd tom | net user tom 123456 |
删除用户 | userdel tom | net user tom /delete |
切换用户 | su admin | runas /user:admin cmd |
权限管理
- Linux文件权限:
- 查看权限:
ls -l /etc/passwd
- 修改权限:
chmod 750 script.sh
- 更改所有者:
chown root:root /etc/nginx/nginx.conf
- 查看权限:
- Windows ACL:
- 查看权限:
icacls C:test.txt
- 赋予权限:
icacls C:test.txt /grant User:(R)
- 查看权限:
软件安装与包管理
包管理器对比表
功能 | Linux (Debian/Ubuntu) | Linux (RedHat/CentOS) | Windows |
---|---|---|---|
更新软件源 | apt update | yum repolist all | Add-Repository |
安装软件包 | apt install nginx | yum install httpd | Install-Package |
卸载软件 | apt purge samba | yum remove vsftpd | Uninstall-Package |
查询已安装包 | dpkg --get-selections | rpm -qa | Get-InstalledModule |
编译安装示例
# Linux编译Nginx tar -zxvf nginx-1.20.1.tar.gz cd nginx-1.20.1 ./configure --with-http_ssl_module make && make install
自动化脚本与任务计划
Cron定时任务(Linux)
# 每天凌晨备份数据库 0 2 * * * /usr/bin/mysqldump -u root -p db_name > /backup/$(date +%F).sql
计划任务(Windows)
# 每小时清理临时文件 $action = New-ScheduledTaskAction -Execute 'CleanTemp.bat' $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddHours(1) -RepetitionInterval (New-TimeSpan -Minutes 60) Register-ScheduledTask -TaskName "TempCleaner" -Action $action -Trigger $trigger
FAQs常见问题解答
Q1:如何通过命令行远程连接服务器?
A:
- Linux服务器:使用SSH协议,命令为
ssh username@server_ip
,若使用密钥认证需配置~/.ssh/id_rsa
私钥。 - Windows服务器:使用PowerShell的
Enter-PSSession
或第三方工具如ssh.exe
,示例:ssh admin@192.168.1.100
。 - 故障排查:检查防火墙是否开放22端口,确认用户权限,使用
ssh -v
查看调试信息。
Q2:服务器运行缓慢时如何定位问题?
A:
- 检查CPU负载:使用
top
/htop
查看高占用进程,或用mpstat
分析多核使用情况。 - 内存诊断:运行
free -m
查看可用内存,检查是否有进程泄漏(如pmap
分析内存映射)。 - 磁盘I/O:通过
iostat
查看磁盘读写速率,使用df
确认磁盘剩余空间。 - 网络流量:执行
iftop
实时监控网络带宽,检查是否存在异常连接。 - 日志分析:查看
/var/log/syslog
或Windows事件日志,寻找错误提示。
小编有话说
命令行是服务器运维的基本功,但真正掌握需要理解命令背后的逻辑。grep
不仅是搜索工具,结合管道符()能实现日志过滤、数据分析等复杂操作,建议新手从模拟环境(如Docker容器)开始练习,逐步尝试生产环境的只读操作,每个命令的--help
参数和man page
文档都是最好的老师,遇到问题时不要害怕使用ctrl+C
中断任务,安全永远是
到此,以上就是小编对于“服务器操作系统命令行”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。
【版权声明】:本站所有内容均来自网络,若无意侵犯到您的权利,请及时与我们联系将尽快删除相关内容!
发表回复