Permanently Change Environment Variables in Window

2019-02-03 05:16发布

I found a way to change the default home directory of a user but I am having trouble with it.

enter image description here

enter image description here

enter image description here

Doing this will change the home drive to C:

But then when I check the environment variable:

enter image description here

It is still H:, with a system restart the Enviroment variables in windows settings will also return to H:/

I have also tried changing it like this:

enter image description here

Which appears to work but if i open a new cmd it will have reverted back to H:/

Now I am trying to do this so that OpenSSH will recognise C as my home directory instead of H: which is a network drive, forcing OpenSSH not to work unless I cam connected to my university network via VPN.

What can I do to set this permanently and in the eyes of OpenSSH?

5条回答
唯我独甜
2楼-- · 2019-02-03 05:32

On windows 8:

  1. Hit "windows key".
  2. type "system environment variables".
  3. Allow "Advance system settings" to make changes.
  4. Select "Advance" tab.
  5. Click on "Enviroment Variables...".
  6. Double click on "Path" from "User variable for XXXXXXXX"
  7. Add at the end the variable the new path that you want separated by semicolon.

    e.g. C:\Program Files\;C:\Python27\ shareeditdelete (1)flag

查看更多
地球回转人心会变
3楼-- · 2019-02-03 05:37

Nowhere does it mention a dependency between the HOMEDRIVE value and the HOMEDIRECTORY value, what was happening (I think) is that it was failing to map the home directory to the HOMEDRIVE and therefore defaulting back to a safe value (C:)

I wrote a script to update the local AD, replace the values in [] with your values. Copy and paste into a .vbs file and double click on it to run it.

Set objUser = GetObject("WinNT://[COMPUTERNAME]/[USERNAME],user")
objUser.homeDirDrive = "H:"
objUser.HomeDirectory = "[URNPATH]"
objUser.SetInfo

e.g.

Set objUser = GetObject("WinNT://UQBDART-2328/BEN,user")
objUser.homeDirDrive = "H:"
objUser.HomeDirectory = "\\SERVER\SHARE"
objUser.SetInfo

run this, reboot and test. It worked for me.

查看更多
祖国的老花朵
4楼-- · 2019-02-03 05:38

Back in the days of DOS the environment variables were part of the Program Segement which meant that you basically got a local snapshop of the variables limited to the scope of that program.

I'm guessing Windows hasn't changed this and the environment variables available to the CMD window only have the scope of that window and any further windows it spawns. This is supported by this little experiment:

in a CMD window type

set homedrive=h:\

and then test it's updated by typing

set h

from this same window type

start cmd

and in this new window type

set h

when I've done this I get HOMEDRIVE=H:\

if I then just open a new CMD window from the start menu and do the same SET H I get HOMEDRIVE=C:\

I don't know anything about OpenSSH but I suspect like the START CMD got the updated HOMEDRIVE environment variable, if you write a batch to update the environment variable and then execute OpenSSH that'll work.

查看更多
冷血范
5楼-- · 2019-02-03 05:43

Sounds like the AD profile on the domain is overwriting the user defined variables. I see your screenshot says you are connected to the eait.org.edu.au domain. That will be the root of your issue. Just to include some details here that I spotted elsewhere, that may be of help to you:

HOMEDRIVE, HOMEPATH & HOMESHARE are set and updated via Active Directory. HOMEDRIVE & HOMEPATH are set even without a home drive set on the account; however they will be overridden by any user account properties set in AD.

Also see these KB articles:

http://support.microsoft.com/kb/841343
http://support.microsoft.com/kb/237566
http://support.microsoft.com/kb/101507 

On a side-note for another way around the issue:

-I have in the past created a new instance of the windows command-line shell executable that automatically runs a custom script, so everytime you launch the shell, the environment variable could be overriden.

-To do that you could just put the code you posted to change the environment variable into a batch script, stored wherever you like, then edit the shortcut(s) used to launch the shell by going to properties > then alter the Target box: %SystemRoot%\system32\cmd.exe /K "C:\Documents and Settings\Administrator\My Documents\customshellscript.cmd" (Obviously the part of the path after /K is the location of your custom script)

This way, if you are using openSSH over the console anyway, it will always have the homedrive set correctly.

查看更多
SAY GOODBYE
6楼-- · 2019-02-03 05:48

Changing those environment variable's values is not "supported", at least it will not work as you expect because Windows changes them back.

According to this Microsoft knowledge base article (KB841343), you should use policies, if you need to change these settings. The article also contains links for how to do that (but personally, I never tried). Note that the article was originally written for Windows 2000, but I would strongly suspect, that it is still valid for current Windows versions.

查看更多
登录 后发表回答