Category - Linux

2020-09-02 10:04:38    704    1    0

列表本目录所占空间

  1. du -sh ./

列表子目录所占空间

  1. du -sh ./*

列表子目录所占空间达到不足1M的

  1. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[K]"

列表子目录所占空间达到1M 以上

  1. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[M,G]"

列表子目录所占空间达到1G 以上

  1. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[G]"

每个级别的列表再按第一列数值排序

  1. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[K]"| sort -n
  2. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[M]"| sort -n
  3. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[G]"| sort -n

在排了序基础上再取最大10 个

  1. du -sh ./* | grep "^[0-9]\{1,\}.\{0,1\}[0-9]\{1,\}[G]" | sort -n | tail -n 10

2020-08-19 15:45:50    889    0    0
  1. # <file system> <mount point> <type> <options> <dump> <pass>

以下是官方对fstab 里六个字段用法的解释

The first field (fs_spec).

  1. For ordinary mounts, it will hold (a link to) a block special device node (as created by mknod(2)) for the device to be mounted, like /dev/cdrom' or/dev/sdb7'.

  2. LABEL= < label > or UUID= < uuid >
    LABEL=< label> or UUID=< uuid> may be given instead of a device name. This is the recommended method, as device names are often a coincidence of hardware detection order, and can change when other disks are added or removed. For example, LABEL=Boot' orUUID=3e6be9de-8139-11d1-9106-a43f08d823a6'. (Use a filesystem-specific tool like e2label(8), xfs_admin(8), or fatlabel(8) to set LABELs on filesystems).
    第一个字段可以是分区名,或标签名,设备id, 用来标记这一行所表示 的文件系统所在的设备块

  1. root@vm1:/vob/GuaKing/DevOps# cat /etc/fstab
  2. # <file system> <mount point> <type> <options> <dum
? qcow2 ? ? 分区 ?    2020-08-13 15:04:24    1110    1    0

官方参考文档

diskimage-builder code
https://opendev.org/openstack/diskimage-builder

官方分区设置

https://docs.openstack.org/diskimage-builder/latest/user_guide/building_an_image.html

If you wish to customise the top-level block-device-default.yaml file
from one of the block-device-* elements, set the environment variable
DIB_BLOCK_DEVICE_CONFIG. This variable must hold YAML structured
configuration data or be a file:// URL reference to a on-disk
configuration file.


在自己自定义的element 的根目录下,创建一个文件block-device-default.yaml,将分区方案按yaml 文件格式配置其中,如果element 下方有一个block-device-default.yaml,则会优先使用用户定义的分区配置,如果用户无定制,则使用默认配置:
https://opendev.org/openstack/diskimage-builder/src/branch/master/diskimage_builder/elements/block-device-mbr/block-device-default.yaml

或使用变量DIB_BLOCK_DEVICE_CONFIG定义分区配置,将block-device-default.yaml的文本赋值给DIB_BLOCK_DEVICE_CONFIG

https://github.com/GuaKing/qcow2build.git

官方的一个默认配置:

磁盘全部容量分配r

? qcow2 ? ? 分区 ?    2020-08-13 13:39:52    1050    1    0

ubuntu 扩展磁盘后如何扩展分区

背景: 使用kvm 安装 qcow2 文件 ,启用一个ubuntu 虚拟机。

直接扩展qcow2 镜像文件 的磁盘大小,并指定扩展在分区N

  1. qemu-img info a.qcow2
  2. qemu-img create -f qcow2 -o preallocation=metadata b.qcow2 60G
  3. virt-resize --expand /dev/sda1 a.qcow2 b.qcow2
  4. #第一个命令查看a.qcow2 镜像磁盘有多大(a 包含我们要安装系统镜像)
  5. #第二个命令创建一块 60G 的镜像磁盘文件(即最终磁盘容量,b须比a大)
  6. #第三个命令,将a 镜像内容拷到b镜像中,并将空闲空间(b'size-a'size)扩展到/dev/sda1 分区上。
  7. #即,如果a.qcow2 原磁盘大小是50G ,执行完命令后b.qcow2 除了是a.qcow2 的完整拷贝外,b.qcow2中的/deb/sda磁盘大小为60G,其中/dev/sda1 分区 比a.qcow2 中/dev/sda1 多了10G.

扩展磁盘,不分配到指定分区,进入系统后再分配

  1. qemu-img resize a.qcow2 +5G
  2. virsh destroy vm
  3. virsh start vm
  4. ## enter vm
  5. fdisk -l | grep sda (/dev/sda 磁盘大小增加了5g, 但是分区 /dev/sda1 未拿到这些空间)
  6. parted /dev/sda (将磁盘的未分配空间,分配给分区sda1
  7. (parted) p
  8. (parted) resizepart 1
  9. (parted) quit
  10. ## 执行log 如下:
  11. root@CASA-MOBILE:~# parted /dev/sda
  12. (parted) p <== remember space of all disk /dev/sda ,and remember the Number of sda1
? 分区 ?    2020-07-20 14:14:45    731    0    0

在使用diskimage-builder生成qcow2 镜像时,磁盘空间足够,但是执行到中间时会报
No space left
用df -h 查看时,/目录下的空间足够。
于是再执行,在执行过程中发现,diskimage-builder在执行过程解包和拷贝过程是会将一个/tmp/dib_build.xxxxxx的临时目录挂载在tmpfs上,而当前系统的tmpfs 只有3.9G 。

  1. root@vm1:~# df -h| grep tmpfs
  2. tmpfs 797M 3.3M 793M 1% /run
  3. tmpfs 3.9G 0 3.9G 0% /dev/shm
  4. tmpfs 5.0M 0 5.0M 0% /run/lock
  5. tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
  6. tmpfs 797M 0 797M 0% /run/user/0
  7. tmpfs 3.9G 3.8G 154M 97% /tmp/dib_build.whmZyNdv
  8. tmpfs 3.9G 0 3.9G 0% /tmp/dib_image.6Jdr8dSL

解决:

了解tmpfs是在哪里分配及分配规则:

https://wiki.archlinux.org/index.php/Tmpfs

tmpfs is a temporary filesystem that resides in memory and/or swap
partition(s). Mounting directories as tmpfs can be

2020-07-20 13:43:32    695    1    0

普通场景:

在ubuntu系统上,安装一系列依赖,及打包工具diskimage-builder, 然后在其上执行包含
disk-image-create 命令的脚本,生成qcow2/vmdk镜像。

容器内无差别构建:

每个人想拥有自己的构建环境 ,都需要另外在自己的系统上安装所需要的软件列表,会存在系统版本和软件版本不一的情况。

将其上环境构建成固定的dokcer 镜像, 每个人不管自己的设备是redhat, centos, ubuntu,都可以使用可控版本的打包容器进行构建,并且使用的软件版本是一致的。使用的镜像基于基础系统另装了哪些工具,都是可追溯可控版的。

Dockerfile

将需要定义的变量,需要预装的软件,都写进dockerfile

  1. FROM ubuntu:bionic AS DIB-env
  2. COPY sources.list /etc/apt/
  3. RUN apt-get update
  4. RUN apt-get install -y python-pip
  5. RUN apt-get install -y qemu-utils
  6. RUN apt-get install -y sudo
  7. RUN apt-get install -y curl
  8. RUN apt-get install -y squashfs-tools
  9. # Install python packages
  10. RUN pip install --upgrade pip
  11. RUN pip install networkx -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
  12. RUN pip install diskimage-builder -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
  13. ENV TEMP_PATH=/vob_temp
  14. RUN mkdir -p ${TEMP_PATH}
  15. COPY temp.tar.gz ${TEMP_PATH}
  16. WORKDIR ${TEMP_PATH}
  17. RUN tar zxvf temp.tar.gz
  18. RUN r
? python ? ? docker ?    2020-07-14 15:38:22    1222    0    0


Dockerfile 里的 pip install 总是failed .但是直接执行可以成功。
估计是docker build 时默认使用的pip 源有太慢导致

Dockerfile 里涉及的报错语句

  1. RUN pip install networkx

报错如下

  1. Step 9/15 : RUN pip install networkx
  2. ---> Running in 3a3f19067b2f
  3. Collecting networkx
  4. Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed ox/
  5. Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed ox/
  6. Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.python.org', port=443): Read timed ox/
  7. Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutErr
2020-04-28 15:32:23    909    0    0

系统:ubuntu

软件:dnsmasq

安装:

  1. apt-get install -y dnsmasq

配置 /etc/dnsmasq.conf:

  1. root@node248:/etc/resolvconf/resolv.conf.d# cat /etc/dnsmasq.conf | grep -v "^#|^$"
  2. port=53
  3. resolv-file=/etc/resolv.conf
  4. strict-order
  5. listen-address=172.0.11.248
  6. addn-hosts=/etc/hosts

配置 /etc/hosts

  1. root@node248:/etc/resolvconf/resolv.conf.d# cat /etc/hosts
  2. #.....
  3. 172.0.11.2 vm2
  4. 172.0.11.3 vm3
  5. 172.0.11.4 vm4.com

配置 上游 nameserver

  1. 使用服务器本身的dns 列表,则resolv-file=/etc/resolv.conf

  2. 使用服务器上另外的dns 列表文件,即上游dns,则resolv-file=/etc/dnsmasq.d/ 此目录下放置 *conf

重启服务:

  1. #14
  2. service dnsmasq restart
  3. #18
  4. systemctl restart dnsmasq

解析过程:

客户端将此服务器ip 设置为dns 之后,查找域名时,会通过服务器的/etc/hosts 去解析域名,然后再通过resolv-file 中列的dns 去解析。

附不同ubuntu系统dns-nameserver的设置:

UBUNTU 14

  1. root@vm1:/etc/dnsmasq.d# cat /etc/resolvconf/resolv.conf.d/base
  2. nameserver 172.0.11.248
  3. root@vm1:/etc/dnsmasq.d# cat /etc/resolv.conf
  4. # Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
  5. #
? shell ?    2020-04-03 10:07:53    2897    1    0

 

设置密码复杂度

http://www.linux-pam.org/Linux-PAM-html/sag-pam_cracklib.html

安装libpam-cracklib

apt-get install libpam-cracklib

 

修改文件 /etc/pam/common-password 中的配置,修改完保存即生效。

如果有加enforce_for_root​ ,则对root 有效。

默认对root无效。 当root 为操作修改的用户时,虽然会提示,但是仍能修改成功。

 

vi common-password

password        requisite                       pam_cracklib.so retry=3 minlen=8 difok=3 dcredit=-1 ocredit=-1 enforce_for_root​

以上例子为:可重试三次,密码最少8个字符,与旧密码至少3个字符不同,至少一个数字,至少一个符号,对root操作也有效。


 

http://www.linux-pam.org/Linux-PAM-html/sag-pam_cracklib.html

-----------------


difok=N

This argument will change the default of 5 for the number of character changes in the new password that differentiate it from the old password.

minlen=N

The minimum acceptable size for the new password (plus one if credits are not disabled which is the default). In addition to the number of characters in the new password, credit (of +1 in length) is given for each different kind of character (other, upper, lower and digit). The default for this parameter is 9 whic

2020-03-19 15:24:02    723    0    0

新装系统 ubuntu 时,安装界面会让我们新建一个非root帐号,这个帐号有sudo 权限,记住装机时设置的用户密码。

在安装界面 安装预装软件时,最好将ssh勾上, 这样在初始设置时会比较方便一点。


1. 从普通用户登入root

Login: username

password:username-password

 

进入系统之后,执行

sudo passwd​

输入三次密码,第一次为用户自己的密码,第二次第三次为root的新密码和密码确认。

看到提示密码修改成功之后,执行su ,  输入刚刚设置的root 密码,切换到root 用户。

su​

2.设置静态ip

ubuntu 18 和 14 的网络配置方式不同,ubuntu 18 需要设置netplan下的XXXX.yaml

其中注意使用yaml 的格式。

注意gateway4 表示ip4的gw ,以及gw 这一行不需要方括号 [ ]

dns 的ip应该再缩进一层再书写。

修改完毕之后,netplan apply 使用配置生效。

root@vm3:~# cd /etc/netplan
root@vm3:~# cat /etc/netplan/01-netcfg.yaml 
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      addresses: [172.0.11.155/24]
      gateway4: 172.0.11.1
      nameservers:
        addresses: [8.8.8.8, 114.114.114.114]
      dhcp4: no
      dhcp6: no
root@vm3:~# netplan apply 



3. 允许root 使用ssh 远程登录

修改PermitRootLogin项的值为yes , 然后重启ssh 服务

root@vm3:~# cat /etc/ssh/sshd_config | grep Root

# the setting of "PermitR
3/7