Author Archive by herui

非root用戶沒有權限運行docker命令

問題描述

[[email protected] ~]$ docker ps
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.26/containers/json: dial unix /var/run/docker.sock: connect: permission denied

問題分析

Manage Docker as a non-root user

The docker daemon binds to a Unix socket instead of a TCP port. By
default that Unix socket is owned by the user root and other users can
only access it using sudo. The docker daemon always runs as the root
user.

If you don’t want to use sudo when you use the docker command, create
a Unix group called docker and add users to it. When the docker daemon
starts, it makes the ownership of the Unix socket read/writable by the
docker group.

解決辦法

# 添加docker用户组
[[email protected] ~]$ sudo groupadd docker

# 将登陆用户加入到docker用户组中
[[email protected] ~]$ sudo gpasswd -a $USER docker

# 更新用户组
[[email protected] ~]$ sudo newgrp docker

# 重啓 docker 服務
[[email protected] ~]$ sudo systemctl restart docker.service

# 授權 docker.sock 
[[email protected] ~]$ sudo chmod a+rw /var/run/docker.sock

批量新增用戶至docker用戶組脚本

#!/bin/bash
#author by Michael Ho

# 用戶列表
user_array=(
    zhouj
    zhangmy
    caimz
    yuy
)

# 新增用戶到docker群組中
add_user() {

    if [[ $EUID -ne 0 ]]; then
        echo -ne "\033[31mThis scripts must be run as root ..\033[0m"
        exit 1
    fi

    groupadd docker

    for i in ${user_array[*]}; do
        gpasswd -a $i docker
        echo -ne "\033[32m 已將 $i 加入到 docker 用戶組! \033[0m"
    done

    newgrp docker
}

# main函式
main() {
    add_user

    # 重啓 docker 服務
    systemctl restart docker.service

    # 授權 docker.sock 
    chmod a+rw /var/run/docker.sock
}

# 程式入口
main

windows安裝pip報錯問題

安裝pip時報錯現象如下

