Ubuntu Server 20.04 使用
Hz 2021-11-10 Wednesday Ubuntu
Ubuntu server 安装及使用。常用功能配置。
1. 基础软件安装
apt install git
apt install bash
apt install dos2unix
apt install efibootmgr ethtool
apt install git-lfs
apt install gzip
apt install openssh-server
apt install mysql-server
apt install rsync
apt install sqlite3
apt install python3-pip
apt install php-fpm
apt install intel-gpu-tools
apt install net-tools nginx
# 开发服务器
apt install build-essential gdb cmake
apt install python3-virtualenv
apt install --no-install-recommends libboost-all-dev
apt install --no-install-recommends doxygen doxygen-doc doxygen-gui graphviz
apt install --no-install-recommends openjdk-11-jdk
# 仓库初始化
pip3 config set global.index-url 'https://pypi.tuna.tsinghua.edu.cn/simple'
pip3 config set global.extra-index-url https://pypi.tuna.tsinghua.edu.cn/simple
# npm
npm config set registry https://registry.npm.taobao.org
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2. 配置时区
查询当前时区状态
timedatectl show
1
查询可用时区配置
timedatectl list-timezones
1
时区配置
timedatectl set-timezone Asia/Shanghai
1
设置同步服务
# 安装依赖
yum install ntpdate
apt install ntpdate //
# 手动更新时间
ntpdate us.pool.ntp.org
# 配置定时任务
*/10 * * * * /usr/sbin/ntpdate us.pool.ntp.org | logger -t NTP
1
2
3
4
5
6
7
2
3
4
5
6
7
3. 用户及组设置
更新GID
useradd -u UID -M -s /bin/false -g GID userName
sudo groupmod -g GID groupName
1
2
2
4. 系统日志syslog使用
在Bash中使用
logger -p local0.notice -t ${0##*/}[$$] Print some here
1
5. 配置nodejs进程管理工具pm2
安装nodejs和npm
sudo apt-get install nodejs npm
1
配置仓库
npm config set registry "http://registry.npm.taobao.org/"
1
全局安装pm2
npm install -g pm2
1
配置启动项
# sudo pm2 startup 系统类型 -u 启动用户
sudo pm2 startup ubuntu -u www-data
# 查看情况
sudo systemctl status pm2-www-data
1
2
3
4
2
3
4
查询守护进程
# 查询
sudo -u www-data pm2 status
# 保存
sudo -u www-data pm2 save
1
2
3
4
2
3
4
6. 配置Docker
添加证书
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
1
添加仓库
添加/etc/apt/sources.list.d/docker.list
文件
内容如下:
deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
1
更新仓库并安装
sudo apt-get update
sudo apt-get install docker-ce docker-compose
1
2
2
配置Portainer
参考Add a Docker Standalone environment (opens new window)
参考资料
7. 安装HTTPS证书
下载
curl https://get.acme.sh | sh -s email=[email protected]
1
配置证书更新策略
# 检查证书更新时间
/usr/bin/openssl x509 -enddate -noout -in /root/.acme.sh/your-domain/your-domain.cer | sed 's/\s//g'
1
2
2
8. 安装Drone
- 官方文档 Gitea & Drone server (opens new window)
- 官方文档 Drone runner (opens new window)
- 部署 Drone (opens new window)
- nginx配置 (opens new window)
- How to install and configure Drone CI on a self-hosted server (opens new window)
- 基于 gitea + drone + docker 的 CI 流程实践 (opens new window)
- docker-compose搭建Gitea+Drone (Registory) (opens new window)
9. 自建镜像仓库
9.1 生成密码文件
sudo docker run --rm --entrypoint htpasswd httpd:2 -Bbn admin 12345678 > ./htpasswd
1
9.2 配置无证书的私有仓库地址
{
"insecure-registries": ["x.x.x.x:5000"]
}
1
2
3
2
3
9.3 启动私有仓库
sudo docker-compose -f docker-compose.yml up -d
1
10. 安装网络打印机
10.1 安装驱动
# 安装hp专用驱动
sudo apt install hplip
1
2
3
2
3
10.2 配置打印服务
# 编辑cupsd.conf
sudo vim /etc/cups/cupsd.conf
1
2
2
修改以下内容:
- Listen 192.168.xx.xxx:631
- Browsing On
- 修改配置
# Restrict access to the server...
<Location />
Order allow,deny
Allow @LOCAL
</Location>
# Restrict access to the admin pages...
<Location /admin>
AuthType Default
Require valid-user
Order allow,deny
Allow @LOCAL
</Location>
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
- 重启服务
sudo systemctl stop cups
sudo systemctl start cups
1
2
2
# 配置当前用户为打印机管理员
sudo usermod -a -G lpadmin
1
2
2
10.3 连接打印机
插上打印机USB
10.4 共享打印机
浏览器打开https://192.168.xx.xxx:631/admin
, 根据指引添加打印机
参考资料
- 在Ubuntu(Bonjour,IPP,Samba,AirPrint)上设置CUPS打印服务器 (opens new window)
- How to print to CUPS (running on another computer in the LAN)? (opens new window)
11 Cockpik 安装
11.1 修复无法安装补丁的问题
出现Cannot refresh cache whilst offline
的提示
# Change netplan config to use NM instead
# File path: /etc/netplan/50-cloud-init.yaml
network:
version: 2
renderer: NetworkManager # This is the changed line. Default is networkd
ethernets:
eno1: # This is different by hardware.
dhcp4: true
# Then apply it
sudo netplan try # This will disable systemd-networkd and enable network-manager.service
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
12 Docker compose
12.1 docker compose 常用命令
# 使用docker-compose构建
docker-compose build
# 清理无用的网络
docker network prune
1
2
3
4
5
2
3
4
5
12.2 指定网络
version: '3.7'
services:
service_name:
restart: always
image: xxx:latest
networks:
service_name_network:
ipv4_address: 172.1.0.2
networks:
service_name_network:
ipam:
config:
- subnet: 172.1.0.0/16
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
参考资料
13 KVM 虚拟机
13.1 创建网桥
仅需要配置IPv4, IPv6的地址客户机可以自动获取
修改netplan的配置文件,在/etc/netplan/
这个路径下
network:
ethernets:
物理网卡名:
dhcp4: true
version: 2
renderer: NetworkManager
bridges:
br0:
interfaces: [物理网卡名]
dhcp4: no
addresses: [所在网段:192.168.X.2/24]
gateway4: 所在网络的网关192.168.X.1
nameservers:
addresses: [所在网络的DNS服务器,192.168.X.1]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
13.2 查看支持的虚拟机类型
sudo apt -y install libosinfo-bin
osinfo-query os
1
2
2
13.3 创建虚拟机并启动
以Windows10 LTSC为例
virt-install \
--name=虚拟机名字 \
--os-variant=win10 \
--memory=4096 \
--vcpus=2 \
--disk path=虚拟机磁盘和大小(单位G),例如/opt/windows10.img,size=50 \
--accelerate \
--cdrom windows安装镜像路径:XXXX.iso \
--graphics vnc,listen=0.0.0.0,password=VNC密码,port=VNC端口 \
--network bridge=br0,model=virtio \
--noautoconsole
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
13.4 安装虚拟机系统
启动VNC客户端,并连接VNC服务,继续虚拟机系统安装
13.5 安装网卡驱动
下载 virtio-win.iso (opens new window) 驱动包,默认是最新版本,如果是win7,需要下载virtio-win-0.1.173-1
这个版本
# 查看虚拟机信息 一般在这个路径: /etc/libvirt/qemu/
# 也可以用 以下命令查看
virsh edit 虚拟机名字
virsh change-media 虚拟机名字 cdrom磁盘,一般是sdb --eject
virsh change-media 虚拟机名字 cdrom磁盘,一般是sdb virtio-win.iso --insert
1
2
3
4
5
2
3
4
5
13.6 使用共享
Windows 10 LTSC的共享访问有点问题,需要修改组策略:
计算机配置→管理模板→网络→Lanman工作站,点击里面的“启用不安全的来宾登录”,将它由“未配置”改为“已启用”,应用后即可。
14 日志清理
14.1 清理系统日志
# 查看日志情况
journalctl --disk-usage
# 控制日志目录大小,避免磁盘爆满
journalctl --vacuum-size=500M
# 控制日志保持时间,保留一周
journalctl --vacuum-time=1w
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
参考
15 远程窗口
15.1 安装软件
15.2 配置和连接
- 配置 Putty. Connection -> SSH -> X11
- 生成 Putty密钥, 使用puttygen,导入id_rsa私钥,生成Putty专用私钥
- 配置 公钥登录 Connection -> SSH -> Auth
- 启动 Xming
- Putty连接 远程服务器
- 查询 xauth 情况
xauth list
- 可以使用Xshell操作同一台服务器,一样可以打开远程窗口
15.3 参考
- Installing/Configuring PuTTy and Xming (opens new window)
- Linux安装X Window服务——远程显示GUI (opens new window)
- Use SSH Keys With PuTTY On Windows (opens new window)
- Putty OpenSSH SSH-2 private key (old PEM format) (opens new window)
- MobaXterm: SSH/X远程客户端, Xmanager的最佳免费替代品 (opens new window)
16 Samba 连接
挂载Samba共享目录
sudo apt-get update
sudo apt-get install cifs-utils
sudo mount -t cifs -o username=share //host/ShareFolder /mnt/target
# input password
1
2
3
4
2
3
4
How to Mount Samba Share in Ubuntu Linux (opens new window)
17 DNS服务
17.1 安装DNSMASQ
sudo apt install dnsmasq
1
17.2 修改DNSMASQ配置
server=114.114.114.114
server=8.8.8.8
address=/.xxxx.com/192.168.1.2
address=/.xxxx.com/192.168.1.3
1
2
3
4
2
3
4
17.3 启用远程访问DNS解析
Edit the /etc/init.d/dnsmasq
and remove --local-service
from DNSMASQ_OPTS
variable to fix it.