腾讯云服务器部署node应用-账号权限配置

351 阅读1分钟

准备一个腾讯云服务器

安全组要开启80端口和443端口

启用root账号

腾讯云主机ubuntu系统默认的账号名是ubuntu,没有开通root账号,如果要创建root账号的话,就执行

sudo passwd -u root

再执行sudo passwd root 系统会让你重新设置root的密码

修改ssh登录的配置,即/etc/ssh/sshd_config文件,修改为允许root登录,sudo vim /etc/ssh/sshd_config

将PermitRootLogin后面的prohibit-password改为yes

重启ssh服务使刚才的ssh配置的修改生效,执行命令sudo service ssh restart

可以使用root登陆了

解决问题Host key verification failed.

$ ssh ubuntu@49.235.172.41
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:Et5eTdLfR+0n/ZfrkbDigmdFm0nqvP20ejBGI/+giJg.
Please contact your system administrator.
Add correct host key in /Users/mac/.ssh/known_hosts to get rid of this message.
Offending ED25519 key in /Users/mac/.ssh/known_hosts:15
ECDSA host key for 49.235.172.41 has changed and you have requested strict checking.
Host key verification failed.

重装了系统后用ssh登陆云主机的时候,本地terminal报错了。修改/Users/mac/.ssh/known_hosts把公网ip对应的那段都删除,然后重新登录即可。用atom或者vscode修改比较快atom /Users/mac/.ssh/known_hosts

创建发布账号

创建一个新的账号来使用,root账号权限太大。创建发布者账号需要root权限。 adduser kim 设置自己的密码之后,其它设置就一直按Enter,使用默认即可。 在root账号下执行gpasswd -a dzhiqin sudo给kim账号以sudo权限。在腾讯云主机的系统内,不能访问不同账号下的文件或者路径,sudo也不行。

免密登陆

如果不想用免密登陆可以跳过

SSH还可以用非对称加密来实现免密登录,避免每次登陆的时候都要输入密码。非对称加密会有两个钥匙,公钥和私钥。

  • 通过公钥加密的密文,只有私钥能打开
  • 通过私钥加密的密文,只有公钥能打开 于是我们就可以在本机保存私钥,把公钥放到远端电脑去,登录时,服务器会送一个锁给用户,用户用自己的私钥解锁,解开了就表示认证成功。
    在配置git的时候已经有在本机产生过ssh key,如果没有的话,执行ssh-keygen -t rsa ,否则直接执行cat ~/.ssh/id_rsa.pub,公钥就显示在终端上。
    然后登录dzhiqin,创建ssh/authorized_keys文件,并且把公钥内容贴进去,具体步骤:
    mkdir ~/.ssh
    touch ~/.ssh/authorized_keys
    nano ~/.ssh/authorized_keys把公钥贴进去,保存退出
    chmod 700 ~/.ssh
    chmod 644 ~/.ssh/authorized_keys 完成后退出当前登陆,再重新登录一次,可以看到这个时候已经不需要手输密码了。
$ ssh kim@49.235.172.41
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-157-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage
New release '18.04.5 LTS' available.
Run 'do-release-upgrade' to upgrade to it.


Last login: Tue Feb  2 11:16:28 2021 from 116.22.18.253
kim@VM-0-12-ubuntu:~$

安全起见,可以还原root登陆限制,禁止ssh登陆,取消账号的密码登陆,直接用免密登陆。 执行vi /etc/ssh/sshd_config编辑

UsePAM yes
Ciphers aes128-ctr,aes192-ctr,aes256-ctr
MACs hmac-sha1,umac-64@openssh.com,hmac-ripemd160
KexAlgorithms diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group1-sha1,curve25519-sha256@libssh.org

#AddressFamily any
AddressFamily inet
PasswordAuthentication no

把PasswordAuthentication设置为no,编辑保存完成后,重启ssh服务使得新配置生效,然后就无法使用口令来登录ssh了。

kim@VM-0-12-ubuntu:~$ systemctl restart sshd.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'ssh.service'.
Multiple identities can be used for authentication:
 1.  ubuntu,,, (ubuntu)
 2.  ,,, (kim)
Choose identity to authenticate as (1-2): 2
Password:

修改配置会要求输入账号的密码,选择对应账号输入对应密码即可。