操作系统-服务器

abstiger大约 4 分钟

操作系统-服务器Server

基于 linux 的发行版

  • https://www.baeldung.com/linux/
  • https://www.baeldung.com/linux/linux-administration-series

基本命令Shell

https://www.baeldung.com/linux/linux-scripting-series

  • bash
  • find
  • grep
  • sed
  • awk

系统工具Systemd

Systemdopen in new window 是 Linux 系统工具,用来启动守护进程,同时还提供各种实用工具,已成为大多数发行版的标准配置。

参考:https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.htmlopen in new window

参考:https://systemd-by-example.com/open in new window

修改主机名称

# 查看主机名称
hostnamectl

# 修改主机名称
sudo hostnamectl set-hostname dev.krproject.org

修改本地语言

# 查看本地化设置
localectl

# 设置本地化参数。
sudo localectl set-locale LANG=zh_CN.UTF-8

修改时间时区

# 查看时间时区
timedatectl

# 设置时区为上海
sudo timedatectl set-timezone "Asia/Shanghai"

查看系统日志

Systemd 统一管理所有 Unit 的启动日志。带来的好处就是,可以只用journalctl一个命令,查看所有日志(内核日志和应用日志)。

# 查看某个 Unit 的日志
sudo journalctl -u nginx.service
sudo journalctl -u nginx.service --since today

# 显示尾部指定行数的日志
sudo journalctl -n 20

# 实时滚动显示最新日志
sudo journalctl -f

服务系统-Ubuntu

镜像操作系统选择为:Ubuntu 20.04 LTS focal,官方文档在: https://ubuntu.com/server/docsopen in new window

基础的 unix 操作不在本文中赘述了,主要记录在 Ubuntu 20.04 系统上的一些个性化基础操作

软件包管理APT

基本使用参考:https://ubuntu.com/server/docs/package-managementopen in new window

  • 软件源修改

参考Aliyun Ubuntu镜像配置:https://developer.aliyun.com/mirror/ubuntuopen in new window

# 备份原官方apt源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.offical.bak 

# 修改apt源为阿里镜像源
sudo echo "
# aliyun ubuntu server 20.04 focal mirror
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
" > /etc/apt/sources.list
  • 软件包更新

软件版本不建议过度追新,在软件没有重大bug,影响正常使用的前提下,一般更新频率可在三个月或更长,看自己心情和新版本的功能诱惑了~

这样我们可以将 /etc/apt/sources.list.d/ 里的文件全部下放到 jail 目录中:

mkdir /etc/apt/sources.list.d/jail/

mv /etc/apt/sources.list.d/*.list /etc/apt/sources.list.d/jail/

在需要更新时,将对应源文件挪出来即可~

应用包管理

https://snapcraft.io/

当你在安装完snap后,你会发现在在根目录下会出现如/dev/loop0的挂载点,这些挂载点正是snap软件包的目录。Snap使用了squashFS文件系统,一种开源的压缩,只读文件系统,基于GPL协议发行。一旦snap被安装后,其就有一个只读的文件系统和一个可写入的区域。应用自身的执行文件、库、依赖包都被放在这个只读目录,意味着该目录不能被随意篡改和写入。

squashFS文件系统的引入,使得snap的安全性要优于传统的Linux软件包。同时,每个snap默认都被严格限制(confined),即限制系统权限和资源访问。但是,可通过授予权限策略来获得对系统资源的访问。这也是安全性更好的表现。

  1. 安装snap
sudo snap install code //安装code snap
  1. 卸载snap
sudo snap remove code
  1. 搜索snap
snap find code
  1. 查看snap信息
snap info code
  1. 查看已安装的snap
snap list
  1. 更新snap
sudo snap refresh code channel=latest/stable //channel来指定通道版本

网络配置

# 修改网络计划配置
sudo echo "
# This is the network config written by 'subiquity'
network:
  ethernets:
    ens160:
      dhcp4: false
      optional: true
      addresses: [192.168.199.110/24]
      gateway4: 192.168.199.1
      nameservers:
        addresses: [114.114.114.114,8.8.8.8]
  version: 2
" > /etc/netplan/00-installer-config.yaml

# 应用网络计划配置
sudo netplan apply

防火墙设置

基本使用参考:https://ubuntu.com/server/docs/security-firewallopen in new window

# 启用
sudo ufw enable

# 关闭
sudo ufw disable 

# 查看防火墙状态
sudo ufw status 

# 允许外部访问80 tcp端口
sudo ufw allow 80/tcp 

卸载 cloud-init

Prevent start

  • Create an empty file to prevent the service from starting
sudo touch /etc/cloud/cloud-init.disabled

Uninstall

  • Disable all services (uncheck everything except "None"):
sudo dpkg-reconfigure cloud-init
  • Uninstall the package and delete the folders
sudo dpkg-reconfigure cloud-init
sudo apt-get purge cloud-init
sudo rm -rf /etc/cloud/ && sudo rm -rf /var/lib/cloud/
  • Restart the computer
sudo reboot

Sources

  • https://cloudinit.readthedocs.io/en/latest/topics/boot.html#generator
  • https://www.blackmoreops.com/2019/04/19/remove-cloud-init-from-ubuntu/

服务系统-CentOS

软件包管理yum

服务系统-OpenWRT

软件包管理opkg

OpenEuler

国产开源欧拉操作系统

Loading...