The information contained in this article has been verified as up-to-date on the date of the original publication of the article. HashiCorp endeavors to keep this information up-to-date and correct, but it makes no representations or warranties of any kind, express or implied, about the ongoing completeness, accuracy, reliability, or suitability of the information provided.
All information contained in this article is for general information purposes only. Any reliance you place on such information as it applies to your use of your HashiCorp product is therefore strictly at your own risk.
Introduction
This article provides an example of adding new SSH users with public keys to the AMI image built by Packer.
Prerequisites
- Packer CLI
- AWS account
Procedure
- Generate an SSH key and copy the generated
id_rsa.pub
to another folder, for example,assets/
:$ ssh-keygen
Output:
Your identification has been saved in /your_home/.ssh/id_rsa
Your public key has been saved in /your_home/.ssh/id_rsa.pub$ mkdir assets $ cp /your_home/.ssh/id_rsa.pub ./assets/
- In the
build
block, add a file provisioner to uploadid_rsa.pub
to/tmp
folder and shell provisioner to create a new SSH user:provisioner "file" {
source = "assets/"
destination = "/tmp"
}
provisioner "shell" {
inline = [
"echo 'Create newuser user and group'",
"sudo groupadd newuser",
"sudo useradd -m -g newuser -s /bin/bash -c \"Newuser\" newuser",
"sudo runuser -l newuser -c 'mkdir /home/newuser/.ssh'",
"sudo cp /tmp/id_rsa.pub /home/newuser/.ssh/",
"sudo chown newuser:newuser /home/newuser/.ssh/id_rsa.pub",
"sudo chmod 600 /home/newuser/.ssh/id_rsa.pub",
"sudo chmod 700 /home/newuser/.ssh/"
]
}
Additional Information