2020年从零开始初始化一台 Ubuntu 19.10 的 VPS

2020年从零开始初始化一台 Ubuntu 19.10 的 VPS

前言

本文假设读者有一定 Linux Server 基础。所以并不会对每一个命令进行解释。更多是对自己需要的记录。

本文大部分内容是

Initial Server Setup with Ubuntu 18.04

这个文章的简单复述。

并且根据用最大性价比达成能全球访问的生产环境原则,在细节的设置上会进行我自己觉得需要做的相应调整。

digitalocean 是个良心厂商,tutorials 的文章都值得一看。

前期准备

默认读者拥有一台VPS。

VPS 如需购买,我使用的主机商是

Vultr

DigitalOcean

最便宜的 VPS 每月 USD $5。

以上仅供参考。

本文具有强烈的个人感情色彩,如有观看不适,请尽快关闭. 本文仅作为个人学习记录使用,也欢迎在许可协议范围内转载或使用,请尊重版权并且保留原文链接,谢谢您的理解合作. 如果您觉得本站对您能有帮助,您可以使用RSS方式订阅本站,这样您将能在第一时间获取本站信息.

命令说明

> , $, # 皆是符号来作为命令行提示符

1
2
3
> // 表示在本地环境输入
$ // 表示在 VPS 环境下普通用户身份输入
# // 表示在 VPS 环境下 root 用户身份输入
1
[] // 表示一个变量,需要替换为你自己的正确值

购买 VPS

Vultr or DigitalOcean or 你喜欢的主机商根据喜好购买一台最便宜的 VPS。OS

选择 Ubuntu 19.10 干净镜像。

一路顺风的完成以后能够需要获得以下信息:

  1. [IP4 地址]
  2. [root 密码]

初始化 VPS

登陆 VPS

1
2
> ssh root@[IP4 地址]
[root 密码]

建立新用户

1
2
# adduser [用户名]
[用户名密码]

除了密码其他部分可以一路回车

给新用户加 sudo

1
# usermod -aG sudo [用户名]

设置基本的防火墙

1
# ufw app list
1
2
3
Output
Available applications:
OpenSSH
1
2
3
# ufw allow OpenSSH
# ufw enable
# ufw status
1
2
3
4
5
6
7
Output
Status: active

To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)

添加 Swap

由于我们使用的是便宜 VPS 一般内存较小。而硬盘很可能已经是 SSD。 所以,我们添加一个 Swap 来优化我们的主机。

检查目前 Swap 信息

1
# swapon --show

如果没有任何输出,就说明没有 Swap

用 free 验证一下

1
# free -h

类似输出

1
2
3
4
Output
total used free shared buff/cache available
Mem: 985M 84M 222M 680K 678M 721M
Swap: 0B 0B 0B

检查目前硬盘信息

1
# df -h

类似输出

1
2
3
4
5
6
7
8
9
10
Output
Filesystem Size Used Avail Use% Mounted on
udev 481M 0 481M 0% /dev
tmpfs 99M 656K 98M 1% /run
/dev/vda1 25G 1.4G 23G 6% /
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/vda15 105M 3.4M 102M 4% /boot/efi
tmpfs 99M 0 99M 0% /run/user/1000

主要看 /dev/vda1 这个 的 Avail 还有多少。

创建 Swap 文件

1
# fallocate -l 2G /swapfile

多大大小自己决定。

1
# ls -lh /swapfile
1
2
Output
-rw-r--r-- 1 root root 2.0G Apr 25 11:14 /swapfile

开启 Swap 文件

1
# chmod 600 /swapfile
1
# ls -lh /swapfile
1
2
Output
-rw------- 1 root root 2.0G Apr 25 11:14 /swapfile
1
# mkswap /swapfile
1
2
3
Output
Setting up swapspace version 1, size = 2048 MiB (1073737728 bytes)
no label, UUID=6e965805-2ab9-450f-aed6-577e74089dbf
1
# swapon /swapfile
1
# swapon --show
1
2
3
Output
NAME TYPE SIZE USED PRIO
/swapfile file 2048M 0B -2

用 free 验证一下

1
# free -h

类似输出

1
2
3
4
Output
total used free shared buff/cache available
Mem: 985M 84M 222M 680K 678M 721M
Swap: 2.0G 0B 2.0G

让 Swap 常驻

1
2
# cp /etc/fstab /etc/fstab.bak
# echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab

打开 Swap 设置

Adjusting the Swappiness Property

1
# cat /proc/sys/vm/swappiness
1
2
Output
60
1
# sysctl vm.swappiness=10
1
2
Output
vm.swappiness = 10
1
# vim /etc/sysctl.conf
1
2
// 在 /etc/sysctl.conf 底部加上
vm.swappiness=10

Adjusting the Cache Pressure Setting

1
# cat /proc/sys/vm/vfs_cache_pressure
1
2
Output
100
1
# sysctl vm.vfs_cache_pressure=50
1
2
Output
vm.vfs_cache_pressure = 50
1
# vim /etc/sysctl.conf
1
2
// 在 /etc/sysctl.conf 底部加上
vm.vfs_cache_pressure=50

完成基本设置退出

1
# exit

One More Thing —— 加入 ssh 免密码登陆

创建 ~/.ssh/id_rsa.pub 文件

确保你有 id_rsa.pub 这个文件

1
> cat ~/.ssh/id_rsa.pub

如果没有会输出类似信息

1
2
Output
cat: ~/.ssh/id_rsa.pub No such file or directory
1
> ssh-keygen -t rsa -b 4096 -C "[your_email@example.com]"

一路回车即可,如果提示有文件是否覆盖。最好确认老的 ~/.ssh/id_rsa.pub 文件是否有地方使用。

拷贝 id_rsa.pub

1
2
> scp  ~/.ssh/id_rsa.pub [用户名]@[IP4 地址]:~/.ssh
[用户名密码]
1
2
> ssh [用户名]@[IP4 地址]
[用户名密码]

如果服务器上有”~/.ssh/authorized_keys”文件

1
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

如果服务器上没有”~/.ssh/authorized_keys”文件,执行下面这条命令

1
$ cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys

完成基本设置退出

1
$ exit

编辑 ~/.ssh/config

1
> vim ~/.ssh/config
1
2
3
4
5
6
// 在 ~/.ssh/config 底部加上
host [自己取的vps代替名]
hostname [IP4 地址]
user [用户名]
port 22
IdentityFile ~/.ssh/id_rsa

链接验证

如果一切顺利,以后都可以直接 ssh 链接服务器了

1
> ssh [自己取的vps代替名]

硬广时间

我目前现生活在新西兰。

如果是新晋奶爸可以看看婴儿奶粉

如果逢年过节孝敬父母可以逛逛澳新保健品

如果经常熬夜或喝酒,你需要程序员神器-护肝片

大量澳新产品均可通过么么爪海购精选购买。

么么爪海购精选上的价格在海外直邮模式上有一定优势,但是跟国内电商上大量低价商品没法比。优势上只能用我自己那可能并不存在的人品担保都是正品。

2020年从零开始初始化一台 Ubuntu 19.10 的 VPS

https://iiiyu.com/2020/01/07/Initial-Server-Setup-with-Ubuntu-19-10-of-2020/

Author

Ewan Xiao

Posted on

January 7th 2020

Updated on

September 28th 2023

Licensed under

Comments