2

I have an instance on Google computer engine, and i have exported environment variable running $ export my_token=my_token. Everything is fine, it prints my_token running $ echo $my_token.

But after closing ssh connection and reconnect to my instance and I try again to run echo $my_token, it prints nothing. Therefore I guess I've lost the exported environment variable after closing ssh.

  • How can i keep my environment variable even after closing ssh on a Google Compute Engine Instance?
GalloCedrone
  • 4,869
  • 3
  • 25
  • 41
voxter
  • 853
  • 2
  • 14
  • 30

1 Answers1

1

On a Compute Engine Instance it works exactly like any other Linux or Unix instance, from the Operating System point of view there is no difference.

Explanation

  • Setting an environment variable applies merely to the session, it is not persistent, each time you open a new shell (for example changing the user or connecting through ssh) you will lose the environment variable set in the previous one.

Solution

To keep a value of an environment variable that will be set in each new shell you start, you could add the export my_token=my_token command to one of your shell's init files. For example ~/.profile or ~/.bashrc.

Further References

GalloCedrone
  • 4,869
  • 3
  • 25
  • 41
  • well, because i don't want to save `my_token` in anywhere in gce, so i used command instead of using bash file. So can we have other ways to do without saving `my_token` on a file ? Saving token on a file can be a seurity problem. – voxter May 21 '18 at 10:03
  • You can place it in a file which can be accessed by your user only as for example .bashrc. Consider that the ssh keys are saved in `./ssh` folder and it is not considered a security issue. If you want to store something and have it permanent also after a reboot I can see merely storing it into a file as a option – GalloCedrone May 21 '18 at 10:15
  • Therefore I do not see it really useful, but if you want to avoid it for any reason: you can "reconnect to a disconnected ssh session" When you first log in, run screen. You get another shell, run commands in that. If you're disconnected, the screen process keeps the terminal alive so that your shell and the processes it is running don't fall over. When you reconnect, run 'screen -r' to resume. – GalloCedrone May 21 '18 at 10:19