docker image ls 提示 “Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock

现象描述

上次在统信Linux上费了很大力气才装上docker,当时记得是可以用了。但今天登录系统后执行 docker image ls却提示上面的问题,但切换到root账户再执行这个命令就不报错了。

root可以正常使用,普通用户不行,大概率是权限的问题,可以给这个用户加docker用户组权限。

尝试1

使用下面的命令,给用户增加权限

usermod -aG docker wang

不过执行之后,再次尝试还是提示同样的错误。不太确定是否需要更新权限(或重启)导致的。

尝试2

找到了一个另外的办法。

用了另外一个命令增加权限,同时更新了一下权限,随后执行不再报错,问题解决。

wang@wang-PC:~$ ls -l /var/run/docker.sock 
srw-rw---- 1 root docker 0 8月  23 08:38 /var/run/docker.sock

wang@wang-PC:~$ su
请输入密码
Password:
验证成功

root@wang-PC:/home/wang# gpasswd -a wang docker
正在将用户“wang”加入到“docker”组中
root@wang-PC:/home/wang# newgrp docker
root@wang-PC:/home/wang# exit
exit

root@wang-PC:/home/wang# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
postgres            12.6                9691f14013a7        15 months ago       300MB

gpasswd和usermod的区别

usermod是修改用户的属性,gpasswd的话主要是对用户组的操作。

但就上面两个命令来讲,没什么区别,都是把wang加入到docker组中。因此,第一个没生效应该是没有更新的原因(随后尝试删除了wang的docker组,重启,再执行报错;usermod添加权限,重启,执行成功,因此验证了尝试1中的猜测)。

可以参考使用说明

[wang@DESKTOP-K8UBC7R system32]$ usermod
Usage: usermod [options] LOGIN

Options:
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS
  -a, --append                  append the user to the supplemental GROUPS
                                mentioned by the -G option without removing
                                the user from other groups
  -h, --help                    display this help message and exit
  -l, --login NEW_LOGIN         new value of the login name
  -L, --lock                    lock the user account
  -m, --move-home               move contents of the home directory to the
                                new location (use only with -d)
  -o, --non-unique              allow using duplicate (non-unique) UID
  -p, --password PASSWORD       use encrypted password for the new password
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -s, --shell SHELL             new login shell for the user account
  -u, --uid UID                 new UID for the user account
  -U, --unlock                  unlock the user account
  -v, --add-subuids FIRST-LAST  add range of subordinate uids
  -V, --del-subuids FIRST-LAST  remove range of subordinate uids
  -w, --add-subgids FIRST-LAST  add range of subordinate gids
  -W, --del-subgids FIRST-LAST  remove range of subordinate gids
  -Z, --selinux-user SEUSER     new SELinux user mapping for the user account

[wang@DESKTOP-K8UBC7R system32]$ gpasswd
Usage: gpasswd [option] GROUP

Options:
  -a, --add USER                add USER to GROUP
  -d, --delete USER             remove USER from GROUP
  -h, --help                    display this help message and exit
  -Q, --root CHROOT_DIR         directory to chroot into
  -r, --delete-password         remove the GROUP's password
  -R, --restrict                restrict access to GROUP to its members
  -M, --members USER,...        set the list of members of GROUP
  -A, --administrators ADMIN,...
                                set the list of administrators for GROUP
Except for the -A and -M options, the options cannot be combined.

也可以看到,两个命令的侧重点是不一样的。

但gpasswd -a wang docker 和 usermod -aG docker wang的功能是一样的。

Linux 用户组的概念

在一些文章里看到了用户组又分为主组和附加组的概念,一时间有点晕乎。查了一下资料,发现两者其实对于使用来讲,区别不大。

对于一个用户来讲,有且只能有一个主组、主组可以更换;可以属于多个附加组。

详细如下:

主组也叫初始组,是用户登录系统时的组,规则如下:

  • 创建新用户时,若未明确指定该用户所属的主组,会默认创建一个与用户名相同的组,作为该用户的主组
  • 用户创建文件时,文件的所属权限组就是当前用户的主组
  • 使用useradd命令时用g参数可以指定主组,则不会默认创建同名的主组
  • 用户有且只能所属一个主组
  • 用户的主组不能被删除
  • 用户不能直接被移出主组,但可以更换主组
  • 用户被删除时它的主组若没有其他所属用户,则会自动删除该主组

附加组:

登录后可切换的其他组,规则如下:

  • 使用useradd命令时用G参数可以指定附加组
  • 用户可以所属零个或多个附加组
  • 用户的附加组和主组可相同
  • 附加组可以直接被删除而无需关心是否所属于用户
  • 附加组可以新增和移除任意个所属用户
  • 用户被删除时所属附属组不会受影响

建议:使用gpasswd命令而不是usermod,因为usermod -G命令如果不写全用户的附属组,会清空之前的所有附属组

参考

https://juejin.cn/post/7051635397419499556

posted @ 2022-09-08 14:41  wswang  阅读(645)  评论(0编辑  收藏  举报