云服务器自用配置记录
本文的运行环境基于华为云学生机 1C/2G/1M,Ubuntu 18.04 Server 64bit,文中命令均以 root 执行。
更换镜像源
直接用华为提供的源
见:如何使用自动化工具配置华为云镜像源(x8664)?弹性云服务器 ECS常见问题镜像源管理_华为云
wget http://mirrors.myhuaweicloud.com/repo/mirrors_source.sh && sh mirrors_source.sh
手动切换源并更新
-
备份自带源配置文件。
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
-
编辑文件。
sudo vim /etc/apt/sources.list
-
默认应该是有内容的,所以使用以下命令清空文件内容。
:1,$d
-
添加以下内容,分别为阿里云和清华大学的源。
deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse
-
保存并退出。
-
更新系统。
sudo apt-get update sudo apt-get upgrade
另外,可以用下面的命令查看内核版本。
cat /proc/version
设置 Swap 文件
这里按照 华为云官方文档 创建交换文件。
-
创建 1G 的 Swap 文件。
sudo dd if=/dev/zero of=/swapfile bs=1M count=1000
-
更改文件为 Swap。
sudo chmod 600 /swapfile
-
更改文件属性为 Swap。
sudo mkswap /swapfile
-
启用 Swap。
sudo swapon /swapfile
-
实现 Swap 开机自动挂载,将 Swap 文件挂载写入
/etc/fstab
。sudo echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
-
挂载 Swap。
sudo mount -a
使用 free -m
命令可以查看内存信息:
free -m
total used free shared buff/cache available
Mem: 1993 556 75 7 1361 1270
Swap: 999 0 999
常用软件安装
Docker
之前是在 CentOS 上安装,这次在 Ubuntu Server 上安装,依然以 官方文档 为准。
官方推荐使用 repository 的方式安装。
-
安装必要的包以使用 HTTPS:
sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common
-
添加 key:
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
-
验证 key:
sudo apt-key fingerprint 0EBFCD88
pub rsa4096 2017-02-22 [SCEA] 9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88 uid [ unknown] Docker Release (CE deb) <[email protected]> sub rsa4096 2017-02-22 [S]
-
设置 repository:
sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable"
-
更新索引:
sudo apt-get update
-
安装:
sudo apt-get install docker-ce docker-ce-cli containerd.io
-
测试:
sudo docker run hello-world
出现下面的文字则安装成功。
Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
Docker Compose
按照官方文档安装:Install Docker Compose | Docker Documentation
如果 Github 连不上的话,可以直接下载链接里的文件,然后改名为 docker-compose
放在 /usr/local/bin/
。
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
安装完成后查看版本:
sudo docker-compose --version