Skip to content

Instantly share code, notes, and snippets.

@MarshalW
Last active January 2, 2024 05:53
Show Gist options
  • Save MarshalW/e5007e7c73bef17c620cda2595d386fb to your computer and use it in GitHub Desktop.
Save MarshalW/e5007e7c73bef17c620cda2595d386fb to your computer and use it in GitHub Desktop.
Ubuntu Server 备查

Ubuntu Server 备查

apt镜像设置

替换源

清华源:https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/

# 替换源
sed -i s@/archive.ubuntu.com/@/mirrors.tuna.tsinghua.edu.cn/@g /etc/apt/sources.list

设置代理

apt设置代理

sudo nano /etc/apt/apt.conf.d/proxy.conf
                       
#Acquire {
#  HTTP::proxy "http://ip:port";
#  HTTPS::proxy "http://ip:port";
#}

Acquire::http::Proxy "http://ip:port";
Acquire::https::Proxy "http://ip:port";
Acquire::http::Proxy {
  mirrors.aliyun.com DIRECT;
  mirrors.tuna.tsinghua.edu.cn DIRECT;
}

在脚本中执行:

sudo touch /etc/apt/apt.conf.d/proxy.conf
echo 'Acquire::socks::proxy "socks5://127.0.0.1:1337/";' | sudo tee -a /etc/apt/apt.conf.d/proxy.conf

Ubuntu Server 常用工具列表

  • htop
  • ctop, docker container top
  • fping,更好性能的ping,基于icmp协议,能同时监控多个,结合watch使用
  • httpie,很好的命令行http工具
  • jq, curl输出json格式化和过滤
  • tree
  • speedtest-cli
  • iperf3
  • glances
  • tldr,比看man的提示效率高
  • tracepath,traceroute的替代方案
  • mtr,traceroute的替代方案,实时统计

mount U盘

# 找到设备名称
sudo fdisk -l

# 创建mount目录
sudo mkdir /media/test

# 挂载 U 盘到目录
sudo mount -t exfat /dev/sda1 /media/test

# 卸载u盘
sudo umount /dev/sda1

监控 usb 设备,比如 u 盘插入

监控 usb:

# watch -n.5 lsusb

Every 0.5s: lsusb                                                                                                        pve: Fri Nov 17 16:47:10 2023

Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 003: ID 152d:0583 JMicron Technology Corp. / JMicron USA Technology Corp. JMS583Gen 2 to PCIe Gen3x2 Bridge
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

设置 sshd

# 查看sshd服务状态
$ sudo systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-01-19 16:38:08 CST; 1 weeks 1 days ago
       Docs: man:sshd(8)
             man:sshd_config(5)
   Main PID: 738 (sshd)
      Tasks: 1 (limit: 76919)
     Memory: 11.7M
     CGroup: /system.slice/ssh.service
             └─738 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
# 编辑配置文件
sudo vim /etc/ssh/sshd_config
 
# 重启服务
sudo systemctl restart ssh

设置ip地址

编辑文件,/etc/netplan/00-installer-config.yaml

network:
    version: 2
    renderer: networkd
    ethernets:
        eth0:
            addresses:
              - 192.168.0.8/24
              - 192.168.0.9/24
            dhcp4: no
            gateway4: 192.168.0.1
            nameservers:
                addresses: [192.168.0.1]

相关命令:

# 检查生成是否有问题
sudo netplan --debug generate

# 让新设置生效
sudo netplan apply

# 重启
sudo reboot

卸载和清除 snap

参见:https://askubuntu.com/a/1251819

# stop snapd services
sudo systemctl stop snapd && sudo systemctl disable snapd
# purge snapd
sudo apt purge snapd
# remove no longer needed folders
rm -rf ~/snap
sudo rm -rf /snap /var/snap /var/lib/snapd /var/cache/snapd /usr/lib/snapd

显示cpu温度

sudo apt install lm-sensors
watch sensors

安装和使用 sar 实时监控系统性能

# 安装
sudo apt install sysstat -y

# 允许数据收集的配置
sudo sed -i 's/false/true/g' /etc/default/sysstat
sudo sed -i 's/5-55\/10/*\/2/g' /etc/cron.d/sysstat

# 重启服务
sudo service sysstat restart

# 检查page faults
sudo sar -B 1

制作 Ubuntu Server 启动U盘

macOS插入u盘,找到设备名:

diskutil list

卸载u盘:

diskutil unmountDisk /dev/disk3

使用dd命令写入:

sudo dd if=ubuntu-20.04.2-live-server-amd64.iso of=/dev/rdisk3 bs=64m
Password:
18+1 records in
18+1 records out
1215168512 bytes transferred in 49.705693 secs (24447270 bytes/sec)

设置时区

sudo timedatectl set-timezone Asia/Shanghai

自定义 dns

sudo su -
rm /etc/resolv.conf
echo "nameserver 192.168.0.222" | tee /etc/resolv.conf

systemctl disable systemd-resolved.service
systemctl stop systemd-resolved

reboot now

磁盘分区/格式化/加载/设置label

# 查看磁盘硬件是否正确加载
sudo fdisk -l
# 或者
sudo parted -l

# 分区
sudo parted /dev/nvme0n1
mklabel gpt
mkpart primary ext4 0% 100%

# 格式化
sudo mkfs.ext4 -L mystorage /dev/nvme0n1p1

# 挂载
sudo mkdir /mnt/m2ssd
echo "LABEL=storage3 /mnt/m2ssd ext4 defaults 0 2" | sudo tee -a /etc/fstab
sudo mount -a

# 查询 label
lsblk --output NAME,LABEL /dev/nvme0n1 
# 设置 label
sudo e2label /dev/nvme1n1 bus_4t

fio 测试 ssd 磁盘性能

sudo apt-get update && sudo apt-get install fio -y
sudo fio --name=randwrite \
    --ioengine=libaio \
    --iodepth=16 \
    --rw=randwrite \
    --bs=4k \
    --size=1G \
    --numjobs=4 \
    --time_based \
    --runtime=10m \
    --group_reporting \
    --norandommap \
    --filename=/dev/nvme0n1,/dev/nvme1n1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment