How do I add a SSH key to Google Cloud Compute Engine VM Linux instance?












0















I have a Linux VM instance running in Google Cloud Platform.
I tried to copy my public key to ~/.ssh/authorized_keys and I can successfully SSH to my VM.



But sometimes ~/.ssh/authorized_keys is flushed and I have to copy the public key again.
It is really a pain to add public key every time.
How do I add a public key permanently?










share|improve this question

























  • Are other files in your home dir disappearing as well (is the homedir wiped)?

    – Dan Cornilescu
    Nov 23 '18 at 15:51











  • No, they are just the same.

    – Valeri
    Nov 26 '18 at 5:21
















0















I have a Linux VM instance running in Google Cloud Platform.
I tried to copy my public key to ~/.ssh/authorized_keys and I can successfully SSH to my VM.



But sometimes ~/.ssh/authorized_keys is flushed and I have to copy the public key again.
It is really a pain to add public key every time.
How do I add a public key permanently?










share|improve this question

























  • Are other files in your home dir disappearing as well (is the homedir wiped)?

    – Dan Cornilescu
    Nov 23 '18 at 15:51











  • No, they are just the same.

    – Valeri
    Nov 26 '18 at 5:21














0












0








0








I have a Linux VM instance running in Google Cloud Platform.
I tried to copy my public key to ~/.ssh/authorized_keys and I can successfully SSH to my VM.



But sometimes ~/.ssh/authorized_keys is flushed and I have to copy the public key again.
It is really a pain to add public key every time.
How do I add a public key permanently?










share|improve this question
















I have a Linux VM instance running in Google Cloud Platform.
I tried to copy my public key to ~/.ssh/authorized_keys and I can successfully SSH to my VM.



But sometimes ~/.ssh/authorized_keys is flushed and I have to copy the public key again.
It is really a pain to add public key every time.
How do I add a public key permanently?







ubuntu google-cloud-platform google-compute-engine






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 10:44









Maxim

1,506210




1,506210










asked Nov 23 '18 at 12:46









ValeriValeri

14511




14511













  • Are other files in your home dir disappearing as well (is the homedir wiped)?

    – Dan Cornilescu
    Nov 23 '18 at 15:51











  • No, they are just the same.

    – Valeri
    Nov 26 '18 at 5:21



















  • Are other files in your home dir disappearing as well (is the homedir wiped)?

    – Dan Cornilescu
    Nov 23 '18 at 15:51











  • No, they are just the same.

    – Valeri
    Nov 26 '18 at 5:21

















Are other files in your home dir disappearing as well (is the homedir wiped)?

– Dan Cornilescu
Nov 23 '18 at 15:51





Are other files in your home dir disappearing as well (is the homedir wiped)?

– Dan Cornilescu
Nov 23 '18 at 15:51













No, they are just the same.

– Valeri
Nov 26 '18 at 5:21





No, they are just the same.

– Valeri
Nov 26 '18 at 5:21












2 Answers
2






active

oldest

votes


















0














This ~/.ssh/authorized_keys takes the ssh keys from the metadata. It best you keep your ssh public keys in the metadata as mentioned here and there’s also a stack discussion here






share|improve this answer































    0














    One possible reason for which the file would "disappear" would be that every time the instance is reloaded it is reloaded from the same VM image, which doesn't contain the file. Check if the occurences are correlated with the instance reloading and if other files in your home dir disappear as well, if they do that's likely the culprit. If so I can see a couple of possible approaches:




    • include the ~/.ssh/authorized_keys file (and, of course, your user and home directory configurations) into a custom VM image that you could then use to boot your instances from


    • configure your home directory on a persistent disk which would survive across instance reloads







    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446990%2fhow-do-i-add-a-ssh-key-to-google-cloud-compute-engine-vm-linux-instance%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      This ~/.ssh/authorized_keys takes the ssh keys from the metadata. It best you keep your ssh public keys in the metadata as mentioned here and there’s also a stack discussion here






      share|improve this answer




























        0














        This ~/.ssh/authorized_keys takes the ssh keys from the metadata. It best you keep your ssh public keys in the metadata as mentioned here and there’s also a stack discussion here






        share|improve this answer


























          0












          0








          0







          This ~/.ssh/authorized_keys takes the ssh keys from the metadata. It best you keep your ssh public keys in the metadata as mentioned here and there’s also a stack discussion here






          share|improve this answer













          This ~/.ssh/authorized_keys takes the ssh keys from the metadata. It best you keep your ssh public keys in the metadata as mentioned here and there’s also a stack discussion here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 18:54









          AdebisiAdebisi

          444




          444

























              0














              One possible reason for which the file would "disappear" would be that every time the instance is reloaded it is reloaded from the same VM image, which doesn't contain the file. Check if the occurences are correlated with the instance reloading and if other files in your home dir disappear as well, if they do that's likely the culprit. If so I can see a couple of possible approaches:




              • include the ~/.ssh/authorized_keys file (and, of course, your user and home directory configurations) into a custom VM image that you could then use to boot your instances from


              • configure your home directory on a persistent disk which would survive across instance reloads







              share|improve this answer






























                0














                One possible reason for which the file would "disappear" would be that every time the instance is reloaded it is reloaded from the same VM image, which doesn't contain the file. Check if the occurences are correlated with the instance reloading and if other files in your home dir disappear as well, if they do that's likely the culprit. If so I can see a couple of possible approaches:




                • include the ~/.ssh/authorized_keys file (and, of course, your user and home directory configurations) into a custom VM image that you could then use to boot your instances from


                • configure your home directory on a persistent disk which would survive across instance reloads







                share|improve this answer




























                  0












                  0








                  0







                  One possible reason for which the file would "disappear" would be that every time the instance is reloaded it is reloaded from the same VM image, which doesn't contain the file. Check if the occurences are correlated with the instance reloading and if other files in your home dir disappear as well, if they do that's likely the culprit. If so I can see a couple of possible approaches:




                  • include the ~/.ssh/authorized_keys file (and, of course, your user and home directory configurations) into a custom VM image that you could then use to boot your instances from


                  • configure your home directory on a persistent disk which would survive across instance reloads







                  share|improve this answer















                  One possible reason for which the file would "disappear" would be that every time the instance is reloaded it is reloaded from the same VM image, which doesn't contain the file. Check if the occurences are correlated with the instance reloading and if other files in your home dir disappear as well, if they do that's likely the culprit. If so I can see a couple of possible approaches:




                  • include the ~/.ssh/authorized_keys file (and, of course, your user and home directory configurations) into a custom VM image that you could then use to boot your instances from


                  • configure your home directory on a persistent disk which would survive across instance reloads








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 25 '18 at 0:10

























                  answered Nov 24 '18 at 19:26









                  Dan CornilescuDan Cornilescu

                  27.8k113162




                  27.8k113162






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446990%2fhow-do-i-add-a-ssh-key-to-google-cloud-compute-engine-vm-linux-instance%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Sphinx de Gizeh

                      Dijon

                      Langue