Linux下如何免sudo执行docker命令

本文介绍如何通过将用户添加到docker组中,实现在Linux系统上无需使用sudo即可执行Docker命令的方法。具体步骤包括创建docker组、将当前用户添加到该组,并重启会话以使更改生效。

根据docker的官方文档的描述

https://docs.docker.com/install/linux/linux-postinstall/

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 rootuser.

If you don’t want to preface the docker command with sudo, create a Unix group called docker and add users to it. When the Docker daemon starts, it creates a Unix socket accessible by members of the dockergroup.

所以,只要把当前用户添加到docker用户组里面就可以了。

 

具体的操作步骤如下:

  1. 如果没有,则创建 docker 用户组。

    $ sudo groupadd docker
    
  2. 把当前用户加入docker用户组。

    $ sudo usermod -aG docker $USER
    
  3. 退出当前会话然后重新登录,使用户加组操作生效

  4. 验证是否可以在免sudo的情况下执行docker命令。

    $ docker run hello-world
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值