# Ubuntu ## 什么是 Ubuntu Ubuntu 是一个基于 Debian 的 Linux 发行版,Ubuntu有三个正式版本,包括桌面版、服务器版及用于物联网设备和机器人的Core版。 ## 查看 Ubuntu 版本 ```Shell $ lsb_release -a $ cat /etc/os-release ``` ## 安装必备工具 ```Shell $ sudo apt update && sudo apt upgrade -y $ sudo apt install net-tools htop iotop iftop dstat -y ``` ## 设置时区 ```shell $ sudo timedatectl set-timezone Asia/Shanghai ``` 使用 SSH 登录建议使用密钥无密码方式,详细配置可以参考 [[开发笔记/服务器运维/Shell#SSH公钥认证登录]] ## UFW 防火墙 UFW (Uncomplicated Firewall) 是 Ubuntu 提供的一个简单易用的防火墙工具,它的命令用来管理和配置防火墙规则,基于 `iptables` 进行操作。 ```shell $ sudo ufw enable # 启用 UFW $ sudo ufw disable # 禁用 UFW $ sudo ufw status # 检查 UFW 状态 $ sudo ufw allow 22/tcp # 允许特定协议和端口的流量 $ sudo ufw delete allow 22/tcp #删除规则 $ sudo ufw reset # 重置 UFW $ sudo ufw reload # 重载 UFW 配置 $ sudo ufw allow from 192.168.1.100 # 允许某个 IP 地址的所有入站流量 $ sudo ufw allow from 192.168.1.100 to any port 22 # 允许某个 IP 地址的特定端口流量 ``` 阿里云服务器建议关闭系统防火墙,使用网络安全组策略。 ```Shell $ sudo ufw disable ``` ## 修改 IP 地址 静态ip修改步骤如下,其他设置可参考文档 https://ubuntu.com/server/docs/network-configuration 1. 查看网卡信息 `ip a` 2. 修改配置文件 `vim /etc/netplan/00-installer-config.yaml` ```Shell # This is the network config written by 'subiquity' network: ethernets: ens3: addresses: - 172.16.0.8/24 routes: - to: default via: 172.16.0.1 nameservers: addresses: [114.114.114.114, 223.5.5.5, 8.8.8.8] version: 2 renderer: networkd ``` 3. 应用设置 `sudo netplan apply` ## 系统服务 ```Shell $ systemctl status docker # 查看状态 $ systemctl start docker # 启动 $ systemctl stop docker # 停止 $ systemctl restart docker # 重启 $ systemctl enable docker # 开机自启 $ systemctl disable docker # 禁止开机自启 ``` ### 创建系统服务 这里以 Snell 为例 ```shell # 下载shell执行文件 $ wget https://dl.nssurge.com/snell/snell-server-v5.0.1-linux-amd64.zip $ unzip snell-server-v5.0.1-linux-amd64.zip $ chmod +x snell-server $ mv snell-server /usr/local/bin/ # 创建shell服务的配置 $ vim /etc/snell-server.conf # >>> [Snell Server] interface = 0.0.0.0 port = 6160 psk = RANDOM_KEY_HERE # <<< # 创建shell系统服务配置 $ vim /etc/systemd/system/snell.service # >>> [Unit] Description=Snell Proxy Service After=network.target [Service] ExecStart=/usr/local/bin/snell-server -c /etc/snell-server.conf Restart=on-failure RestartSec=5s User=nobody CapabilityBoundingSet=CAP_NET_BIND_SERVICE AmbientCapabilities=CAP_NET_BIND_SERVICE NoNewPrivileges=true [Install] WantedBy=multi-user.target # <<< # 加载服务配置 $ systemctl daemon-reload # 运行服务并设置开机自启 $ systemctl enable snell.service $ systemctl start snell.service # 检查服务状态 $ systemctl status snell.service # 查看服务日志 $ journalctl -u snell.service -f ``` ## 扩容磁盘 1. 查看磁盘容量 ```shell $ df -h $ sudo fdisk -l $ sudo lsblk ``` 2. 使用 `growpart` 扩容 ```shell $ sudo growpart /dev/sda 分区号 ``` 3. 更新逻辑卷大小 ``` $ sudo lvresize --extents +100%FREE --resizefs /dev/mapper/ubuntu--vg-ubuntu--lv ``` ## 开启 bbr 1. 安装软件包 ```Shell $ sudo apt install wget apt-transport-https gnupg2 software-properties-common ``` 2. 检查是否开启 bbr ```Shell $ sysctl net.ipv4.tcp_congestion_control ``` 3. 如果没有开启 bbr 需要修改配置文件 ```Shell $ sudo vim /etc/sysctl.conf >>> net.core.default_qdisc=fq 和 net.ipv4.tcp_congestion_control=bbr $ sudo sysctl -p ``` ## 更换软件源 ```shell # 备份 $ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak # 修改 source.list 文件,内容替换成下面的 $ sudo vim /etc/apt/sources.list # >>> # 官方源 deb http://archive.ubuntu.com/ubuntu/ jammy main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu/ jammy-updates main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu/ jammy-backports main restricted universe multiverse deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse # 阿里云源 deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse # <<< # 更新 sudo apt-get update ``` # Debain ## 什么是 Debain Debain 是众多 Linux 发行版(如 Ubuntu、Kali Linux、Raspberry Pi OS 等)的基。 ## 安装时不创建用户 在 Debian 12 安装过程中,系统默认要求创建一个普通用户,并禁止直接使用 `root` 账户登录。但可以通过以下方法跳过普通用户创建,仅保留 `root` 账户: 1. 按照常规步骤进行安装。 2. 在 Root 密码界面,设置 `root` 密码。 3. 在创建普通用户界面,直接留空用户名并继续。 4. 按 `ESC` 退出用户名设置,跳过该步骤,进入时间设置界面。 这样,系统将仅创建 `root` 账户,无需额外的普通用户。 # 常用命令 ## chmod 修改权限 可以用来修改文件或目录的权限,`-R` 选项表示递归(recursive) ```Shell $ chmod -R 700 filename ``` 700这三个数字分别对应三个权限位,也可以用符号表示,常见的权限有: - rw------- (600) 只有拥有者有读写权限。 - rw-r--r-- (644) 只有拥有者有读写权限;而属组用户和其他用户只有读权限。 - rwx------ (700) 只有拥有者有读、写、执行权限。 - rwxr-xr-x (755) 拥有者有读、写、执行权限;而属组用户和其他用户只有读、执行权限。 - rwx--x--x (711) 拥有者有读、写、执行权限;而属组用户和其他用户只有执行权限。 - rw-rw-rw- (666) 所有用户都有文件读、写权限。 所有用户都有读、写、执行权限。 ## chown 修改所有者 用于修改文件或目录的所有者和所属组 ```Shell $ chown -R user2:group2 dir1 ``` ## find 查找 ```shell # 查找大文件 $ find . -type f -size +800M | xargs du -hm | sort -nr ``` ## grep 搜索内容 ```shell # 查看 nginx 进程 $ ps aux | grep nginx ``` ## which 文件位置 ```shell # 查找 nginx 文件位置 $ which nginx ``` ## du 磁盘 ```shell # 查找大目录 $ du -hm --max-depth=1 | sort -nr | head -10 ``` ## scp 文件传输 ```Shell # 上传 $ scp -P <port> ./apps.tar.gz root@<ip>:~/apps.tar.gz # 下载 $ scp -P <port> root@<ip>:~/apps.tar.gz ./apps.tar.gz ``` ## tar 文件压缩 ```Shell # 压缩 $ tar czvf filename.tar.gz filename # 解压 $ tar xzvf filename.tar.gz # apt install zip unzip # zip -r -e ssh.zip .ssh ``` # SSH SSH(Secure Shell)是一种用于在不安全的网络上安全登录和其他安全网络服务的协议。它主要用于远程登录到其他计算机和传输文件,同时保护通信安全。以下是关于 SSH 的详细介绍: ## 开启 ssh 和 root 登录 ```Shell # 安装ssh服务并启动 $ sudo apt install openssh-server -y $ sudo systemctl enable --now ssh $ sudo systemctl status ssh # 开启root登录 $ sudo passwd # 设置root密码 $ sudo vim /etc/ssh/sshd_config # >>> PermitRootLogin yes # 修改ssh端口 $ sudo vim /etc/ssh/sshd_config # >>> Port 5550 # 修改ssh连接间隔,解决闲置过久自动断开问题 $ sudo vim /etc/ssh/sshd_config # >>> ClientAliveInterval 60 和 ClientAliveCountMax 10 #重启ssh服务 $ sudo service sshd restart ``` ## 生成 SSH 密钥对 在生成过程中,系统会提示你输入保存密钥的位置和一个可选的密码。你可以按回车键使用默认位置(通常是 `~/.ssh/id_rsa`)并选择是否设置密码。 ```shell $ ssh-keygen -t rsa -b 4096 -C "[email protected]" ``` ## 设置 `id_rsa` 和 `id_rsa.pub` 文件权限 ```shell $ chmod 600 ~/.ssh/id_rsa $ chmod 644 ~/.ssh/id_rsa.pub $ chmod 700 ~/.ssh ``` ## SSH公钥认证登录 实现无密码登录的方法有多种,以下是常见的几种方法: 1. 将公钥复制到远程服务器 ```Shell $ ssh-copy-id -p <port> root@<ip> ``` 2. 远程服务器手动配置 ```Shell $ mkdir ~/.ssh $ vi ~/.ssh/authorized_keys $ chmod 700 ~/.ssh $ chmod 600 ~/.ssh/authorized_keys ``` ## 代理转发登录 ```Shell $ ssh-add ~/.ssh/id_rsa $ ssh-add -l $ ssh -A user@remote-server ```