华为云服务器折腾记录

最近在做一个关于应用上云的测试,于是看到有华为云的免费服务器试用就注册了一个,记录下操作备忘。

个人用户实名注册即可领取一个低性能的2核4g的服务器,试用15天。领取链接

初始化操作

领取成功后,按照创建步骤即可。如果没有设定登录密码的,在控制台设置即可。弹性公网即为服务器的公网IP,与其链接通信试用这个ip。

远程登录可以试用 CloudShell VNC Xshell shell 等登录,先使用VNC设置初始密码,网络安全组的入站等规则。

此页面也可查询服务器详细一段时间内运行状态

需要的初步操作:

  1. 添加普通用户

    root 用户权限太大,为了以防误操作和开发需要,需要新建一个有root权限的普通用户。

    # 创建用户
    sudo adduser username 
    ## 命令将向你询问一系列的问题。密码是必需的,其他字段都是可选的。
    Enter new UNIX password: 
    Retype new UNIX password: 
    passwd: password updated successfully
    Changing the user information for username
    Enter the new value, or press ENTER for the default
        Full Name []: 
        Room Number []: 
        Work Phone []: 
        Home Phone []: 
        Other []: 
    Is the information correct? [Y/n] 
    # 最后,输入Y确认信息是否正确。
    
    # 如果您希望新创建的用户具有管理权限,请将用户添加到sudo组:
    sudo usermod -aG sudo username
    
    # 您可以使用两个命令行工具来删除用户帐户:userdel和deluser。在Ubuntu上,建议您使用deluser命令,因为它比userdel 更友好。
    
    # 要删除用户而不删除用户文件,请运行:
    sudo deluser username
    
    # 如果要删除并且用户的家目录和邮件使用--remove-home选项:
    sudo deluser --remove-home username
    
  2. ssh公钥免密登录

    我更习惯直接使用ssh登录远程服务器,所以设置免密登录很有必要

    apt-get install  openssh-server openssh-client
    # 如果没有或者需要一个新公私钥的创建命令
    ssh-keygen -t rsa -f ~/.ssh/cloud
    # 复制公钥,需要登录那个用户就复制到哪
    ssh-copy-id -i ~/.ssh/cloud.pub root@弹性公网IP
    
  3. X11图形界面允许

    如果需要符合传输X11协议的图形界面需要确保ssh登录后:

    环境说明: A主机ssh连接B主机,A使用X11服务显示使用B主机的code等界面
    a. 首先确保A可以连接上B主机
    b. 设置主机A运行其他服务器的 X11 界面:
        xhost +
    c. 设置主机A sudo vim /etc/ssh/ssh_config
        ForwardAgent yes
        ForwardX11 yes
        ForwardX11Trusted yes
    d. 设置主机B的 daemon 配置X11转发
        sudo vim /etc/ssh/sshd_config #补充下面这行,我这台服务器是已经配置好的。
        X11Forwarding yes #确保正确后重启ssh daemon
        sudo systemctl restart sshd
    最后连接B主机即可 ssh -X  username@B主机(IP)
    
  4. 更改主机名

    如果你在一个云实例上运行 Ubuntu,并且安装了cloud-init软件包,你也可以编辑/etc/cloud/cloud.cfg文件。这个软件包由云服务器厂商提供,通常默认被安装,并且它可以被用来处理云服务器实例的初始化。如果文件存在于你的系统上,打开它:sudo nano /etc/cloud/cloud.cfg搜索"preserve_hostname”,并且将值从false修改到true。

    #This will cause the set+update hostname module to not operate (if true)
    preserve_hostname: true
    # 保存文件,并且关闭编辑器运行命令
    hostnamectl sethostname YOUR_define_name
    

远程Vscode

虽然服务器自带vim等编辑器,但是还是vscode用的顺手,下面是配置远端登录vscode的记录。(用x11打开的vscode太卡,不推荐如此使用)

打开本地主机的vscode扩展商店,搜索安装 Remote - SSH: Editing Configuration Files ms-vscode-remote.remote-ssh-edit 之后打开此扩展设定远程登录的IP和用户

可是启动远程登录时ssh Server卡在Setting up SSH Host XX: Downloading VS Code Server

重新连接后有detail选项,打开显示的部分日志如下:

[14:30:07.314] Log Level: 2
[14:30:07.317] remote-ssh@0.66.0
[14:30:07.318] linux x64
[14:30:07.321] SSH Resolver called for "ssh-remote+cloud-huawei-root", attempt 1
[14:30:07.322] "remote.SSH.useLocalServer": true
[14:30:07.322] "remote.SSH.path": undefined
[14:30:07.322] "remote.SSH.configFile": undefined
[14:30:07.322] "remote.SSH.useFlock": true
[14:30:07.323] "remote.SSH.lockfilesInTmp": false
[14:30:07.323] "remote.SSH.localServerDownload": auto
[14:30:07.323] "remote.SSH.remoteServerListenOnSocket": false
[14:30:07.323] "remote.SSH.showLoginTerminal": false
[14:30:07.324] "remote.SSH.defaultExtensions": []
[14:30:07.324] "remote.SSH.loglevel": 2
[14:30:07.325] SSH Resolver called for host: cloud-huawei-root
[14:30:07.325] Setting up SSH remote "cloud-huawei-root"
[14:30:07.331] Acquiring local install lock: /tmp/vscode-remote-ssh-fd33cb2f-install.lock
[14:30:07.352] Looking for existing server data file at /home/lk233/.config/Code/User/globalStorage/ms-vscode-remote.remote-ssh/vscode-ssh-host-fd33cb2f-b4c1bd0a9b03c749ea011b06c6d2676c8091a70c-0.66.0/data.json
[14:30:07.356] Using commit id "b4c1bd0a9b03c749ea011b06c6d2676c8091a70c" and quality "stable" for server
[14:30:07.368] Install and start server if needed
[14:30:07.379] PATH: /home/lk233/.local/bin:/home/lk233/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/lk233/.local/bin:/home/lk233/go/bin:/usr/local/go/bin:/home/lk233/.local/bin:/home/lk233/.local/bin
[14:30:07.380] Checking ssh with "ssh -V"
[14:30:07.418] > OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017

