ps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-">Linux 下sudo网络权限详解
对于设置了网络代理的服务器,在当前用户下执行网络访问没有问题,但通过sudo执行命令时,就会出现“无网络连接”的错误。
背景
对于设置了网络代理的服务器,在当前用户下执行网络访问没有问题,但通过sudo执行命令时,就会出现“无网络连接”的错误。
普通权限下,wget成功。
| 代码如下 | 复制代码 |
|
# wget https://github**.c*om
--2016-12-08 09:00:43-- https://github**.c*om/
Connecting to 109.105.113.200:8080... connected.
Proxy request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html'
2016-12-08 09:01:03 (1.33 KB/s) - ‘index.html' saved [25692]
使用sudo命令后,连接失败。
# sudo wget https://github**.c*om
--2016-12-08 09:01:41-- https://github**.c*om/
Resolving github.com (github.com)... failed: Name or service not known.
wget: unable to resolve host address ‘github.com'
|
|
解决方案
出现这种情况的原因,是因为sudo命令的作用是为了允许普通用户使用超级用户权限的工具,但却没有继承来自环境变量。而网络代理的设置,是通过设置http_proxy,https_proxy等环境变量来实现的。因此,sudo后也就失去了网络代理,自然也就没有了网络连接。为了解决该问题,我们需要修改sudo的配置文件。修改sudo的配置文件有特殊的命令和方法,具体参考http://www.***jb51.net/article/113902.htm,我这里不做详细介绍了。
在sudo的配置文件中,增加Defaults env_keep += "http_proxy https_proxy"。该行表示使用sudo命令时,保存后面列出的环境变量。
| 代码如下 | 复制代码 |
|
增加配置后,sudo可访问网络。
# sudo wget https://github**.c*om
--2016-12-08 09:02:52-- https://github**.c*om/
Connecting to 109.105.113.200:8080... connected.
Proxy request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html.1'
2016-12-08 09:02:56 (20.1 KB/s) - ‘index.html.1' saved [25692]
|
|
感谢阅读,希望能帮助到大家。
孢子2生物进化中文版(Spores)
孢子2生物进化中文版让我们来做一回疯狂的博士,打造一个属于自
叫我万岁爷九游官方版
叫我万岁爷带你体验当换地点感觉,全新的模拟手游大作让你开始一
梦回凤歌
梦回凤歌手游中你将步入古代的宫廷纷争之中,本想度过美好幸福的
洋果子店rose最新版本
洋果子店最新版是一款十分容易让人垂涎的开店经营类手游。游戏玩
明日大亨最新版
明日大亨手游,一个非常经典有趣的模拟经营类型的商战手游,在游