在 Windows 中永久更改环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11928078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:48:15  来源:igfitidea点击:

Permanently Change Environment Variables in Windows

windowscmdenvironment-variables

提问by Fantastic Mr Fox

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:

这样做会将主驱动器更改为 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:/

它仍然是 H:,随着系统重新启动 Windows 设置中的环境变量也会返回 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:/

这似乎有效,但如果我打开一个新的 cmd,它将恢复为 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.

现在我正在尝试这样做,以便 OpenSSH 将 C 识别为我的主目录而不是 H: 这是一个网络驱动器,迫使 OpenSSH 无法工作,除非我通过 VPN 连接到我的大学网络。

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

在 OpenSSH 眼中,我该怎么做才能永久设置它?

采纳答案by joocer

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:)

它没有提到 HOMEDRIVE 值和 HOMEDIRECTORY 值之间的依赖关系,发生的事情(我认为)是它未能将主目录映射到 HOMEDRIVE 并因此默认回安全值(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.

我写了一个脚本来更新本地 AD,用你的值替换 [] 中的值。复制并粘贴到 .vbs 文件中,然后双击它以运行它。

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.

运行这个,重新启动并测试。它对我有用。

回答by absentmindeduk

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:

听起来域上的 AD 配置文件正在覆盖用户定义的变量。我看到您的屏幕截图显示您已连接到 eait.org.edu.au 域。这将是你问题的根源。只是在这里包含我在其他地方发现的一些细节,这可能对您有所帮助:

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.

HOMEDRIVE、HOMEPATH 和 HOMESHARE 通过 Active Directory 设置和更新。即使没有在帐户上设置主驱动器,也设置了 HOMEDRIVE 和 HOMEPATH;但是,它们将被 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.

-我过去创建了一个 Windows 命令行 shell 可执行文件的新实例,它会自动运行自定义脚本,因此每次启动 shell 时,环境变量都可能被覆盖。

-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)

- 要做到这一点,您可以将您发布的用于更改环境变量的代码放入批处理脚本中,存储在您喜欢的任何位置,然后通过转到属性来编辑用于启动 shell 的快捷方式 > 然后更改目标框: %SystemRoot%\system32\cmd.exe /K "C:\Documents and Settings\Administrator\My Documents\customshellscript.cmd"(很明显/K后面的那部分路径就是你自定义脚本的位置)

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

这样,如果您无论如何都在控制台上使用 openSSH,它将始终正确设置 homedrive。

回答by Christian.K

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

不“支持”更改这些环境变量的值,至少它不会像您期望的那样工作,因为 Windows 会将它们改回来。

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.

根据这篇Microsoft 知识库文章 (KB841343),如果您需要更改这些设置,您应该使用策略。该文章还包含有关如何执行此操作的链接(但就个人而言,我从未尝试过)。请注意,该文章最初是为 Windows 2000 编写的,但我强烈怀疑它对当前的 Windows 版本仍然有效。

回答by joocer

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.

回到 DOS 时代,环境变量是程序段的一部分,这意味着您基本上可以获得仅限于该程序范围的变量的本地快照。

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:

我猜 Windows 没有改变这一点,CMD 窗口可用的环境变量只有该窗口的范围以及它产生的任何其他窗口。这个小实验支持了这一点:

in a CMD window type

在 CMD 窗口类型中

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:\

完成此操作后,我得到 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:\

如果我然后从开始菜单打开一个新的 CMD 窗口并执行相同的 SET HI 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.

我对 OpenSSH 一无所知,但我怀疑 START CMD 获得了更新的 HOMEDRIVE 环境变量,如果您编写一个批处理来更新环境变量,然后执行 OpenSSH 就可以了。

回答by Richard

This issue had been really frustrating me trying to find a solution, but I have found what I think is the definitive answer and posted it at:

这个问题一直让我试图找到解决方案感到非常沮丧,但我找到了我认为的最终答案并将其发布在:

https://stackoverflow.com/a/60235759/12903197

https://stackoverflow.com/a/60235759/12903197

You need to run 'net user USERNAME /homedir:PATH' where USERNAME is the name of the user you are trying to change and PATH is the drive letter and full path to the desired home folder, which must already exist.

您需要运行“net user USERNAME /homedir:PATH”,其中 USERNAME 是您要更改的用户的名称,PATH 是所需主文件夹的驱动器号和完整路径,该文件夹必须已存在。

回答by Adolfo Macias

On windows 8:

在 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

  1. 点击“windows键”。
  2. 输入“系统环境变量”。
  3. 允许“高级系统设置”进行更改。
  4. 选择“高级”选项卡。
  5. 单击“环境变量...”。
  6. 双击“XXXXXXXX的用户变量”中的“路径”
  7. 在变量末尾添加要以分号分隔的新路径。

    例如 C:\Program Files\;C:\Python27\shareeditdelete (1)flag