新装ubuntu电脑的一些调整
- 必要命令的安装
- 必要开发工具的安装
更换国内软件源
/etc/apt/sources.list文件,后面添加下面地址用来添加阿里源
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse\ deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
然后更新一下
sudo apt update sudo apt upgrade
然后是清华软件源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
上面步骤重复一下就好,目前就只有这个是可有,其他的博客上的软件源用了会有报错,记录一下。因为经常用python和其他一些关键命令需要python的一些类库相关的头文件和pkg-config,这些都是被linux分拆成一个个dev包的,所以这里给补上:
sudo apt install python3-dev
添加pip软件源
在用户目录下编写.pip/pip.conf文件:
[global] timeout = 6000 index-url = https://pypi.tuna.tsinghua.edu.cn/simple trusted-host = pypi.tuna.tsinghua.edu.cn
然后在使用更新包pip/pip3包的时候会有一个警告:
WARNING: The scripts pip, pip3 and pip3.10 are installed in '/home/xxx/.local/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
那就按照警告说的,添加用户变量吧,编辑~/.bashrc文件
#添加 export PATH=/home/xxx/.local/bin:$PATH
然后source一下,就可以继续更新pip了。
添加docker加速源和go加速
首先是查看docker版本,然后针对性进行配置,如下:
jam@jam:~$ docker version Client: Docker Engine - Community Version: 23.0.0 API version: 1.42 Go version: go1.19.5 Git commit: e92dd87 Built: Wed Feb 1 17:47:51 2023 OS/Arch: linux/amd64 Context: default permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/version": dial unix /var/run/docker.sock: connect: permission denie
这种情况就是权限不足,给docker分一点:
sudo chmod 777 /var/run/docker.sock
然后再查看docker版本,这时候信息就很全面了,没有权限报错了,我的docker api版本是1.42,根据下面网址配置阿里云的镜像加速服务:
阿里云镜像加速申请和配置指南
配置nginx服务和redis还有mq服务
docker作为容器服务的提供者,很多人用它来打包项目环境,然后在需要的地方直接pull镜像就可以复用了,企业也很多都使用docker来作为流水线的服务提供者,使用k8s来进行管理即可。这里把nginx,redis和mq都放在不同的docker镜像中,要用的时候再跑起来:
docker search nginx docker pull nginx:latest # docker images命令查看拉取的镜像 #后台运行,-p参数映照容器80端口到本机8080端口 docker run --name nginxtest -p 8080:80 -d nginx docker ps -a #查看容器的运行情况 #同样的步骤都来几遍 docker run -itd --name redisserv -p 8081:6379 redis # 设置访问需要密码 docker run -itd --name mongoserv -p 8082:27017 mongo --auth #mq后面补上
然后就是配置go的加速:
go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.cn
配置了国内的加速镜像七牛云,说实话,不知道它还能顶多久。
配置一下flask和django的虚拟开发环境
这里使用virtualenv:
pip install virtualenv # 创建对应目录,然后使用virtualenv来设置为虚拟工程目录 virtualenv myproject #或者指定python版本 virtualenv -p 绝对路径/python myproject # 激活环境,退出就用deactive命令 source myproject/bin/activate #安装,前后可以用pip list查看安装先后包变化 pip install flask #安装好以后可以进python交互界面查看flask版本 python3 >>>flask.__version__ '2.2.2'
简单写一个helloflask:
from flask import Flask app = Flask(__name__) @app.route('/') def sayhello(): return "hello, flask." if __name__ == '__main__': app.run()
然后使用python app.py或者flask run都行,不过flask run一定要编写app.py名称的源文件,这个是它的设定,然后访问127.0.0.1:5000本地回流地址即可,默认是5000端口的。
django准备
pip install django #进入交互界面 python3 >>>import django >>>django.get_version() '4.1.6' >>>exit() # 创建django项目 django-admin startproject www cd www python manage.py runserver
然后就可以访问回流地址的8000端口查看结果了。至此,flask和django都算搞定了。
后面遇到的问题
问题伊始,是访问github的时候发现网络有问题,然后就ping了一下,发现一直卡着,但也不结束,感觉问题有点麻烦,跟着网上一篇博客进行调整,发现是能解决问题,但没有完全解决问题。首先,我安装的是ubuntu20.04的系统,这是大前提。
修理过程:
现象
# 就这么一直卡着,但每退出去 jam:~$ ping github.com PING github.com (20.205.243.166) 56(84) bytes of data. ^C --- github.com ping statistics --- 4 packets transmitted, 0 received, 100% packet loss, time 3071ms # 检查一下网络是否可通 jam:~$ ping www.bing.com PING china.bing123.com (202.89.233.100) 56(84) bytes of data. 64 bytes from 202.89.233.100 (202.89.233.100): icmp_seq=1 ttl=119 time=42.7 ms 64 bytes from 202.89.233.100 (202.89.233.100): icmp_seq=2 ttl=119 time=42.2 ms 64 bytes from 202.89.233.100 (202.89.233.100): icmp_seq=3 ttl=119 time=44.4 ms ^C --- china.bing123.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 42.196/43.093/44.414/0.953 ms
听说是dns污染的问题,还可能是运营商,不清楚。
那就配置一下dns吧,在谷歌专用的8.8.8.8和另外的1.1.1.1可ping通的情况下,配置/etc/resolv.conf,打开后看到一段未知配置:
# This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8). # Do not edit. # # This file might be symlinked as /etc/resolv.conf. If you're looking at # /etc/resolv.conf and seeing this text, you have followed the symlink. # # This is a dynamic resolv.conf file for connecting local clients to the # internal DNS stub resolver of systemd-resolved. This file lists all # configured search domains. # # Run "resolvectl status" to see details about the uplink DNS servers # currently in use. # # Third party programs should typically not access this file directly, but only # through the symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a # different way, replace this symlink by a static file or a different symlink. # # See man:systemd-resolved.service(8) for details about the supported modes of # operation for /etc/resolv.conf. nameserver 127.0.0.53 options edns0 trust-ad search .
未知代码是指nameserver那里的配置,没印象配过这种,不过删掉这部分添加两个域名解析服务器,修改成下面这样:
nameserver 8.8.8.8 nameserver 1.1.1.1
不过,上面的文件注释是伏笔,注意(Do not edit)。
修改了以后,再去ping一下github.com进行测试,发现还是不行,nslookup测试一下:
# 如果显示没有nslookup可以这么解决 sudo age install dnsutils jam:~$ nslookup github.com 8.8.8.8 Server: 8.8.8.8 Address: 8.8.8.8#53 Non-authoritative answer: Name: github.com Address: 20.205.243.166 jam:~$ ping 20.205.243.166 PING 20.205.243.166 (20.205.243.166) 56(84) bytes of data. ^C --- 20.205.243.166 ping statistics --- 4 packets transmitted, 0 received, 100% packet loss, time 3060ms jam:~$ nslookup github.com 1.1.1.1 Server: 1.1.1.1 Address: 1.1.1.1#53 Non-authoritative answer: Name: github.com Address: 192.30.255.113 jam:~$ ping 192.30.255.113 PING 192.30.255.113 (192.30.255.113) 56(84) bytes of data. 64 bytes from 192.30.255.113: icmp_seq=1 ttl=48 time=262 ms 64 bytes from 192.30.255.113: icmp_seq=2 ttl=48 time=264 ms 64 bytes from 192.30.255.113: icmp_seq=3 ttl=48 time=203 ms ^C --- 192.30.255.113 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2004ms rtt min/avg/max/mdev = 203.132/242.843/263.700/28.091 ms jam:~$
上面的检测结果发现,8.8.8.8这个服务器要么被搞了,要么就是上面的ip被搞了,现在把1.1.1.1放在8.8.8.8前面试一下:
# 修改/etc/resolv.conf如下: nameserver 1.1.1.1 nameserver 8.8.8.8 # 然后测试 jam:~$ ping github.com PING github.com (192.30.255.113) 56(84) bytes of data. 64 bytes from lb-192-30-255-113-sea.github.com (192.30.255.113): icmp_seq=1 ttl=48 time=306 ms 64 bytes from lb-192-30-255-113-sea.github.com (192.30.255.113): icmp_seq=2 ttl=48 time=232 ms 64 bytes from lb-192-30-255-113-sea.github.com (192.30.255.113): icmp_seq=3 ttl=48 time=249 ms ^C --- github.com ping statistics --- 4 packets transmitted, 3 received, 25% packet loss, time 3005ms rtt min/avg/max/mdev = 232.454/262.432/306.175/31.631 ms jam:~$
好,四分之一的丢包率,可以接受。但实际上后续还是遇上了同样的问题,还以为是github用了ssh,搞得又回头重新配了一次ssh-key,但当我重新回到resolv.conf一看,又回到最开始的地方才发现它自己偷偷修改了。。。。。。
这种dns的配置问题,才让我发现修改resolv.conf没啥用,有博客介绍说是dhcp的问题,但说法太多,了解太少,只能照做,没法判断:
# 修改/etc/systemd/resolved.conf,修改DNS一项 DNS=1.1.1.1 8.8.8.8 # 重启服务 jam:~$ sudo systemctl restart systemd-resolved # 设定开机自启 jam:~$ sudo systemctl enable systemd-resolved # 保存备份文件 jam:~$ sudo mv /etc/resolv.conf /etc/resolv.conf.bak # 重新映射服务到etc目录下,如果ln -l查看/etc/resolv.conf会发现这个映射关系 jam:~$ sudo ln -s /run/systemd/resolve/resolv.conf /etc/
全程要使用root权限执行,否则总会有各种权限限制,执行完以后,应该就可以在/etc/resolv.conf中看到新的两个nameserver了。重启看看效果,嗯,阔以,至少域名解析应该暂时没问题了。另外在上面有记录电脑系统是ubuntu20.04,所以很多设置都会有变动,系统好像也放弃了NetworkManger其他的管理来着,用上了netplan。慢慢记录吧。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了