{"id":63,"date":"2014-04-15T16:58:00","date_gmt":"2014-04-15T14:58:00","guid":{"rendered":"http:\/\/www.cubeos.org\/blog\/2014\/04\/azure-from-the-linux-command-line-part-2\/"},"modified":"2014-04-15T16:58:00","modified_gmt":"2014-04-15T14:58:00","slug":"azure-from-the-linux-command-line-part-2","status":"publish","type":"post","link":"https:\/\/www.cubeos.org\/blog\/2014\/04\/azure-from-the-linux-command-line-part-2\/","title":{"rendered":"Azure from the Linux command line (part 2)"},"content":{"rendered":"<p>Hi!<\/p>\n<p>About a month ago, I wrote <a href=\"http:\/\/blogs.msdn.com\/b\/holgerkenn\/archive\/2014\/03\/20\/azure-from-the-linux-command-line-part-1.aspx\" target=\"_blank\"  rel=\"nofollow\" >the first blog post<\/a> of this series where I have shown how to set up the&nbsp;xplat-CLI (Cross Platform CLI) on Linux and I described how to create&nbsp;IAAS VMs on Azure.<\/p>\n<p>But the approach described had one important drawback: It creates the VMs with a default user and password, but not with an SSH key set up to login.<\/p>\n<p>So let me fix this&nbsp;here.<\/p>\n<p>When you&#8217;re familiar with SSH&nbsp;on unix platforms, the usual pattern is to use <span style=\"font-family: terminal,monaco\">ssh-keygen<\/span> to&nbsp;create a key pair, then push the public key into the <span style=\"font-family: terminal,monaco\">~\/.ssh\/authorized_keys<\/span> file on the remote host and keep the private key in your <span style=\"font-family: terminal,monaco\">~\/.ssh\/id_rsa<\/span> file. When using the same user name on both sides, the&nbsp;command&nbsp;<span style=\"font-family: terminal,monaco\">ssh&nbsp;&lt;remotehost&gt;<\/span> then just works without&nbsp;entering a password. And so does <span style=\"font-family: terminal,monaco\">scp, sftp<\/span>&nbsp;and (in case you have set the <span style=\"font-family: terminal,monaco\">rsync_rsh<\/span> environment variable&nbsp;to <span style=\"font-family: terminal,monaco\">ssh<\/span> in your login script) <span style=\"font-family: terminal,monaco\">rsync<\/span>.&nbsp;&nbsp;And as you have probably used an empty keyphrase for the secret key, this works nicely from scripts. (And of course I don&#8217;t recommend using that empty keyphrase in general, especially not for privileged accounts)&nbsp;<\/p>\n<p>On Microsoft Azure, we have an internal key deployment mechanism that is used for multiple things, it can deploy keys into Windows and Linux VMs, into PAAS roles and so on. And this mechanism is also used to deploy your ssh public key into your IAAS VMs. But in order to work, it needs the keys in a common universal file format. So just generating the keys using <span style=\"font-family: terminal,monaco\">ssh-keygen<\/span> won&#8217;t work. Instead, you can use openssl&nbsp; to generate the private and public key files in x.509 der format.<\/p>\n<p><span style=\"font-family: terminal,monaco\">$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout myPrivateKey.key -out myCert.pem<\/span><br \/><span style=\"font-family: terminal,monaco\">$ chmod 600 myPrivateKey.key<\/span><br \/><span style=\"font-family: terminal,monaco\">$ openssl&nbsp; x509 -outform der -in myCert.pem -out myCert.cer<\/span><\/p>\n<p>The first line generates the key pair, as you have probably guessed from the command line parameters, it&#8217;s a 2048 bit RSA keypair with a lifetime of 365 days. Again, you can create this key without a passphrase, but that might be a security risk.<\/p>\n<p>Remember the&nbsp;bash script line&nbsp;to create a VM from part one:<\/p>\n<p>$ <span style=\"font-family: terminal,monaco\">azure vm create -e -z extrasmall -l &#8220;West Europe&#8221; $1 $IMAGENAME&nbsp;azureuser &#8220;$PASSWORD&#8221;<\/span><\/p>\n<p>Now let&#8217;s modify this to use the newly generated key in addition to the password:<\/p>\n<p>$ <span style=\"font-family: terminal,monaco\">azure vm create -e -t myCert.pem -z extrasmall -l &#8220;West Europe&#8221; $1 $IMAGENAME&nbsp;azureuser &#8220;$PASSWORD&#8221;<\/span><\/p>\n<p>This creates the VM, but this time, azureuser gets a pre-configured <span style=\"font-family: terminal,monaco\">authorized_key<\/span>.<\/p>\n<p>There is one difference when doing a ssh into this VM: you need to specify the key to use as authorization and the remote user name:<\/p>\n<p><span style=\"font-family: terminal,monaco\">$ ssh -i myPrivateKey.key &lt;cloudservicename&gt;.cloudapp.net<\/span><\/p>\n<p>And now you&#8217;re not asked for a password anymore.<\/p>\n<p>The -i option also works for scp and sftp. For rsync, you can use<\/p>\n<p><span style=\"font-family: terminal,monaco\">$ export RSYNC_RSH=&#8221;ssh -i \/path\/to\/myPrivateKey.key&#8221;<\/span><\/p>\n<p>or use the <span style=\"font-family: terminal,monaco\">rsync &#8211;rsh &#8220;ssh -i \/path\/to\/myPrivateKey.key&#8221;<\/span>&nbsp;command line option to specify the remote shell and identity file to use.<\/p>\n<p>Hope it helps,<\/p>\n<p>H.<\/p>\n<p>&nbsp;<\/p>\n<div style=\"clear:both\"><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/blogs.msdn.com\/aggbug.aspx?PostID=10517514\" width=\"1\" height=\"1\"><br \/>\nSource: msdn<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi! About a month ago, I wrote the first blog post of this series where I have shown how to set up the&nbsp;xplat-CLI (Cross Platform CLI) on Linux and I described how to create&nbsp;IAAS VMs on Azure. But the approach &hellip; <a href=\"https:\/\/www.cubeos.org\/blog\/2014\/04\/azure-from-the-linux-command-line-part-2\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-microsoft"],"_links":{"self":[{"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":0,"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cubeos.org\/blog\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}