D:\>python get-pip.py
C:\Python38\lib\site-packages\setuptools\distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
  warnings.warn(
Looking in indexes: http://pypi.douban.com/simple
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.douban.com'.
ERROR: Could not find a version that satisfies the requirement pip
ERROR: No matching distribution found for pip
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.douban.com'.

D:\>curl https://bootstrap.pypa.io/get-pip.py | python3。
'python3。' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

D:\>
D:\>curl https://bootstrap.pypa.io/get-pip.py | python
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1882k  100 1882k    0     0   171k      0  0:00:11  0:00:11 --:--:--  152k
C:\Python38\lib\site-packages\setuptools\distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
  warnings.warn(
Looking in indexes: http://pypi.douban.com/simple
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.douban.com'.
ERROR: Could not find a version that satisfies the requirement pip
ERROR: No matching distribution found for pip
WARNING: The repository located at pypi.douban.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.douban.com'.

解決辦法

  • 在系统目录C:\Users\用户\AppData\Roaming新建pip文件夹
  • 在pip文件夹下新建pip.ini文件,并写入如下配置:
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com

注意

  • 安裝Windows版python.exe時,一定要以管理員模式打開並安裝,否則pip要人肉安裝,比如我
  • curl get-pip.py 時,可以在任意盤符目錄,但當前用戶一定能可以訪問到該目錄
  • 這個報錯是 pip 版本 >=7 時,使用鏡像源時,會提示源地址不受信任或不安全,需要ssl安全認證

CentOS7根目录磁盘扩容(/dev/mapper/centos-root 空间不足)

0.查看根分区大小

[[email protected] ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
devtmpfs                         63G     0   63G   0% /dev
tmpfs                            63G     0   63G   0% /dev/shm
tmpfs                            63G  1.3G   62G   2% /run
tmpfs                            63G     0   63G   0% /sys/fs/cgroup
/dev/mapper/cl_geekthings-root   50G   38G   13G  76% /
/dev/sda1                      1014M  311M  704M  31% /boot
/dev/mapper/cl_geekthings-home  918G   12G  906G   2% /home
tmpfs                            13G   12K   13G   1% /run/user/42
tmpfs                            13G     0   13G   0% /run/user/0
overlay                          50G   38G   13G  76% /var/lib/docker/overlay2/70dffe831895bb55d79b4a8f483364a4f736637ccb2f6694f1cf82ba7ff1743d/merged

1.查看磁盘编号

[[email protected] ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2

2.这里在vMware vCenter中新增一块硬盘(编辑->新增设备) 再次查看磁盘

[[email protected] ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb

3.对新增硬盘进行分区, 这里新增的磁盘名称叫 /dev/sdb

[[email protected] ~]# fdisk /dev/sdb

# 输入 m, 得到帮助,可以按照提示操作进行分区了
# 因为这块硬盘只有 100G,所以我只打算分一个区,这里是需要输入 n,然后接下来都按 Enter 键,默认即可,最后输入 w 保存退出
[[email protected] ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

4.将分区后的磁盘格式化成xfs类型

[[email protected] ~]# mkfs.xfs /dev/sdb1

5.创建pv

[[email protected] ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.

6.查看vg组

[[email protected] ~]# vgs
  VG            #PV #LV #SN Attr   VSize    VFree
  cl_geekthings   1   3   0 wz--n- <999.00g 4.00m

7.扩展vg

[[email protected] ~]# vgextend cl_geekthings /dev/sdb1
  Volume group "cl_geekthings" successfully extended

[[email protected] ~]# vgs
  VG            #PV #LV #SN Attr   VSize VFree  
  cl_geekthings   2   3   0 wz--n- 1.17t 200.00g

8.扩展lv

[[email protected] ~]# lvextend -L +200G /dev/mapper/cl_geekthings-root
  Size of logical volume cl_geekthings/root changed from 50.00 GiB (12800 extents) to 250.00 GiB (64000 extents).
  Logical volume cl_geekthings/root successfully resized.

9.系统重新读取大小

[[email protected] ~]# xfs_growfs /dev/mapper/cl_geekthings-root
meta-data=/dev/mapper/cl_geekthings-root isize=512    agcount=4, agsize=3276800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=13107200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6400, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 13107200 to 65536000

10.查看扩容是否成功

[[email protected] ~]# df -h
Filesystem                      Size  Used Avail Use% Mounted on
devtmpfs                         63G     0   63G   0% /dev
tmpfs                            63G     0   63G   0% /dev/shm
tmpfs                            63G  1.3G   62G   2% /run
tmpfs                            63G     0   63G   0% /sys/fs/cgroup
/dev/mapper/cl_geekthings-root  250G   38G  213G  16% /
/dev/sda1                      1014M  311M  704M  31% /boot
/dev/mapper/cl_geekthings-home  918G   12G  906G   2% /home
tmpfs                            13G   12K   13G   1% /run/user/42
tmpfs                            13G     0   13G   0% /run/user/0
overlay                         250G   38G  213G  16% /var/lib/docker/overlay2/70dffe831895bb55d79b4a8f483364a4f736637ccb2f6694f1cf82ba7ff1743d/merged
[[email protected] ~]# lsblk
NAME                   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                      8:0    0  1000G  0 disk 
├─sda1                   8:1    0     1G  0 part /boot
└─sda2                   8:2    0   999G  0 part 
  ├─cl_geekthings-root 253:0    0   250G  0 lvm  /
  ├─cl_geekthings-swap 253:1    0  31.5G  0 lvm  
  └─cl_geekthings-home 253:2    0 917.5G  0 lvm  /home
sdb                      8:16   0   200G  0 disk 
└─cl_geekthings-root   253:0    0   250G  0 lvm  /
sr0                     11:0    1   4.1G  0 rom

CentOS記錄所有用戶登陸操作詳細日志

setp1 : 在 /etc/profile 末尾追增以下代碼

############################ 用戶登陸操作歷史脚本 ############################

USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
HISTDIR=/tmp/log/history
DT=`date +%Y-%m-%d`

if [ -z $USER_IP ]; then
    USER_IP=`hostname`
fi

pdf="."
if [[ ! $USER_IP == *${pdf}* ]]; then
    USER_IP=`hostname`
fi

if [ ! -d $HISTDIR ]; then
    mkdir -p $HISTDIR
    chmod 300 $HISTDIR
fi

if [ ! -d $HISTDIR/${DT} ]; then
    mkdir -p $HISTDIR/${DT}
    chmod 300 $HISTDIR/${DT}
fi

export HISTFILESIZE=10000 # 定義文件中最多的只有HISTFILESIZE行
export HISTSIZE=10000 # 定義history命令輸出的記錄數
DT2=`date +%Y-%m-%d_%H:%M:%S`
export HISTFILE="$HISTDIR/${DT}/${LOGNAME}@${USER_IP}_$DT2"
export HISTTIMEFORMAT="%Y-%m-%-d_%H:%M:%S # "
chmod 600 $HISTDIR/${DT}/*_* 2>/dev/null

##############################################################################

step2: 刷新生效

[[email protected] ~]# source /etc/profile

CentOS两种方式安装7z命令

一、安装(以CentOS为例)

1.yum安装(能访问互联网的,用此方式)

建议更换国内源 :中国科学技术大学镜像源

2.二进制安装

7z 最新版本链接
7z 官网

  • 下载到/usr/local
    [[email protected] ~]# cd /usr/local && wget https://nchc.dl.sourceforge.net/project/p7zip/p7zip/16.02/p7zip_16.02_src_all.tar.bz2
  • 解压
    [[email protected] local]# sudo tar xjvf p7zip_16.02_src_all.tar.bz2
  • 安装
    [[email protected] local]# cd p7zip_16.02 && sh install.sh

二、命令

[[email protected] opt]# 7za x frontend_7_06_JsTest_0303.7z  -r -o./
# 参数含义:
# x  代表解压缩文件,并且是按原始目录树解压(还有个参数 e 也是解压缩文件,但其会将所有文件都解压到根下,而不是自己原有的文件夹下)
# frontend_7_06_JsTest_0303.7z是压缩文件,这里我用phpadmin做测试。这里默认使用当前目录下的phpMyAdmin-3.3.8.1-all-languages.7z
# -r 表示递归解压缩所有的子文件夹
# -o 是指定解压到的目录,-o后是没有空格的,直接接目录。

[[email protected] opt]# 7za a -t7z -r frontend_7_06_JsTest_0303.7z /opt/frontend_7_06_JsTest_0303/*
# 参数含义:
# a  代表添加文件/文件夹到压缩包
# -t 是指定压缩类型,这里定为7z,可不指定,因为7za默认压缩类型就是7z
# -r 表示递归所有的子文件夹
# frontend_7_06_JsTest_0303.7z 是压缩好后的压缩包名
# /opt/frontend_7_06_JsTest_0303/*   :是压缩目标。
# 7za不仅仅支持.7z压缩格式,还支持.tar.bz2等压缩类型的。如上所述,用-t指定即可。

CentOS7下使用Oracle 11g sqlplus信息显示乱码

CentOS7下使用Oracle 11g sqlplus信息显示乱码

现象
[[email protected] ~]$ sqlplus /nologin

SQL*Plus: Release 11.2.0.1.0 Production on ������ 1�� 20 23:43:44 2021

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

SQL*Plus: Release 11.2.0.1.0 Production

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

ʹ�� SQL*Plus ִ�� SQL, PL/SQL �� SQL*Plus ���䡣

�÷� 1: sqlplus -H | -V

    -H             ��ʾ SQL*Plus �汾��
                   �÷�������
    -V             ��ʾ SQL*Plus �汾��

�÷� 2: sqlplus [ [<option>] [{logon | /nolog}] [<start>] ]

  <option> Ϊ: [-C <version>] [-L] [-M "<options>"] [-R <level>] [-S]

    -C <version>   ����Ӱ���������ļ���������Ϊ
                   <version> ָ���İ汾���ð汾����
                   "x.y[.z]" ��ʽ������, -C 10.2.0
    -L             ֻ���Ե�¼һ��, ������
                   �ڳ���ʱ�ٴ���ʾ��
    -M "<options>" �����������Զ� HTML ���ǡ�ѡ��
                   �ĸ�ʽΪ:
                   HTML [ON|OFF] [HEAD text] [BODY text] [TABLE text]
                   [ENTMAP {ON|OFF}] [SPOOL {ON|OFF}] [PRE[FORMAT] {ON|OFF}]
    -R <level>     ��������ģʽ, �Խ������ļ�ϵͳ������
                    SQL*Plus �����������
                   �� 1, 2 �� 3���������Ƽ���Ϊ -R 3, �ü���
                   �������ļ�ϵͳ������
                   �����û����
    -S             ��������ʾģʽ, ��ģʽ����
                   ������ SQL*Plus ����, ��ʾ�ͻ���
                   ����ʾ��
解决
# 查看 Oracle 环境变量
[[email protected] ~]$ echo $NLS_LANG
SIMPLIFIED CHINESE_CHINA.ZHS16GBK

# 修改环境变量为 SIMPLIFIED CHINESE_CHINA.AL32UTF8 [这种修改方式,只在当前 bash 中生效]
[[email protected] ~]$ export NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8"

# 检查是否还乱码 (如下图所示)
[[email protected] ~]$ sqlplus /nolog

SQL*Plus: Release 11.2.0.1.0 Production on 星期三 1月 20 23:21:51 2021

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

SQL> exit
[[email protected] ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.1.0 Production on 星期三 1月 20 23:22:05 2021

Copyright (c) 1982, 2009, Oracle.  All rights reserved.

连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
扩展阅读
  • NLS_LANG 是一个环境变量,用于定义语言,地域以及字符集属性。对于非英语的字符集,NLS_LANG 的设置就非常重要。

  • NLS(National Language Support) 当我们设定一种 NLS 的时候实际上我们是为 Oracle 在存放数据时指定了他的语种所特有的一些表达形式,比如我们选择 Chinese, 那么它的中文字符如何存放,按什么规则排序,货币如何表示,日期格式也就被设定了。

  • NLS_LANG 参数由以下部分组成

    NLS_LANG=<Language>_<Territory>.<Clients Characterset>
    • Language

      • Oracle(错误)信息的语言
      • 日和月份的名称
        注意:NLS_LANGUAGE与插入和查询的数据的语言无关。
    • Territory

      • 货币和数字格式
      • 计算星期和天数的范围和惯例
    • Clients Characterset(客户端字符集)

      • 定义Oracle客户端,客户应用使用的编码
      • 或者它要符合您 Microsoft Windows 代码页( GUI 工具的 ACP, 命令提示符的 CHCP 值)
      • 或者为 Unicode WIN32 应用设置为 UTF8/AL32UTF8

CentOS7将新增硬盘挂载到已有目录下

已知在虚拟机 VMWare 给指定服务器新增一块 100G 大小的硬盘
以 /data 目录为例,将 /data 目录挂载到新增磁盘上

0.对新增磁盘进行分区(关于 Linux 分区知识,请点击这里)
# 这里新增的磁盘名称叫 /dev/sdb
[[email protected] ~]# fdisk /dev/sdb

# 输入 m, 得到帮助,可以按照提示操作进行分区了
# 因为这块硬盘只有 100G,所以我只打算分一个区,这里是需要输入 n,然后接下来都按 Enter 键,默认即可,最后输入 w 保存退出
[[email protected] ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
1.将分区后的磁盘格式化成xfs类型
[[email protected] ~]# mkfs.xfs /dev/sdb1
2.创建临时挂载点至该硬盘上
[[email protected] ~]# mkdir -p /mnt/data
[[email protected] ~]# mount /dev/sdb1  /mnt/data

# 查看有没有挂载成功
[[email protected] ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             7.8G     0  7.8G   0% /dev
tmpfs                7.8G     0  7.8G   0% /dev/shm
tmpfs                7.8G   11M  7.8G   1% /run
tmpfs                7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/mapper/cl-root   45G   39G  6.6G  86% /
/dev/sda1            3.0G  221M  2.8G   8% /boot
tmpfs                1.6G     0  1.6G   0% /run/user/0
/dev/sdb1            100G     0    0G   0% /mnt/data
3.将 /data/ 下面的数据迁移到临时目录 /mnt/data
[[email protected] ~]# rsync -avzP /data /mnt/data/
4.删除原来的 /data 目录下文件
[[email protected] ~]# rm -rf /data/*
5.卸载 /dev/sdb1 硬盘
[[email protected] ~]# umount /dev/sdb1
6.设置开机挂载
[[email protected] ~]# vim /etc/fstab

# 末尾追增一行 
/dev/sdb1     /data     xfs     defaults    1 2

# 保存退出
7.挂载 /etc/fstab 中未挂载的分区
[[email protected] ~]# mount -a
8.检查挂载是否成功
[[email protected] ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
devtmpfs             7.8G     0  7.8G   0% /dev
tmpfs                7.8G     0  7.8G   0% /dev/shm
tmpfs                7.8G   11M  7.8G   1% /run
tmpfs                7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/mapper/cl-root   45G   39G  6.6G  86% /
/dev/sda1            3.0G  221M  2.8G   8% /boot
tmpfs                1.6G     0  1.6G   0% /run/user/0
/dev/sdb1            100G   15G   86G  15% /data

docker运行目录/var/lib/docker目录迁移

docker运行目录/var/lib/docker目录迁移

0.停止docker服务
systemctl stop docker
1.创建docker新目录
mkdir -p /data/docker/lib
2.开始迁移
rsync -avzP /var/lib/docker /data/docker/lib/
3.在docker守护进程文件中指定新的docker运行目录
vim /lib/systemd/system/docker.service
# 在ExecStart加入
--graph=/data/docker/lib/docker

4.重启docker服务

systemctl daemon-reload && systemctl start docker

5.检查docker服务状态

systemctl status docker