目录

    aliyun esc云服务器搭建

    系统版本

    • Ubuntu 24.04.2 LTS

      # 查看版本
      cat /etc/os-release
      # 或
      lsb_release -a
      

    新建用户

    • 新服务器通过 aliyun 远程连接登录用户默认是 ecs-assist-user ,推荐新建用户,用于平时的使用

      sudo adduser [用户名]
      sudo useradd -m -s /bin/bash [用户名] # -m 创建家目录,-s 指定默认shell
      sudo passwd newuser # 然后为 newuser 设置密码
      sudo usermod -aG sudo [用户名] # 将用户加入 sudo 组
      
      su - [用户名] # 切换到新用户,需要输入新用户的密码
      sudo whoami # 尝试执行sudo命令
      

    使用 SSH 登录

    • 如果每次使用服务器都要登录到 aliyun 中进行远程连接的话还是比较麻烦的,推荐使用 SSH 进行远程登录,在本地终端中使用命令行工具就可以进行连接。

    查看 SSH 服务状态

    • 查看是否安装:

      ssh -V
      
      • 返回如 OpenSSH_9.6p1 Ubuntu-3ubuntu13, OpenSSL 3.0.12 则是已经安装。
      • 返回如 command not found 则是未安装。
    • 安装 OpenSSH (已安装请略过)

      • Ubuntu / Debian

        sudo apt update && sudo apt install openssh-client openssh-server
        
      • Fedora / CentOS

        sudo dnf install openssh-clients openssh-server
        
    • 查看 SSH 服务状态

      sudo systemctl status ssh
      
      • 返回结果中注意 Active:Active: active (running) 则为运行中, Active: inactive (stop) 则为未运行。
    • 启用 SSH 服务

      sudo systemctl start ssh
      
    • 开机自启动 SSH 服务 (建议)

      sudo systemctl enable ssh
      
    • 查看 SSH 服务端口 (默认是 22 。如果连接有问题也可以排查一下阿里云中是否有开放此端口,默认是开放了22)

      sudo ss -tlnp | grep ssh
      
      • 返回如

        LISTEN 0      4096         0.0.0.0:22         0.0.0.0:*    users:(("sshd",pid=859,fd=3),("systemd",pid=1,fd=89))
        LISTEN 0      4096            [::]:22            [::]:*    users:(("sshd",pid=859,fd=4),("systemd",pid=1,fd=90))
        
    • 查看 SSH 服务完整配置

      sudo cat /etc/ssh/sshd_config
      
      • 其中有如 Port [端口号] 则SSH端口就是 [端口号] ,以 # 开头的行为注释,不用理会。

    使用用户名及密码登陆

    • 在登录之前,建议先查看一下 SSH 服务的配置

      sudo cat /etc/ssh/sshd_config
      
      • 新的服务器中,似乎默认是关闭了 root 登录以及密码登录,也可能和系统的版本有关
      PermitRootLogin no             # 是否允许 root 登录
      PasswordAuthentication no      # 是否允许密码登录
      PubkeyAuthentication yes       # 是否允许公钥登录
      
    • PasswordAuthenticationno ,则使用 ssh [用户名]@[服务器 ip] 登录会报 permission deny

    • 若对文件 /etc/ssh/sshd_config 进行修改的话,需要重启 SSH 服务才可生效

      sudo systemctl restart ssh  # 重启 SSH 服务
      
    • 使用root登录的话记得为root用户设置密码,并且将上述文件中的 PermitRootLogin 设置为 yes 并重启服务

    • 使用户名密码登录

      ssh [用户名]@[服务器 ip]
      # 输入用户密码
      

    在本地终端中生成 SSH 密钥

    • 生成密钥

      ssh-keygen -t ed25519 -C "aliyun"
      # 响应
      # Generating public/private ed25519 key pair.
      # Enter file in which to save the key ([user_root_path 用户根目录]/.ssh/id_ed25519):[此处输入路径后确认将使用输入的目录]
      # Enter passphrase for "[user_root_path 用户根目录]/.ssh/id_ed25519" (empty for no passphrase):[此处可为空]
      # Enter same passphrase again:[同上]
      
    • 获取公钥

      cat [user_root_path 用户根目录]/.ssh/id_ed25519.pub
      # ssh-ed25519 AAAAC*************** aliyun