技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁 > 教程 > 服務(wù)器類 >

Linux 下sudo網(wǎng)絡(luò)權(quán)限詳細(xì)介紹

來源:技術(shù)員聯(lián)盟┆發(fā)布時間:2017-10-14 18:42┆點(diǎn)擊:

  Linux 下sudo網(wǎng)絡(luò)權(quán)限詳解

  對于設(shè)置了網(wǎng)絡(luò)代理的服務(wù)器,在當(dāng)前用戶下執(zhí)行網(wǎng)絡(luò)訪問沒有問題,但通過sudo執(zhí)行命令時,就會出現(xiàn)“無網(wǎng)絡(luò)連接”的錯誤。

  背景

  對于設(shè)置了網(wǎng)絡(luò)代理的服務(wù)器,在當(dāng)前用戶下執(zhí)行網(wǎng)絡(luò)訪問沒有問題,但通過sudo執(zhí)行命令時,就會出現(xiàn)“無網(wǎng)絡(luò)連接”的錯誤。

  普通權(quán)限下,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'

  解決方案

  出現(xiàn)這種情況的原因,是因?yàn)閟udo命令的作用是為了允許普通用戶使用超級用戶權(quán)限的工具,但卻沒有繼承來自環(huán)境變量。而網(wǎng)絡(luò)代理的設(shè)置,是通過設(shè)置http_proxy,https_proxy等環(huán)境變量來實(shí)現(xiàn)的。因此,sudo后也就失去了網(wǎng)絡(luò)代理,自然也就沒有了網(wǎng)絡(luò)連接。為了解決該問題,我們需要修改sudo的配置文件。修改sudo的配置文件有特殊的命令和方法,具體參考,我這里不做詳細(xì)介紹了。

  在sudo的配置文件中,增加Defaults env_keep += "http_proxy https_proxy"。該行表示使用sudo命令時,保存后面列出的環(huán)境變量。

  增加配置后,sudo可訪問網(wǎng)絡(luò)。

  # 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]