Linux解决无法访问学术网站的问题

科研工作中需要经常访问学术网站,然而有时候却无法访问或速度太慢,以致于论文无法下载。为解决这一问题,本文提出修改/etc/hosts文件的方法。

解决追加内容到 hosts 时无权限问题

1. 使用子命令

1
2
3
4
5
sudo bash -c "cat >> /etc/hosts" << EOF
172.67.9.71                  journals.aps.org
104.26.5.132                 doi.org
66.22.61.221                 iopscience.iop.org
EOF

2. 使用tee命令

1
2
3
4
5
sudo tee -a /etc/hosts > /dev/null <<EOF
172.67.9.71                  journals.aps.org
104.26.5.132                 doi.org
66.22.61.221                 iopscience.iop.org
EOF

Ip 查询

{% note primary %} 注意:选择上述任意的一个网站,然后输入你要访问的学术网站的域名,查询到对应的IP地址,保存下来。 {% endnote %}

写入到 hosts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#! /bin/sh
#
# Program  : updatehost.sh
# Version  : v1.1
# Date     : 2023-10-08
# Author   : fengzhenhua
# Email    : fengzhenhua@outlook.com
# CopyRight: Copyright (C) 2022-2025 FengZhenhua(冯振华)
# License  : Distributed under terms of the MIT license.
#
OutHost="/etc/hosts"
sudo curl -o $OutHost https://gitlab.com/ineo6/hosts/-/raw/master/next-hosts\?inline\=false
sudo sh -c "cat >> $OutHost" <<EOF

172.67.9.71                  journals.aps.org
104.26.5.132                 doi.org
66.22.61.221                 iopscience.iop.org
104.20.229.70                orcid.org
172.64.146.247               www.researchgate.net
151.101.3.42                 arxiv.org
EOF
sudo systemctl restart NetworkManager

Windows下修改hosts

  • 打开文本编辑器: 在管理员权限下打开你选择的文本编辑器。
  • 打开hosts文件: 在文本编辑器中,选择文件→打开,找到的hosts文件
    C:\Windows\System32\drivers\etc\hosts。
  • 编辑hosts文件: 在hosts文件的最后一行,添加你需要的域名和对应的IP地址。例如,你想将
    www.example.com指向本地的127.0.0.1,那么可以在hosts文件中添加以下内容:
1
127.0.0.1       www.example.com

每个映射关系应该占据一行,IP地址和域名之间使用制表符或空格进行分隔。

  • 保存文件: 在编辑完成后,保存hosts文件。
  • 刷新DNS缓存
    • 打开命令提示符: 在开始菜单中搜索“命令提示符”,并以管理员权限打开。
    • 执行命令: 在命令提示符中输入以下命令,并按下回车键:
    1
    
    ipconfig/flushdns

参考文章