Ubuntu修改ssh免密登陆默认端口

  • Post author:
  • Post category:其他




ssh免密登陆步骤

第一步:在本地机器上使用ssh-keygen产生公钥私钥对

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:+CzS29+WU8L4yTW/QAkm2t/SzEqqadqBr2mHlNBute8 root@u171
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|                 |
|    .    . o     |
|   . . oo o . .  |
|    o +.S. o o   |
|     *.+  o O +  |
|    +.+.+  * % o |
|     ++*o.o.X . .|
|    .+*=+E.o.. ..|
+----[SHA256]-----+

第二步:用ssh-copy-id将公钥复制到远程机器中(可能需要输入目标机器密码)

$  ssh-copy-id -i .ssh/id_rsa.pub  用户名字@192.168.x.xxx

注:报错

Permission denied, please try again.

解决方案

1、修改ssh配置文件vim /etc/ssh/sshd_config,设置为允许root远程登录:

2、找到PermitRootLogin prohibie-password 修改为:PermitRootLogin yes 即可。

3、保存退出,重启ssh服务/etc/init.d/ssh restart,回车,输入密码即可。

第三步: 登录到远程机器不用输入密码

$  ssh 用户名字@192.168.x.xxx



修改默认密码

查看目录

$ ls /etc/ssh
moduli      sshd_config       ssh_host_dsa_key.pub  ssh_host_ecdsa_key.pub  ssh_host_ed25519_key.pub  ssh_host_rsa_key.pub
ssh_config  ssh_host_dsa_key  ssh_host_ecdsa_key    ssh_host_ed25519_key    ssh_host_rsa_key          ssh_import_id

修改配置文件sshd_config和ssh_config ,将port(默认22)改为新值(如:3322)

#Port 22
Port 3322

重启服务

$ systemctl restart sshd



版权声明:本文为u011095039原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。