[14:30:07.429] askpass server listening on /run/user/1000/vscode-ssh-askpass-40f6502d1f6ea56e214539b4c7f7cd72b715cc8b.sock
[14:30:07.430] Spawning local server with {"serverId":1,"ipcHandlePath":"/run/user/1000/vscode-ssh-askpass-2918dcdc514eef86200f16b5db8a75360fbea57c.sock","sshCommand":"ssh","sshArgs":["-v","-T","-D","45283","-o","ConnectTimeout=15","cloud-huawei-root"],"dataFilePath":"/home/lk233/.config/Code/User/globalStorage/ms-vscode-remote.remote-ssh/vscode-ssh-host-fd33cb2f-b4c1bd0a9b03c749ea011b06c6d2676c8091a70c-0.66.0/data.json"}
[14:30:07.430] Local server env: {"DISPLAY":":0","ELECTRON_RUN_AS_NODE":"1","SSH_ASKPASS":"/home/lk233/.vscode/extensions/ms-vscode-remote.remote-ssh-0.66.0/out/local-server/askpass.sh","VSCODE_SSH_ASKPASS_NODE":"/usr/share/code/code","VSCODE_SSH_ASKPASS_MAIN":"/home/lk233/.vscode/extensions/ms-vscode-remote.remote-ssh-0.66.0/out/askpass-main.js","VSCODE_SSH_ASKPASS_HANDLE":"/run/user/1000/vscode-ssh-askpass-40f6502d1f6ea56e214539b4c7f7cd72b715cc8b.sock"}
[14:30:07.447] Spawned 11142
[14:30:07.653] > local-server-1> Spawned ssh, pid=11150
[14:30:07.659] stderr> OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
[14:30:07.914] stderr> debug1: Server host key: ecdsa-sha2-nistp256 SHA256:TeikcLf0gyEyeAnlAgObhrs6PKRrfQQeW6zLEFgFiA4
[14:30:08.172] stderr> Authenticated to 123.60.23.165 ([123.60.23.165]:22).
[14:30:08.977] > Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-70-generic aarch64)
> 
>  * Documentation:  https://help.ubuntu.com
>  * Management:     https://landscape.canonical.com
>  * Support:        https://ubuntu.com/advantage
> 
>   System information as of Sun Nov 14 14:30:08 CST 2021
> 
>   System load:  0.05               Processes:           99
>   Usage of /:   10.2% of 38.63GB   Users logged in:     0
>   Memory usage: 6%                 IP address for eth0: 192.168.0.20
>   Swap usage:   0%
> 
*****

查了下果然是这个vscode-server包需要科学上网,所以云服务器无法连接上。 只能按照How can I install vscode-server in linux offline类似的安装了。不过里面问题提问的人的是x86架构的包,我使用的是ARM的包。

上面日志可以看到有一句Using commit id “b4c1bd0a9b03c749ea011b06c6d2676c8091a70c” and quality “stable” for server,这个等价替换上面问题的解决方案的commitid即可。其实直接查看主机运行的code 相关进程即可看到:

root@cloud:~# ps aux | grep code
root       507  0.0  0.1  11488  5940 ?        S    14:27   0:00 wget --tries=1 --connect-timeout=7 --dns-timeout=7 -nv -O vscode-server.tar.gz https://update.code.visualstudio.com/commit:b4c1bd0a9b03c749ea011b06c6d2676c8091a70c/server-linux-arm64/stable
root      5321  0.0  0.0   5672   664 pts/0    S+   14:31   0:00 grep --color=auto code

总结下解决方案:

  1. 本地下好对应包 我的版本的 commit id = b4c1bd0a9b03c749ea011b06c6d2676c8091a70c,使用stable版本(inside预览不推荐) 浏览器或wget下载https://update.code.visualstudio.com/commit:b4c1bd0a9b03c749ea011b06c6d2676c8091a70c/server-linux-arm64/stable
  2. scp复制该压缩包上传云服务器 $ scp /home/localuser/vscode-server-linux-arm64.tar.gz user@123.60.23.165:/home/user/
  3. 解压并设定状态 如果之前下载失败了记得把commit对应文件夹清空,或者新建一个。user对应云服务器用户名一般为 /root/.vscode-server-insiders/bin/${commit_id} 或者 /home/user/.vscode-server-insiders/bin/${commit_id}
    tar zxvf ./vscode-server-linux-x64.tar.gz -C /home/user/.vscode-server-insiders/bin/${commit_id} --strip 1
    touch /home/user/.vscode-server-insiders/bin/${commit_id}/0
    

最后,安装完毕,登录成功如下所示

当然,要记得关闭后台更新。