ssh密钥登录和免密认证

创建密钥对

[root@host ~]$ ssh-keygen  <== 建立密钥对
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): <== 按 Enter
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): <== 输入密钥锁码,或直接按 Enter 留空
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:
0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08 root@host

密钥锁码在使用私钥时必须输入,这样就可以保护私钥不被盗用。当然,也可以留空,实现无密码登录。

现在,在 root 用户的家目录中生成了一个 .ssh 的隐藏目录,内含两个密钥文件。id_rsa 为私钥,id_rsa.pub 为公钥

在服务器上安装公钥

[root@host ~]$ cd .ssh
[root@host .ssh]$ cat id_rsa.pub >> authorized_keys

修改文件权限

[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh

设置 SSH,打开密钥登录功能

编辑 /etc/ssh/sshd_config 文件,进行如下设置:

PubkeyAuthentication yes   //打开密钥认证
PasswordAuthentication no   //关闭密码登录

重启sshd服务

service sshd restart

ssh连接时使用私钥id_rsa登录即可

ssh免密登陆(A机器登录到B机器免密)

ssh免密登陆(A机器登录到B机器免密)
A生成密钥
ssh-keygen -t rsa
将A的公钥复制到B的.ssh/authorized_keys
scp .ssh/id_rsa.pub 172.16.1.5:/root/.ssh/authorized_keys
在B机器上修改 authorized_keys访问权限
chmod 600 .ssh/authorized_keys

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,JGit使用ssh认证需要进行以下几个步骤: 1. 生成ssh和私 您可以在本地使用ssh-keygen命令生成ssh和私,例如: ``` ssh-keygen -t rsa -C "your_email@example.com" ``` 这里的“your_email@example.com”是您的邮箱地址,执行命令后,系统会提示您输入保存对的文件名及码等信息。 2. 将公添加到git服务器上 将生成的公添加到Git服务器上的authorized_keys文件中,这里以GitHub为例,进入GitHub网站,点击右上角头像,选择“Settings”,在左侧菜单栏中选择“SSH and GPG keys”,点击“New SSH key”,将生成的公复制到Key文本框中,点击“Add SSH key”按钮即可。 3. 在JGit中使用ssh协议克隆代码库 在JGit中使用ssh协议克隆代码库时,需要设置ssh协议的URL,例如: ``` git@github.com:your_username/your_repository.git ``` 这里的“your_username”是您的GitHub用户名,“your_repository”是您的代码库名称,您也可以将这个URL放到URI对象中,然后通过CloneCommand对象进行克隆操作。在克隆之前,您需要设置JschConfigSessionFactory对象,该对象是用于创建JschSession对象的工厂,您可以在其中设置ssh码等信息。 代码示例: ``` String gitUrl = "git@github.com:your_username/your_repository.git"; String privateKey = "/path/to/your/private_key"; String passphrase = "your_passphrase"; // 设置JschConfigSessionFactory JschConfigSessionFactory sessionFactory = new JschConfigSessionFactory() { @Override protected void configure(Host hc, Session session) { // 设置ssh JschSession jschSession = (JschSession) session; jschSession.setConfig("StrictHostKeyChecking", "no"); jschSession.setIdentity(privateKey, passphrase); } }; // 克隆代码库 Git.cloneRepository() .setURI(gitUrl) .setTransportConfigCallback(transport -> { SshTransport sshTransport = (SshTransport) transport; sshTransport.setSshSessionFactory(sessionFactory); }) .setDirectory(new File("/path/to/your/local/repository")) .call(); ``` 希望以上信息能够帮助到您。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值