32 篇文章带有标签 “sysadmin”

在Ubuntu上配置apt镜像源

在国内使用官方的镜像源安装 Ubuntu 应用非常慢,通常配置国内的镜像源来加快速度,如阿里云。

这里以[阿里云镜像源]为例,替换掉官方源。

使用 http://mirrors.aliyun.com/ubuntu/ 替换 http://cn.archive.ubuntu.com/ubuntu/,需要查看一下文件 /etc/apt/sources.list,要替换的可能是 http://archive.ubuntu.com/ubuntu/

替换配置

sed -i 's/cn.archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list

基于配置的镜像源更新安装包的列表

apt-get update

国内主要的 Ubuntu 镜像源

  • 阿里云 http://mirrors.aliyun.com/ubuntu/
  • 网易 http://mirrors.163.com/ubuntu/
  • 清华大学 https://mirrors.tuna.tsinghua.edu.cn/ubuntu/
  • 中科大 https://mirrors.ustc.edu.cn/ubuntu/

Linux系统禁用交换分区

Kubernetes集群为了不影响性能要禁用交换分区。

查看交换区信息

$ swapon --show

临时禁用 /proc/swaps 中的交换分区(系统重启后会失效)

$ sudo swapoff -a

启用 /etc/fstab 中的所有交换区

$ sudo swapon -a

禁用交换分区(系统重启后也有效)

$ sudo sed -i '/swap/s/^/#/' /etc/fstab

查看是否禁用交换分区(什么也不显示代表成功)

$ swapon

参考资料

Linux系统DNS设置

之前在文件/etc/resolv.conf中设置了,过段时间总是自动恢复默认值。(注释中写的很详细,是不可编辑的由系统自动生成的文件)

/etc/resolv.conf # This file is managed by man:systemd-resolved(8). Do not edit. # # 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 must not access this file directly, but only through the # symlink at /etc/resolv.conf. To manage man:resolv.

Linux系统上修改用户名

今天同事安装了一台新的服务器Ubuntu20.04,但用户名和主机名不是我想要的,这里尝试了直接修改Linux文件的方式。

登录root用户

$ su - root

修改用户信息

/etc/passwd

# nano /etc/passwd
username:x:1000:1000:username:/home/username:/bin/bash

/etc/shadow

# nano /etc/shadow
username:D78/D2/DdYW.FVG.GlqDlZsZ4sK21gSxhDooqWlJtCVl3oUbDUTKtGxBWkCE3E/Oha40kjDrk0pBbsvT4TwtzuH61vYmnJ/GY.bAHWbVv1:18545:0:99999:7:::

/etc/group

# nano /etc/group
adm:x:4:syslog,username
cdrom:x:24:username
sudo:x:27:username
dip:x:30:username
plugdev:x:46:username
lxd:x:116:username
username:x:1000:

修改HOME路径

# mv /home/old_username /home/new_username

修改主机名 /etc/hostname # nano /etc/hostname hos

在Ubuntu上安装Docker

在 Ubuntu20.04 上安装 Docker

安装 Docker

$ curl -fsSL https://get.docker.com | sh -

把用户名(username)加入到 docker 组,这样以后在非 root 用户下不用每次操作都 sudo

$ sudo usermod -aG docker username

需要退出用户会话,重新登录方可生效。

卸载Docker

  • 删除Docker及其依赖
$ sudo apt-get remove --auto-remove docker
  • 删除所有数据
$ sudo rm -rf /var/lib/docker

基于Apt-Cacher NG创建本地Ubuntu存储库

安装 Apt-Cacher-NG

$ sudo apt install apt-cacher-ng
$ sudo systemctl enable apt-cacher-ng
$ sudo service apt-cacher-ng start

配置

$ sudo nano /etc/apt-cacher-ng/acng.conf
  • CacheDir: /var/cache/apt-cacher-ng
  • LogDir: /var/log/apt-cacher-ng

查看缓存目录的数据

$ du -sh /var/cache/apt-cacher-ng/
19M	/var/cache/apt-cacher-ng/

客户端配置

$ sudo nano /etc/apt/apt.conf.d/00aptproxy
Acquire::http::Proxy "http://172.16.33.157:3142";

安装软件

$ sudo apt install nodejs

参考资料 使用apt-cacher-ng的快速Debian/Ubuntu軟件包緩存代理設置 使用“ apt-cacher”设置“ apt-cache”服务器 使用 apt-mir

NFS配置

Ubuntu

服务端

  • 安装服务端
sudo apt install nfs-kernel-server
  • 查看服务状态
sudo systemctl status nfs-server
  • 查看开启的NFS协议
