我正在使用cloudformation启动EC2实例。我想使用密码而不是pem密钥为该实例启用SSH。我可以编写启动脚本(用户数据)来执行此操作,但我已设置静态密码' pass123'对于用户'学生'。我想让这个动态(随机)。 除了SSH客户端证书身份验证之外,我们对任何事情都很满意。
这是我的cloudformation模板:
{
"AWSTemplateFormatVersion":"2010-09-09",
"Description":"Password for instance",
"Parameters":{
"KeyName":{
"Description":"Name of an existing EC2 KeyPair",
"Type":"String"
},
"AWSAmiId":{
"Description":"AMI Id to find",
"Type":"String",
"Default":"x86_64,amzn-ami-pv-2014.09.1.x86_64-ebs,amazon,ebs,paravirtual"
}
},
"Resources":{
"Ec2Instance":{
"Type":"AWS::EC2::Instance",
"DependsOn":"Ec2SecurityGroup",
"Properties":{
"ImageId":{
"Ref":"AWSAmiId"
},
"InstanceType":"t1.micro",
"Tags":[
{
"Key":"Name",
"Value":"MYINSTANCE"
}
],
"DisableApiTermination":"true",
"SecurityGroupIds":[
{
"Ref":"Ec2SecurityGroup"
}
],
"KeyName":{
"Ref":"KeyName"
},
"UserData":{
"Fn::Base64":{
"Fn::Join":[
"\n",
[
"#!",
"useradd student\n",
"echo pass123 | passwd student --stdin\n",
"echo \"student ALL=(ALL) NOPASSWD: ALL\" | tee -a /etc/sudoers\n",
"mkdir /home/student/.ssh\n",
"cp cp ~ec2-user/.ssh/authorized_keys ~student/.ssh/authorized_keys\n",
"sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config\n",
"service sshd reload\n"
]
]
}
}
}
},
"Ec2SecurityGroup":{
"Type":"AWS::EC2::SecurityGroup",
"Properties":{
"GroupDescription":"VPC Security Group",
"SecurityGroupIngress":[
{
"CidrIp":"0.0.0.0/0",
"FromPort":"22",
"IpProtocol":"tcp",
"ToPort":"22"
}
]
}
}
},
"Outputs":{
"Password":{
"Description":"newly created EC2 instance",
"Value":"pass123"
}
}
}
密码可以是:
SSH密钥的文件名
AMZN帐户ID(例如:45678923)
随机字符串(poiblkjfda)
答案 0 :(得分:0)
如果您要求生成随机字符串作为密码,则可以使用 / dev / urandom 。对于10个字符的字符串
head /dev/urandom | tr -dc A-Za-z0-9 | head -c 10 ; echo ''
您可以将其包含在用户数据中,而不是静态密码中。
答案 1 :(得分:0)
返回随机密码有几个选项:
只有第一个很简单。其余的涉及一些配置/编码。