温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

linux代理设置

发布时间:2020-08-10 14:00:10 来源:网络 阅读:5537 作者:xjsunjie 栏目:建站服务器

http代理

有些局域网环境上网需要使用代理上网,图形界面的很好解决就设置一下浏览器的代理就好了,但是Linux纯命令行的界面就需要手动配置了。

      如果要全局用户使用应用于所有的Shell,就需要修改 /etc/profile 文件

 1 # vi /etc/profile 

在文件中添加以下配置  

1 http_proxy=proxy.abc.com:8080  2 https_proxy=$http_proxy  
3 ftp_proxy=user:password@proxy.abc.com:8080  4 no_proxy=*.abc.com,10.*.*.*,192.168.*.*,*.local,localhost,127.0.0.1  5 export http_proxy https_proxy ftp_proxy no_proxy  

 

其中:
  http_proxy:http协议使用代理服务器地址;
  https_proxy:https协议使用安全代理地址;
  ftp_proxy:ftp协议使用代理服务器地址;
  user:代理使用的用户名;
  password:代理使用用户名的密码;
  proxy.abc.com:代理地址,可以是IP,也可以是域名;
  8080:使用的端口;
  no_proxy:不使用代理的主机或IP。

        保存退出,注销重新登陆系统即可生效。

        此方法只适合配置http代理,使用socket代理上网的另有其他配置方法。


常用linux程序配置代理服务器的方法:

[ 通用代理服务器配置 ]

对于大多数Linux控制台程序,例如Debian或Ubuntu中的apt-getaptitude命令、git命令、wget命令,这些程序都使用http_proxyftp_proxy环境变量来获取代理服务的配置。

方法是在你的~/.bashrc里加上类似下面的话:

export http_proxy=http://username:password@proxyserver:port/
export ftp_proxy=http://username:password@proxyserver:port/

如果你的代理服务器需要用户名和密码才能访问,需要填写上面的username和passwd部分,否则的话,省略这两部分。

例如,假设你的代理服务器为192.168.1.1,端口是8080,用户名为easwy,密码是123456,那么应该这样设置这两个环境变量:

export http_proxy=http://easwy:123456@192.168.1.1:8080 
export ftp_proxy=http://easwy:123456@192.168.1.1:8080

这样配置之后,退出再登录一次,或者直接使用下面的命令source一下.bashrc:

source ~/.bashrc

现在,上述程序就可以通过代理服务器访问网络了。

[ subversion的代理服务器配置 ]

要配置subversion的代理服务器,需要修改$HOME/.subversion/servers文件,在此文件的[global]段加上:

http-proxy-host = 192.168.1.1
http-proxy-port = 8080 
http-proxy-username = easwyhttp-proxy-password = 123456

现在svn就可以使用代理服务器访问版本库了。

[ yum的代理服务器配置 ]

如果想让CentOS中的yum可以通过代理服务器更新程序,则需要修改文件/etc/yum.conf,在此文件中加上:

proxy=http://easwy:123456@192.168.1.1:8080

现在使用yum就可以更新你的软件了


内网机器访问外网走代理

公司一般出于安全考虑, 在同一局域网中只有一台机器可以访问外网,运维进行了整体的限制, 但是在后面的工作中,需要在机器上安装一些软件,及命令,所以其他的机器需要访问外网来简化工作, 但又不能打乱原有运维的设置,所以需要在能访问外网的机器上做个代理。

第一步、二步都是在能访问外网的机器上操作, 第三步是在不能访问外网机器上操作。
1、检查机器
     在操作中发现访问外网的机器本身不能使用yum 安装软件,故需要配置yum安装源。如果可以的,这步可忽略。


[html] view plain copy

  1. [root@localhost test]# lsb_release -a  

  2. LSB Version:    :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-ia32:printing-4.0-noarch  

  3. Distributor ID: RedHatEnterpriseServer  

  4. Description:    Red Hat Enterprise Linux Server release 5.7 (Tikanga)  

  5. Release:        5.7  

  6. Codename:       Tikanga  

  7. [root@localhost test]# cd /etc/yum.repos.d/  

  8. [root@localhost test]# wget http://mirrors.163.com/.help/CentOS5-Base-163.repo  

 中间 wget 所获取的文件,需要将$releasever 修改为 5



