如果在 docker 容器中,安装了 openssh-server
,启动的时候会提示没有权限,是因为需要 docker 使用 --privileged
来启动容器,如果没有 privileged
权限,可以尝试使用 service 来启动
# docker run --rm -it centos:7.2.1511 bash
yum install openssh-server -y -q
yum install initscripts -y -q
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
systemctl disable sshd
cat > /etc/init.d/sshd << "EOF"
#!/bin/bash
#
# sshd: OpenSSH Server daemon
#
# description: The sshd daemon is the server process for ssh
#
# Source function library.
. /etc/init.d/functions
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
# Make sshd pid file directory if not exists
mkdir -p /var/run/sshd
# OpenSSH daemon options
OPTIONS="-f /etc/ssh/sshd_config"
# Read environment variables file
if [ -f /etc/sysconfig/sshd ] ; then
. /etc/sysconfig/sshd
fi
# Start or stop daemon
case "$1" in
start)
echo -n $"Starting sshd daemon: "
daemon /usr/sbin/sshd $OPTIONS
echo
;;
stop)
echo -n $"Stopping sshd daemon: "
killproc /usr/sbin/sshd
echo
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 2
esac
exit $?
EOF
chmod +x /etc/init.d/sshd
service sshd start