mfz.github.io

Logging into singularity instance using ssh

Posted at — Jun 15, 2022

Sometimes we want to connect into a singularity instance using ssh. For example, when setting up a pseudo-distributed environment, Hadoop tries to ssh into the pseudo nodes to start up the services.

The way to achieve this is to install a sshd within the container, and specify a different port than 22 (as this is used by the host).

cat > openssh.def << EOF
Bootstrap: library
From: ubuntu:20.04

%help
      An example of running openssh-server within a singularity container

      singularity instance start openssh.sif openssh

      ssh localhost -p 12121

%post
      apt-get update && apt-get install -y openssh-server

      # make hostkey accessible to sshd invocation by user
      chmod 755 /etc/ssh
      chmod 655 /etc/ssh/*

%startscript
      /usr/sbin/sshd -p 12121 -o UsePAM=no -h /etc/ssh/ssh_host_rsa_key
EOF

Build using

sudo singularity build openssh.sif openssh.def

Try using

singularity instance start openssh.sif myopenssh

ssh localhost -p 12121  # this connects to singularity instance

singularity instance stop myopenssh