[html] view plain copy

  1. # CentOS-Base.repo  

  2. #  

  3. # The mirror system uses the connecting IP address of the client and the  

  4. # update status of each mirror to pick mirrors that are updated to and  

  5. # geographically close to the client.  You should use this for CentOS updates  

  6. # unless you are manually picking other mirrors.  

  7. #  

  8. # If the mirrorlistdoes not work for you, as a fall back you can try the   

  9. # remarked out baseurlline instead.  

  10. #  

  11. #  

  12.   

  13. [base]  

  14. name=CentOS-5 - Base - 163.com  

  15. baseurl=http://mirrors.163.com/centos/5/os/$basearch/  

  16. #mirrorlist=http://mirrorlist.centos.org/?release=5&arch=$basearch&repo=os  

  17. gpgcheck=1  

  18. gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5  

  19.   

  20. #released updates   

  21. [updates]  

  22. name=CentOS-5 - Updates - 163.com  

  23. baseurl=http://mirrors.163.com/centos/5/updates/$basearch/  

  24. #mirrorlist=http://mirrorlist.centos.org/?release=5&arch=$basearch&repo=updates  

  25. gpgcheck=1  

  26. gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5  

  27.   

  28. #packages used/produced in the build but not released  

  29. [addons]  

  30. name=CentOS-5 - Addons - 163.com  

  31. baseurl=http://mirrors.163.com/centos/5/addons/$basearch/  

  32. #mirrorlist=http://mirrorlist.centos.org/?release=5&arch=$basearch&repo=addons  

  33. gpgcheck=1  

  34. gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5  

  35.   

  36. #additional packages that may be useful  

  37. [extras]  

  38. name=CentOS-5 - Extras - 163.com  

  39. baseurl=http://mirrors.163.com/centos/5/extras/$basearch/  

  40. #mirrorlist=http://mirrorlist.centos.org/?release=5&arch=$basearch&repo=extras  

  41. gpgcheck=1  

  42. gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5  

  43.   

  44. #additional packages that extend functionality of existing packages  

  45. [centosplus]  

  46. name=CentOS-5 - Plus - 163.com  

  47. baseurl=http://mirrors.163.com/centos/5/centosplus/$basearch/  

  48. #mirrorlist=http://mirrorlist.centos.org/?release=5&arch=$basearch&repo=centosplus  

  49. gpgcheck=1  

  50. enabled=0  

  51. gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5  

  52.   

  53. #contrib - packages by Centos Users  

  54. [contrib]  

  55. name=CentOS-5 - Contrib - 163.com  

  56. baseurl=http://mirrors.163.com/centos/5/contrib/$basearch/  

  57. #mirrorlist=http://mirrorlist.centos.org/?release=5&arch=$basearch&repo=contrib  

  58. gpgcheck=1  

  59. enabled=0  

  60. gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5  

  yum clean all  清除原有缓存
  yum list           获取yum列表
  如果出现列表数据如下这种表示设置成功



[html] view plain copy

  1. sqlite-devel.i386                                                                            3.3.6-7                                                                   base       

  2. sqlite-devel.x86_64                                                                          3.3.6-7                                                                   base       

  3. squashfs-tools.x86_64                                                                        3.0-4                                                                     base       

  4. squirrelmail.noarch                                                                          1.4.8-21.el5.centos                                                       base       

  5. srptools.x86_64                                                                              0.0.4-10.el5                                                              base       

  6. sssd.x86_64                                                                                  1.5.1-70.el5                                                              base       

  7. sssd-client.i386                                                                             1.5.1-70.el5                                                              base       

  8. sssd-client.x86_64                                                                           1.5.1-70.el5                                                              base       

  9. sssd-tools.x86_64                                                                            1.5.1-70.el5                                                              base  


2、安装squid
    squid介绍及其简单配置
    前面准备好yum命令, 可以直接在线安装



[html] view plain copy

  1. yum install squid  

   安装完成后, cd  /etc/squid/ 目下, 修改squid.conf 文件中的内容,修改之前, 可以先备份该文件:

[html] view plain copy

  1. cp squid.conf squid.conf_bak  

  然后找到 文件中的 http_access deny all   将其修改为 http_access allow all  表示所有用户都可以访问这个代理,
  还有找到  http_port 3128  修改为  http_port 192.168.3.171:3128  这里的IP及端口是 squid的代理IP及端口,
  该IP是能访问外网机器的IP地址,如果是本机,则可以不用修改该地址, 下面启动squid 代理

[html] view plain copy

  1. [root@localhost squid]# squid -k parse  

  2. [root@localhost squid]#   

  3. [root@localhost squid]#   

  4. [root@localhost squid]# squid -z  

  5. 2014/07/22 14:43:01| Creating Swap Directories  

  6. [root@localhost squid]#   

  7. [root@localhost squid]#   

  8. [root@localhost squid]# service squid start  

  9. Starting squid:                                            [  OK  ]  

  10. [root@localhost squid]#   

  11. [root@localhost squid]#   

  12. [root@localhost squid]#   

  13. [root@localhost squid]# netstat -nltp  

  14. Active Internet connections (only servers)  

  15. Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name     

  16. tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      4341/portmap          

  17. <span style="color:#ff0000;">tcp        0      0 0.0.0.0:3128                0.0.0.0:*                   LISTEN      22862/(squid)  </span>       

  18. tcp        0      0 0.0.0.0:792                 0.0.0.0:*                   LISTEN      4426/rpc.statd  


3、配置不能访问外网的机器代理
   在其他有需要访问外网的内网机器上设置其访问外部网络的代理机器

[html] view plain copy

  1. export http_proxy=<span style="color:#ff0000;">http://192.168.3.171:3128</span>  

   export的效力仅及于该此登陆操作,非全局,详细内容可参考:linux环境变量 export命令详解

   这样就可以让不能访问外部网络的内网机器通过可以访问外网的机器做为代理访问到外部网络,来方便安装一些软件,命令等操作。


4、测试

     在配置的内网机器上执行:curl www.baidu.com,出现以下内容即访问网络成功:


[html] view plain copy

  1. [cosw@platdevsxyd01 ~]$ curl www.baidu.com  

  2. <!DOCTYPE html>  

  3. <!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge>  

  4. <meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css>  

  5. <title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper>   


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI