Ubuntu 系统常见操作指令
作为开发者,日常和服务器打交道时,Ubuntu 是最常用的 Linux 发行版之一。本文整理了 Ubuntu 系统中最高频的操作指令,涵盖系统管理、文件操作、网络连接、软件安装等方面。
系统基础信息
查看系统版本
1 2 3 4 5 6 7 8 9 10 11
| lsb_release -a
uname -r
dpkg --print-architecture
uptime
|
查看系统资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| free -h
df -h
du -sh *
cat /proc/cpuinfo | grep "model name" | head -1
top
htop
|
文件和目录操作
基础操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| cd /path/to/directory
cd ..
cd ~
cd -
pwd
|
浏览文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| ls
ls -a
ls -la
ls -lt
ls -R
ls -d */
|
文件权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| chmod 755 file.sh
chmod +x script.sh
chmod -R 755 /path/to/dir
chown user:group file.txt
chown -R user:group /path/to/dir
|
文件操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| touch filename.txt
mkdir dirname
mkdir -p /path/to/nested/dir
cp source.txt destination.txt
cp -r source_dir/ destination_dir/
mv oldname.txt newname.txt
rm filename.txt
rm -f filename.txt
rmdir dirname
rm -rf dirname/
|
文本查看和编辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| cat filename.txt
less filename.txt
head -n 20 filename.txt
tail -n 50 filename.txt
tail -f /var/log/syslog
wc -l filename.txt
|
搜索文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| find . -name "filename.txt"
find . -iname "filename"
find . -type f -name "*.log"
find . -size +100M
find . -mtime -7
which python3
type -a python3
|
用户和权限管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| whoami
who
id username
sudo adduser username
sudo deluser username
passwd username
su - username
sudo command
sudo -l
|
软件包管理 (APT)
更新软件源
1 2 3 4 5 6 7 8
| sudo apt update
sudo apt upgrade
sudo apt dist-upgrade
|
安装/卸载软件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| sudo apt install nginx
sudo apt install nginx git vim
sudo apt reinstall nginx
sudo apt remove nginx
sudo apt purge nginx
sudo apt autoremove
|
搜索和查看
1 2 3 4 5 6 7 8 9 10 11
| apt search nginx
apt show nginx
apt list --installed
dpkg -l nginx
|
系统服务管理
Ubuntu 使用 systemd 管理服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| sudo systemctl status nginx
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
sudo systemctl enable nginx
sudo systemctl disable nginx
systemctl list-units --type=service
systemctl is-active nginx
|
网络操作
查看网络配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ip addr
ip link show
cat /etc/resolv.conf
ss -tlnp
netstat -an
|
网络测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| ping -c 4 google.com
ip route
traceroute google.com
telnet example.com 80
nc -zv example.com 80
dig example.com
wget https://example.com/file.zip
curl -O https://example.com/file.zip
|
SSH 连接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| ssh user@server_ip
ssh -p 2222 user@server_ip
ssh -i ~/.ssh/my_key.pem user@server_ip
ssh user@server_ip "df -h"
ssh-keygen -t rsa -b 4096
ssh-copy-id user@server_ip
|
防火墙 (UFW)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| sudo ufw status
sudo ufw enable
sudo ufw disable
sudo ufw allow ssh
sudo ufw allow 80/tcp
sudo ufw allow from 192.168.1.100
sudo ufw deny 3306/tcp
sudo ufw delete allow 80/tcp
sudo ufw status verbose
|
进程管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| ps
ps aux
pstree
top
htop
pgrep -a python
kill process_id
kill -9 process_id
killall nginx
./script.sh &
jobs
bg
fg
|
压缩和解压
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| tar -cvf archive.tar directory/
tar -xvf archive.tar
tar -czvf archive.tar.gz directory/
tar -xzvf archive.tar.gz
tar -cjvf archive.tar.bz2 directory/
tar -xjvf archive.tar.bz2
zip -r archive.zip directory/
unzip archive.zip
unzip -l archive.zip
|
磁盘操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| lsblk
df -h
sudo mount /dev/sdb1 /mnt
sudo umount /mnt
sudo mkfs.ext4 /dev/sdb1
sudo blkid
UUID=xxx /mnt ext4 defaults 0 2
|
日志查看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| sudo tail -f /var/log/syslog
sudo tail -f /var/log/auth.log
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log
last
sudo lastb
sudo dmesg
sudo dmesg | less
|
定时任务 (Cron)
1 2 3 4 5 6 7 8
| crontab -e
crontab -l
crontab -r
|
Crontab 格式:
1 2 3 4 5 6 7
| * * * * * command │ │ │ │ │ │ │ │ │ └── 星期 (0-7, 0和7都是周日) │ │ │ └──── 月份 (1-12) │ │ └────── 日期 (1-31) │ └──────── 小时 (0-23) └───────── 分钟 (0-59)
|
示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| * * * * * /path/to/script.sh
0 3 * * * /path/to/script.sh
0 6 * * 1 /path/to/script.sh
0 2 1 * * /path/to/script.sh
*/5 * * * * /path/to/script.sh
|
其他常用指令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| date
sudo timedatectl set-timezone Asia/Shanghai
cal
bc
clear
history
printenv
echo $PATH
ln -s /path/to/original /path/to/link
command --help
man command
|
实用技巧
1. 命令行补全
按 Tab 键自动补全命令、文件名、路径。
2. 复用历史命令
↑ / ↓ 浏览历史命令
Ctrl+R 搜索历史命令
!! 执行上一条命令
!n 执行第 n 条历史命令
3. 管道和重定向
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| command > output.txt
command >> output.txt
command 2> error.txt
command > output.txt 2>&1
command1 | command2
cat /var/log/syslog | grep error
cat /var/log/syslog | wc -l
|
4. 后台运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| nohup ./script.sh &
screen -S mysession screen -ls screen -r mysession Ctrl+A D
tmux new -s mysession tmux ls tmux attach -t mysession Ctrl+B D
|
掌握这些常用指令,足以应付日常的 Ubuntu 服务器管理工作。建议收藏本文,需要时随时查阅。
有问题欢迎留言交流!