1 # This module defines a systemd service that sets the SSH host key and
2 # authorized client key and host name of virtual machines running on
3 # Amazon EC2, Eucalyptus and OpenStack Compute (Nova).
16 (mkRemovedOptionModule [ "ec2" "metadata" ] "")
21 systemd.services.apply-ec2-data = {
22 description = "Apply EC2 Data";
28 before = [ "sshd.service" ];
29 after = [ "fetch-ec2-metadata.service" ];
31 path = [ pkgs.iproute2 ];
34 ${optionalString (config.networking.hostName == "") ''
35 echo "setting host name..."
36 if [ -s /etc/ec2-metadata/hostname ]; then
37 ${pkgs.nettools}/bin/hostname $(cat /etc/ec2-metadata/hostname)
41 if ! [ -e /root/.ssh/authorized_keys ]; then
42 echo "obtaining SSH key..."
45 if [ -s /etc/ec2-metadata/public-keys-0-openssh-key ]; then
46 (umask 177; cat /etc/ec2-metadata/public-keys-0-openssh-key >> /root/.ssh/authorized_keys)
47 echo "new key added to authorized_keys"
51 # Extract the intended SSH host key for this machine from
52 # the supplied user data, if available. Otherwise sshd will
53 # generate one normally.
54 userData=/etc/ec2-metadata/user-data
59 if [ -s "$userData" ]; then
60 key="$(sed 's/|/\n/g; s/SSH_HOST_DSA_KEY://; t; d' $userData)"
61 key_pub="$(sed 's/SSH_HOST_DSA_KEY_PUB://; t; d' $userData)"
62 if [ -n "$key" ] && [ -n "$key_pub" ] && [ ! -e /etc/ssh/ssh_host_dsa_key ]; then
63 (umask 077; echo "$key" > /etc/ssh/ssh_host_dsa_key)
64 echo "$key_pub" > /etc/ssh/ssh_host_dsa_key.pub
67 key="$(sed 's/|/\n/g; s/SSH_HOST_ED25519_KEY://; t; d' $userData)"
68 key_pub="$(sed 's/SSH_HOST_ED25519_KEY_PUB://; t; d' $userData)"
69 if [ -n "$key" ] && [ -n "$key_pub" ] && [ ! -e /etc/ssh/ssh_host_ed25519_key ]; then
70 (umask 077; echo "$key" > /etc/ssh/ssh_host_ed25519_key)
71 echo "$key_pub" > /etc/ssh/ssh_host_ed25519_key.pub
76 serviceConfig.Type = "oneshot";
77 serviceConfig.RemainAfterExit = true;
80 systemd.services.print-host-key = {
81 description = "Print SSH Host Key";
82 wantedBy = [ "multi-user.target" ];
83 after = [ "sshd.service" ];
85 # Print the host public key on the console so that the user
86 # can obtain it securely by parsing the output of
87 # ec2-get-console-output.
88 echo "-----BEGIN SSH HOST KEY FINGERPRINTS-----" > /dev/console
89 for i in /etc/ssh/ssh_host_*_key.pub; do
90 ${config.programs.ssh.package}/bin/ssh-keygen -l -f "$i" || true > /dev/console
92 echo "-----END SSH HOST KEY FINGERPRINTS-----" > /dev/console
94 serviceConfig.Type = "oneshot";
95 serviceConfig.RemainAfterExit = true;
100 meta.maintainers = with maintainers; [ arianvp ];