1、进入系统家目录
#su - root 切换到root用户
#cd ~ 进入root用户家目录
2、进入.ssh文件夹
.ssh文件夹一般是隐藏起来的,可以输入以下命令来查到:
#ll -a 列出当前目录下的所有文件(包含隐藏文件)
这里可以看到/root目录下有.ssh文件夹,进入
#cd .ssh
如果没找到就创建一个文件夹#mkdir .ssh
3、创建密钥对
#cd .ssh 进入.ssh文件夹
#ssh -keygen -t rsa 执行创建密钥对命令
Generating public/private rsa key pair.
Please be patient....Key generation may take a few minutes
Enter file in which to save the key(/root/.ssh/id_rsa):
#按回车保存为://.ssh/id_rsa,即当前用户的私钥
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:
31:03:78:64:4f:8e:9d:a9:31:bf:38:5d:1a:79:08:e6 appnms@T254N0V4
#密钥指纹
The key's randomart image is:
4、测试连接
此时在/root/.ssh/下会有authorized_keys、id_rsa和id_rsa.pub三个文件,id_rsa为私钥,id_rsa.pub为公钥,而authorized_keys则是我们要配置的服务器的公钥。
将原来的authorized_keys文件备份:
#mv authorized_keys authorized_keys_bak
将id_rsa.pub文件改为authorized_keys文件:
#cp id_rsa.pub authorized_keys
输入sftp root@IP地址尝试连接,能连接上sftp服务,成功。