# linux操作指南 ## 常用系统操作 ### 创建新用户 示例用户名:food * 添加用户 ```shell adduser food ``` * 修改用户密码 ``` passwd food ``` ### 密钥登录 * 示例用户:food * 要先登录要配置密钥登录的用户,这样会更方便 配置步骤: 1. 生成密钥 ```shell ssh-keygen ``` 2. 把id_rsa.pub重命名为authorized_keys ```shell mv /home/food/.ssh/id_rsa.pub /home/food/.ssh/authorized_keys ``` 注:使用该方法,无需设置相应文件(authorized_keys)和文件夹(.ssh)的权限 说明: * id_rsa是私钥文件,要自己保管 * id_rsa.pub是公钥文件 * authorized_keys 是公钥文件集,即用于存放多个公钥信息 ### SSH安全配置 安全配置文件位置: /etc/ssh/sshd_config 配置步骤: 1. 修改配置文件 ```shell vi /etc/ssh/sshd_config ``` 2. 修改配置项 ```shell 修改监听的端口号 Port 20022 启用密匙验证 PubkeyAuthentication yes 禁止ssh密码登陆 PasswordAuthentication no 禁止root账户远程登录 PermitRootLogin no 禁止密码为空的用户登录 PermitEmptyPasswords no ``` 3. 保存修改并关闭文件 ```shell :wq ``` 4. 重启ssh服务 ```shell service ssh restart ``` ### 开启BBR算法 步骤: 1. 进入/etc/sysctl.d目录,该目录下可以创建系统配置文件 ```shell cd /etc/sysctl.d ``` 2. 创建配置文件(格式:以**.conf**结尾) ```shell vi bbr.conf ``` 3. 输入配置项 ```shell net.core.default_qdisc=fq net.ipv4.tcp_congestion_control=bbr ``` 4. 保存 ```shell :wq ``` 5. 让配置项生效 ```shell sysctl -p sysctl net.ipv4.tcp_available_congestion_control ``` 也可以重启生效 ```shell reboot ``` 6. 验证生效,执行如下命令,看到tcp_bbr的字眼则为配置成功 ```shell lsmod | grep bbr ``` ### 防火墙设置 注: * 只在alpine上使用过 * 防火墙规则和docker的使用有冲突,导致docker服务失效 步骤: 1. 使用iptables命令查看是否已安装 ```shell iptables ``` 安装防火墙 ```shell apk add iptables ``` 2. 添加开机自启动 ```shell rc-update add iptables ``` 3. 配置防火墙规则 | 端口 | 类型 | 用途 | 策略 | | -------- | ------------ | ----- | ---------- | | 10022 | TCP | SSH | 允许出入站 | | 80 | TCP | HTTP | 允许出入站 | | 443 | TCP | HTTPS | 允许出入站 | | | ICMP | ICMP | 允许出入站 | | 任意 | TCP/UDP/ICMP | | 允许出站 | | 本地任意 | TCP/UDP/ICMP | | 允许出入站 | | 任意 | TCP/UDP/ICMP | | 禁止入站 | 具体命令 ```shell iptables -A INPUT -p tcp --dport 10022 -j ACCEPT && \ iptables -A OUTPUT -p tcp --sport 10022 -j ACCEPT && \ iptables -A INPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT && \ iptables -A OUTPUT -p tcp --sport 80 -m state --state NEW,ESTABLISHED -j ACCEPT && \ iptables -A INPUT -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT && \ iptables -A OUTPUT -p tcp --sport 443 -m state --state NEW,ESTABLISHED -j ACCEPT && \ iptables -A INPUT -p icmp --icmp 8 -j ACCEPT && \ iptables -A OUTPUT -p icmp --icmp 0 -j ACCEPT && \ iptables -P INPUT DROP && \ iptables -P FORWARD DROP && \ iptables -P OUTPUT DROP && \ iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT && \ iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT && \ iptables -A OUTPUT -j ACCEPT ``` 4. 保存防火墙规则 ```shell service iptables save ``` 5. 查看防火墙规则 ``` iptables -L -n ``` 清楚防火墙操作 ``` iptables -F && iptables -X && iptables -Z ``` 注:全清除前最好先关闭防火墙,否则会导致ssh连接断开 参考:[Alpine Linux 一键构建LNSP&防火墙规则](https://yuu.ink/article/131/) ### IPV6设置 配置步骤: 1. 在HE.net中申请IPV6隧道 2. 修改网络配置 /etc/network/interfaces,添加如下内容 ```shell auto he-ipv6 iface he-ipv6 inet6 v4tunnel address xxx:xxx:xxx:xxx::x netmask 64 endpoint xxx.xx.xxx.x local xxx.xxx.xx.xxx ttl 255 gateway xxxx:xxx:xxx:xxx::x ``` 注: * 该配置内容在申请IPV6隧道时获得 * 通过该种配置方式,能够开机自动配置 3. 测试命令 ```shell ping6 ipv6.google.com ``` 注:能ping通即配置成功 ### 设置时区与开启时间同步 配置步骤 1. (可选)显示当前时区信息 ```shell timedatectl ``` 2. (可选)显示可选时区信息 ```shell timedatectl list-timezones ``` 注:没有看到beijing字眼,只看到Asia/Hong_Kong 3. 设置时区 ```shell timedatectl set-timezone Asia/Hong_Kong ``` 4. 启用时间同步 ```shell timedatectl set-ntp true ``` 注:如果时间不准确,可以执行这一步 5. 查看当前时间 ```shell date ``` 如果前面配置成功,就能看到正确的时间信息了 ### 设置系统自动重启 配置步骤: 1. 编辑定时任务 ```shell crontab -e ``` 2. 添加定时重启任务:每天凌晨4点重启 ```shell 0 4 * * * /sbin/reboot ``` ## 软件安装 ### 安装v2ray #### docker安装 使用镜像:teddysun/v2ray:latest #### 脚本安装 服务端安装:使用一键安装脚本 ```shell bash <(curl -s -L https://git.io/v2ray.sh) ``` #### win客户端安装 客户端安装:使用v2rayN作为图形界面,可以直接上github上下载,也可以使用scoop安装 ``` scoop install v2rayN ``` #### 参考 [2022年最新VPS一键安装脚本搭建V2ray详细图文教程](https://github.com/xiaoming2028/FreePAC/wiki/2022%E5%B9%B4%E6%9C%80%E6%96%B0VPS%E4%B8%80%E9%94%AE%E5%AE%89%E8%A3%85%E8%84%9A%E6%9C%AC%E6%90%AD%E5%BB%BAV2ray%E8%AF%A6%E7%BB%86%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B) ## 常用软件配置 ### Shadowsocks强制谷歌走ipv6隧道 Shadowsocks配置文件位置:/etc/shadowsocks-libev/config.json 配置步骤: 1. 修改配置文件 ```shell vi /etc/shadowsocks-libev/config.json ``` 2. 在配置文件中添加如下配置项 监听所有端口 ```shell "server":["[::0]", "0.0.0.0"] ``` IPV6优先 ```shell "ipv6_first":true ``` 3. 测试地址:www.google.com/sorry/index 注:如果显示IPv6则配置成功