centos ssh 使用systemctl报错:Failed to get D-Bus connection: Operation not permitted

使用centos官网docker镜像,版本为CentOS Linux release 7.4.1708 (Core)

使用systemctl报错:Failed to get D-Bus connection: Operation not permitted

centos docker镜像官网中已经说明,在centos7.2会解决这个问题,但是需要自己去开启.

解决方案.

编写Dockerfile,构造自己的镜像版本.

FROM registry.docker-cn.com/library/centos
ENV container docker
MAINTAINER liao <1149790323@qq.com>
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
RUN yum -y install net-tools
RUN yum -y install iproute
RUN yum -y install vim-enhanced
RUN yum -y install sudo passwd
RUN yum -y install iptables
RUN yum -y install openssl openssh-server
RUN ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
RUN ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key  -N ''
RUN sed -i "s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g" /etc/ssh/sshd_config
RUN sed -i "s/UsePAM.*/UsePAM no/g" /etc/ssh/sshd_config
RUN sed -i "s/#Port.*/Port 20022/g" /etc/ssh/sshd_config
RUN echo "root:123" | chpasswd
EXPOSE 20022
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]

2.运行docker镜像

docker run --privileged -dti -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 20022:20022 local/centos_ssh

说明:
1.运行后不能用docker attach containerid去连接,只能用ssh连接
2.更改22端口为20022,防止冲突.

    原文作者:Sophie12138
    原文地址: https://www.jianshu.com/p/4d921a7fceca
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