Vagrantのログインユーザを変更する

1. ユーザの作成と公開鍵の設置

- ユーザ作成

```
$ vagrant ssh

$ sudo -s
# useradd -m ansible
```

- sudoの設定

```
# cp -p /etc/sudoers.d/{vagrant,ansible}
# sed -e 's/vagrant/ansible/g' /etc/sudoers.d/ansible
ansible ALL=(ALL) NOPASSWD:ALL
# sed -i 's/vagrant/ansible/g' /etc/sudoers.d/ansible
```

- 公開鍵の設置


```
## scp用にconfigファイルを作成
$ vagrant ssh-config > vagrant_sshconfig

## VM内にコピー
$ scp -F vagrant_sshconfig /Users/tshu1/.ssh/id_ed25519_ansible.pub default:~/

## VMにログインし、公開鍵を設置する
# mkdir /home/ansible/.ssh && chmod 700 /home/ansible/.ssh
# mv id_ed25519_ansible.pub /home/ansible/.ssh/authorized_keys
# chmod 400 /home/ansible/.ssh/authorized_keys && chown -R ansible:ansible /home/ansible

# ls -laR /home/ansible/
/home/ansible/:
total 24
drwxr-xr-x 3 ansible ansible 4096 Jan  3 03:10 .
drwxr-xr-x 5 root    root    4096 Jan  3 03:09 ..
-rw-r--r-- 1 ansible ansible  220 Feb 25  2020 .bash_logout
-rw-r--r-- 1 ansible ansible 3771 Feb 25  2020 .bashrc
-rw-r--r-- 1 ansible ansible  807 Feb 25  2020 .profile
drwx------ 2 ansible ansible 4096 Jan  3 03:11 .ssh

/home/ansible/.ssh:
total 12
drwx------ 2 ansible ansible 4096 Jan  3 03:11 .
drwxr-xr-x 3 ansible ansible 4096 Jan  3 03:10 ..
-r-------- 1 ansible ansible  103 Jan  3 03:05 authorized_keys
```

2. Vagrantfileの変更


```diff
diff --git a/Vagrantfile b/Vagrantfile
index 259ec02..78b6909 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -1,3 +1,5 @@
 Vagrant.configure("2") do |config|
   config.vm.box = "ubuntu/focal64"
+  config.ssh.username = "ansible"
+  config.ssh.private_key_path = "/Users/tshu1/.ssh/id_ed25519_ansible"
 end

```

- reloadの実施

```
$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'ubuntu/focal64' version '20201210.0.0' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: ansible
    default: SSH auth method: private key
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
[default] GuestAdditions 6.1.16 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/tshu1/ghq/github.com/tshu1/ubuntu-2004
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
```


3. ログイン確認

```
$ vagrant ssh
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-54-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Sun Jan  3 03:27:19 UTC 2021

  System load:  0.05              Processes:               119
  Usage of /:   4.2% of 38.71GB   Users logged in:         0
  Memory usage: 18%               IPv4 address for enp0s3: 10.0.2.15
  Swap usage:   0%


12 updates can be installed immediately.
8 of these updates are security updates.
To see these additional updates run: apt list --upgradable


$ id
uid=1002(ansible) gid=1002(ansible) groups=1002(ansible)
```