vagrant 默认是使用用户名和密钥登陆的,之所以在vagrantfile
目录输入vagrant ssh
可以登陆,是因为vagtant创建虚拟机的时候生成了非对称加密的公私钥
,公钥上传到了虚拟机,私钥保存在宿主机中。
查看私钥地址,我们使用ssh连接试一下,我这里已经创建好了一台虚拟机并处于运行状态。
# 查看当前虚拟机的配置,可以看到`PasswordAuthentication no` 密码登陆被禁止,`IdentityFile` 私钥地址
Administrator@ADMINIS-AL9GNP3 MINGW64 /e/code/test/vm/data2
$ vagrant ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile C:/Users/Administrator/.vagrant.d/boxes/traefik_box/0/virtualbox/vagrant_private_key
IdentitiesOnly yes
LogLevel FATAL
Administrator@ADMINIS-AL9GNP3 MINGW64 /e/code/test/vm/data2
# 尝试使用密钥文件和用户名登陆
$ ssh -i C:/Users/Administrator/.vagrant.d/boxes/traefik_box/0/virtualbox/vagrant_private_key vagrant@127.0.0.1 -p 2222
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-210-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
UA Infra: Extended Security Maintenance (ESM) is not enabled.
0 updates can be applied immediately.
88 additional security updates can be applied with UA Infra: ESM
Learn more about enabling UA Infra: ESM service for Ubuntu 16.04 at
https://ubuntu.com/16-04
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
Last login: Thu Jun 16 07:31:35 2022 from 10.0.2.2
vagrant@swarm4:~$
进入虚拟机启动密码登陆
# PasswordAuthentication 修改为yes
sudo sed -i "52c PasswordAuthentication yes" /etc/ssh/sshd_config
# 重启加载最新配置
sudo service sshd restart
尝试密码登陆成功
Administrator@ADMINIS-AL9GNP3 MINGW64 /e/code/test/vm/data2
$ ssh vagrant@127.0.0.1 -p 2222
vagrant@127.0.0.1's password:
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-210-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
UA Infra: Extended Security Maintenance (ESM) is not enabled.
0 updates can be applied immediately.
88 additional security updates can be applied with UA Infra: ESM
Learn more about enabling UA Infra: ESM service for Ubuntu 16.04 at
https://ubuntu.com/16-04
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
Last login: Mon Jun 20 06:39:45 2022 from 10.0.2.2
vagrant@swarm4:~$
ssh小技巧
如何愉快的使用ssh登陆虚拟机呢?每次都需要进入vagrantfile目录执行命令执行命令,或者ssh指定密钥/输入密码什么的都挺麻烦。我这里介绍一个简单方法,只需要轻轻的配置一下,就可以愉快的玩耍了。
找到宿主机的用户目录下面的
.ssh
目录,我的是在这里C:\Users\Administrator\.ssh
,新建config.txt
文件,把vagrant ssh-config
输入的配置复制粘贴到config.txt
,修改Host
为一个自己喜欢的名称。Host vagrant-ssh HostName 127.0.0.1 User vagrant Port 2222 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile C:/Users/Administrator/.vagrant.d/boxes/traefik_box/0/virtualbox/vagrant_private_key IdentitiesOnly yes LogLevel FATAL
登陆测试,可以在任何目录执行此命令,这个命令是全局的,这样就简单多了
# 使用ssh Host(之前指定的Host) Administrator@ADMINIS-AL9GNP3 MINGW64 /e/code/test/vm/data2 $ ssh vagrant-ssh Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.4.0-210-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage
- updates can be applied immediately.
additional security updates can be applied with UA Infra: ESM
Learn more about enabling UA Infra: ESM service for Ubuntu 16.04 at
https://ubuntu.com/16-04
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
Last login: Mon Jun 20 06:44:22 2022 from 10.0.2.2
vagrant@swarm4:~$
One comment
windows ssh 推荐工具 https://mobaxterm.mobatek.net/