對於設置了網絡代理的服務器,在當前用戶下執行網絡訪問沒有問題,但經過sudo執行命令時,就會出現「無網絡鏈接」的錯誤。html
普通權限下,wget成功。 # wget https://github.com --2016-12-08 09:00:43-- https://github.com/ 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.com --2016-12-08 09:01:41-- https://github.com/ 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://blog.163.com/clevertanglei900@126/blog/static/111352259201142621547160/,我這裏不作詳細介紹了。
在sudo的配置文件中,增長Defaults env_keep += "http_proxy https_proxy"。該行表示使用sudo命令時,保存後面列出的環境變量。git
增長配置後,sudo可訪問網絡。 # sudo wget https://github.com --2016-12-08 09:02:52-- https://github.com/ 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]