Home >Operation and Maintenance >Linux Operation and Maintenance >Sample code sharing about sudo network permissions under Linux

Sample code sharing about sudo network permissions under Linux

黄舟
黄舟Original
2017-06-04 11:17:341747browse

This article mainly introduces the relevant information on the detailed explanation of sudo network permissions under Linux. Friends in need can refer to

The detailed explanation of sudo network permissions under Linux

For servers with a network proxy set up, there is no problem when performing network access under the current user, but when executing commands through sudo, an error of "no network connection" will occur.

Background

For servers with a network proxy set up, there is no problem when performing network access under the current user, but when executing commands through sudo, it will appear. "No network connection" error.

Under normal permissions, wget is successful.

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

Solution

The reason for this situation is because the sudo command is used to allow ordinary users to use tools with super user privileges, but it does notInheritedfrom environmentVariables. The setting of the network proxy is achieved by setting environment variables such as http_proxy and https_proxy. Therefore, after sudo, the network proxy is lost, and naturally there is no network connection. In order to solve this problem, we need to modify sudo's config file. There are special commands and methods to modify the sudo configuration file

In the sudo configuration file, add Defaults env_keep += "http_proxy https_proxy". This line indicates that when using the sudo command, save the environment variables listed later.

增加配置后,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]

The above is the detailed content of Sample code sharing about sudo network permissions under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn