Ubuntu Server 常用命令行 - Ubuntu 20.04 LTS

自从CentOS Linux被红帽收购CentOS官方宣布很快即将停止更新,取而代之的是CentOS Stream,一但切换到CentOS Stream,就意味着使用者成了RHEL版本发布前的测试员。

所以推荐使用 Ubuntu Linux 取而代之。

### 网络

```bash# 从所有接口获取IP地址
networkctl status

# 列出主机所有IP地址
hostname -I

# 启用/禁用接口
ip link set <interface> up
ip link set <interface> down

# 管理防火墙规则
sudo ufw enablesh  # 启用防火墙
sudo ufw status    # 列出规则
sudo ufw allow <port>    # 开放端口
sudo ufw deny <port>     # 关闭端口

# 通过SSH远程连接
ssh <user>@<host IP><e>```</e>

### 软件包

```bash# 搜索软件包
apt search <string>
snap find <string>

# 列出可用更新
apt list --upgradable

# 应用所有可用更新
sudo apt update && sudo apt upgrade

# 从 Ubuntu archive 安装软件包
sudo apt install <package>

# 从 snap store 安装软件包
sudo snap install <package>

# 使用 apt-file 命令查看文件是哪个软件包提供的,解决缺少依赖利器。
sudo apt install apt-file
sudo apt-file update
apt-file <filename or command><e>```</e>

### 文件

```bash# 列出文件
ls

# 列出文件并且包含权限和日期信息
ls -al

# 常用文件操作
touch <filename>                  # 创建空文件
echo "<content>" > <filename>     # 将内容 <content> 写入文件
echo "<content>" >> <filename>    # 追加内容到文件
cat <file>                        # 查看文件内容
cp <file> <target filename>       # 复制文件
mv <file> <target directory/filename>     # 移动/重命名文件
rm <file>                         # 删除文件

# 创建目录
mkdir <directory>

# 创建多层目录
mkdir -p <directory1>/<directory2>

# 删除目录
rm -r <directory>

# 快速文件搜索
locate <q>
locate \*.ps  # 例如寻找以 '.ps' 结尾的文件 - 先运行 'updatedb' 命令

# 在文件中搜索字符串
grep <string> <filename>

# 在目录中递归搜索字符串
grep -Iris <string> <directory>

# 查找最近 n 分钟内修改过的文件
find <directory> -mmin -<n> -type f
find . -mmin -5 -type f      # 例如查找最近5分钟内修改过的文件

# 在文件中打印第 <n> 列的值,<separator> 是分隔符
col<n> “<separator>” <filename>
col2 “,” foo.csv     # 例如打印 foo.csv 第二列的值

# 分页显示文件
less <filename>

# 显示文件前 n 行
head -n <n> <filename>

# 显示文件后 n 行
tail -n <n> <filename>

# 跟踪文件内容的增长
tail -f <filename>

# 文件压缩打包
zip -r <target>.zip <source dir>            # 打包为 zip 文件
tar cvzf <target>.tar.gz <source dir>       # 打包为 tar.gz 文件

# 文件解压缩
unzip <zip file>              # 解压 zip 文件   
tar zxvf <tar.gz file>        # 解压 tar.gz 文件

# 复制文件到远程服务器
scp <filename> <user@server>:<destination>
scp config.yaml admin@192.168.0.1:/config    # 例如复制 config.yaml 到 192.168.0.1 服务器的 /config 目录

# 从远处服务器递归复制文件到本地
scp -r <user@server>:<source> <destination>
scp -r admin@192.168.0.1:/config /tmp        # 例如复制远程服务器 192.168.0.1 的 /config 目录到本地 /tmp 目录下

### 系统

```bash# 获取系统内核版本
uname -r

# 查看磁盘使用率
df -h

# 获取内存使用率
cat /proc/meminfo
free -m

# 获取系统时间
timedatectl status

# 设置系统时区
timedatectl list-timezones
sudo timedatectl set-timezone <zone>

# 获取系统所有正在运行的服务
systemctl --state running

# 启动/停止服务
service <service> start/stop
systemctl start/stop <service>

# 监视服务的最新日志
journalctl -u <service> --since now -f

# 获取系统最近登录信息
last

# 显示正在运行的进程
htop

# 按 ID 终止进程
kill <process id>

# 按名称终止进程
pkill <process name>

# 在后台运行命令
<command> &

# 显示后台运行命令任务
jobs

# 将后台运行命令任务调置前台
fg <n><e>```</e>

### 安全

```bash# 查看正在登录的用户
w

# 获取用户的密码过期时间
chage -l <user>

# 设置用户密码过期时间
sudo chage <user>

# 禁用用户
sudo passwd -l <user>

# 解禁用户
sudo passwd -u <user>

# 列出打开的端口和相关的进程
sudo netstat -tulpn<e>```</e>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值