sudo cat /proc/fs/nfsd/versions
-2 +3 +4 +4.1 +4.2
  • 配置访问的路径
sudo nano /etc/exports
/data/nfs        172.16.33.0/24(rw,sync,fsid=0,crossmnt,no_subtree_check)
  • 应用配置
sudo exportfs -ra
  • 查看当前应用
sudo exportfs -v
  • 重启服务
sudo systemctl restart nfs-server

客户端

  • 安装客户端
sudo apt install nfs-common
  • 查看NFS服务器导出列表
showmount -e 172.16.33.157
Export list for 172.16.33.157:
/data/nfs 172.16.33.0/24,172.16.128.164
  • 挂载NFS
sudo mount -t nfs 172.16.33.157:/ $(pwd)/nfs
  • 移除挂载
sudo umount $(pwd)/nfs

参考资料 如何在Ubuntu 18.04上安装和配置NFS服务器 ubuntu18.

磁盘:分区-格式化-挂载

分区

列出块设备信息

lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0    7:0    0 71.3M  1 loop /snap/lxd/18772
loop1    7:1    0 55.4M  1 loop /snap/core18/1944
loop2    7:2    0 31.1M  1 loop /snap/snapd/10492
loop3    7:3    0 31.1M  1 loop /snap/snapd/10707
loop4    7:4    0 71.2M  1 loop /snap/lxd/18674
loop5    7:5    0 55.4M  1 loop /snap/core18/1932
sda      8:0    1  558G  0 disk 
├─sda1   8:1    1    1M  0 part 
└─sda2   8:2    1  558G  0 part /
sdb      8:16   1  2.2T  0 disk 

创建分区 sudo fdisk /dev/sdb Welcome to fdisk (util-linux 2.34). Changes will remain in memory only, until you decide to write them. Be careful before using the write command.

Linux设置时区

查看系统的时区

$ ll /etc/localtime
lrwxrwxrwx. 1 root root 35 2月  18 2020 /etc/localtime -> ../usr/share/zoneinfo/UTC

列出有效的时区

$ timedatectl list-timezones
Africa/Abidjan
......
America/New_York
......
Asia/Shanghai
......

设置时区

# timedatectl set-timezone Asia/Shanghai

查看系统的时区

$ ll /etc/localtime
lrwxrwxrwx. 1 root root 35 2月  18 2020 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai

SSH使用密匙登录

生成身份验证密钥

ssh-keygen -t rsa,在~/.ssh/目录下生成私匙id_rsa和公匙id_rsa.pub两个文件。

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa): 直接回车
Enter passphrase (empty for no passphrase): 直接回车
Enter same passphrase again: 直接回车
Your identification has been saved in /home/username/.ssh/id_rsa.
Your public key has been saved in /home/username/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:foo-YWwIv/a/HGEGt9P6vvmff/QjBGEvzlYM4hBWeR0 username@hostname
The key's randomart image is:
+---[RSA 2048]----+
|         +oo.ooE.|
|        . o..o+. |
|      . .  .. .+ |
|  .    o o  o.o  |
|   o o  S .  +.  |
|    o =+ +  .. ..|
|     + .+ . . o..|
|    o .o = . + .o|
|   . o+o=o+o. o..|
+----[SHA256]-----+

Linux系统网络配置

Ubuntu

修改IP

$ sudo nano /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
  ethernets:
    eno1:
      addresses:
      - 172.16.33.1/24
      gateway4: 172.16.33.254
      nameservers: {}
  version: 2

应用配置

$ sudo netplan apply
# 推荐使用 debug 参数
$ sudo netplan --debug apply

或者

sudo netplan generate

验证

ip a | grep eno1
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 172.16.33.1/24 brd 172.16.33.255 scope global eno1

参考资料 How to setup a static IP on Ubuntu Server 18.04 How to configure Network Settings in Ubuntu 18.

开启SSH服务

Ubuntu

  • 安装、卸载SSH服务
sudo apt-get install openssh-server
sudo apt-get remove openssh-server
  • 启动、停止、重启SSH服务
sudo service ssh start
sudo service ssh stop
sudo service ssh restart
  • 查询SSH服务状态
service ssh status
  • 配置文件(更改配置需要重启SSH服务)
sudo vi /etc/ssh/sshd_config
  • 连接SSH服务
ssh username@ip -p 22
  • 设置、移除SSH服务开机自启动
sudo update-rc.d ssh defaults
sudo update-rc.d ssh remove
  • 查看SSH服务设置的自启动
ls /etc/rc*
......
......
ls -l /etc/rc2.d/S02ssh
    lrwxrwxrwx 1 root root 13 12月 25 16:33 /etc/rc2.d/S02ssh -> ../init.d/ssh

参考资料