Docker安全策略--非root用户运行

该学习笔记仅为有需要的学友学习使用,参考了

https://www.linuxidc.com/Linux/2016-11/137549.htm 

https://blog.csdn.net/kongxx/article/details/52413332

再次表示感谢!

拙见:

1、docker 自身已具备一定的安全测试,可以说是比较安全的,但由于docker本身共享了宿主机的资源,这会产生docker 进程权限过大的(特权)。

2、目前docker 容器内部基本都是root 权限运行的,当我们需要-v映射资源到宿主机时又存在另一个安全问题,这需要我们注意-v 的宿主机目录不能是docker的主进程目录且最好按需指定目录权限,少用特权属性(--privileged=true)

3、docker 已经支持非root 权限运行了,所以我们可以利用linux 的安全策略给 docker再加一层保护

4、容器对外暴露的资源默认仅有指定的端口,所以我们竟可能的减少端口映射。

非root运行:

1、创建新用户

创建一个用户名为:pengfeima

[root@localhost ~]# adduser pengfeima

为这个用户初始化密码,linux会判断密码复杂度,不过可以强行忽略:

[root@localhost ~]# passwd pengfeima
更改用户 pengfeima 的密码 。

2、授权

个人用户的权限只可以在本home下有完整权限,其他目录要看别人授权。而经常需要root用户的权限,这时候sudo可以化身为root来操作。我记得我曾经sudo创建了文件,然后发现自己并没有读写权限,因为查看权限是root创建的。

新创建的用户并不能使用sudo命令,需要给他添加授权。

sudo命令的授权管理是在sudoers文件里的。可以看看sudoers:

[root@localhost ~]# sudoers
bash: sudoers: 未找到命令...
[root@localhost ~]# whereis sudoers
sudoers: /etc/sudoers /etc/sudoers.d /usr/libexec/sudoers.so /usr/share/man/man5/sudoers.5.gz

找到这个文件位置之后再查看权限:

[root@localhost ~]# ls -l /etc/sudoers
-r--r----- 1 root root 4251 9月  25 15:08 /etc/sudoers

是的,只有只读的权限,如果想要修改的话,需要先添加w权限:

[root@localhost ~]# chmod -v u+w /etc/sudoers
mode of "/etc/sudoers" changed from 0440 (r--r-----) to 0640 (rw-r-----)

然后就可以添加内容了,在下面的一行下追加新增的用户:

[root@localhost ~]# vim /etc/sudoers


## Allow root to run any commands anywher  
root    ALL=(ALL)       ALL  
pengfeima  ALL=(ALL)       ALL  #这个是新增的用户

wq保存退出,这时候要记得将写权限收回:

[root@localhost ~]# chmod -v u-w /etc/sudoers
mode of "/etc/sudoers" changed from 0640 (rw-r-----) to 0440 (r--r-----)

3、创建docker组

sudo groupadd docker

将当前用户加入docker组

sudo gpasswd -a ${USER} docker

重新启动docker服务(下面是CentOS7的命令)

sudo systemctl restart docker

4、切换账号

使用 su pengfeima 切换到pengfeima

[root@k8smaster /]# su pengfeima
[pengfeima@k8smaster /]$ 

5、操作容器 

使用sudo docker images 来查看镜像

[pengfeima@k8smaster /]$ sudo docker images
[sudo] password for pengfeima: 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[pengfeima@k8smaster /]$ 

所有docker的操作都需要使用sudo进行

猜你喜欢

转载自blog.csdn.net/hnmpf/article/details/82866770
今日推荐