Search code examples
linuxenvironment-variablesredhat

Accidently deleted all enviromnent variables on my linux machine


I wanted to install JDK on my linux machine, so I followed these steps: https://www.javahelps.com/2015/03/install-oracle-jdk-in-ubuntu.html

When I opened my /etc/environment file, it was empty, so I added PATH variable and the rest three variables from tutorial. Since then it seems that all env variables are deleted so I cannot do almost any command including sudo, su, vim etc. I am new with linux so I didn't know that would happen. I guess there were some default variables in case none of them are set?

Is there a way to restore that file? And where were those PATH variables before? How is it possible that I overwrote them since I didn't delete anything?

My OS is Redhat Linux Server.


Solution

  • /etc/environment is used by PAM to override environment variable settings, so by putting a PATH in there which does not contain any default directories like /usr/bin, you effectively removed them.

    It sounds like your OS does not use PAM to set the path if /etc/environment was empty, so you can probably restore things by deleting the PATH line you added. If that works, you can see what your 'normal' default PATH is, and add it back, along with the additions for oracle jdk

    Failing that, just ensure that the PATH you define in /etc/environment contains at least /usr/bin and /bin (and perhaps /usr/sbin, /sbin, and /usr/local/bin.) A decent one is

    PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"