Skip to content

scp is broken on Debian after script applies #14

@artschekoff

Description

@artschekoff

Hi everyone!

After the script was applied, with all the Enter ... Enter steps and a reboot, I can no longer use scp.

The error is:

┬─[riskyworks@Awesome-MacBook:~/.s/docker]─[19:30:47]─[I]─[G:master =]
╰─>$ scp install-docker-debian.sh auroradev:/home/techcode/
subsystem request failed on channel 0
scp: Connection closed

After investigating, I found that the problem is in the SFTP subsystem path. This fix worked for me:

#!/usr/bin/env bash
# Fix SFTP subsystem path after fortress_improved.sh (or similar) rewrote
# /etc/ssh/sshd_config. Intended for Debian/Ubuntu; falls back if layout differs.
set -euo pipefail

SSH_CONFIG="/etc/ssh/sshd_config"

resolve_sftp_server() {
  if [[ -x /usr/lib/openssh/sftp-server ]]; then
    printf '%s\n' "/usr/lib/openssh/sftp-server"
    return 0
  fi
  local cand
  cand="$(command -v sftp-server 2>/dev/null || true)"
  if [[ -n "$cand" && -x "$cand" ]]; then
    printf '%s\n' "$cand"
    return 0
  fi
  if command -v dpkg &>/dev/null; then
    cand="$(dpkg -L openssh-server 2>/dev/null | grep -E '/sftp-server$' | head -1)"
    if [[ -n "$cand" && -x "$cand" ]]; then
      printf '%s\n' "$cand"
      return 0
    fi
  fi
  return 1
}

if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
  echo "Run as root: sudo $0" >&2
  exit 1
fi

SFTP_PATH="$(resolve_sftp_server || true)"
if [[ -z "${SFTP_PATH:-}" ]]; then
  echo "Cannot find executable sftp-server (install: apt-get install -y openssh-server)" >&2
  exit 1
fi

if [[ ! -f "$SSH_CONFIG" ]]; then
  echo "Missing ${SSH_CONFIG}" >&2
  exit 1
fi

cp -a "$SSH_CONFIG" "${SSH_CONFIG}.bak.fortress-debian-fix.$(date +%Y%m%d%H%M%S)"

if grep -qE '^[[:space:]]*Subsystem[[:space:]]+sftp[[:space:]]' "$SSH_CONFIG"; then
  sed -i "s|^[[:space:]]*Subsystem[[:space:]]\+sftp[[:space:]].*|Subsystem sftp ${SFTP_PATH}|" "$SSH_CONFIG"
else
  printf '\n# fortress_improved.debian_fix: SFTP subsystem\nSubsystem sftp %s\n' "$SFTP_PATH" >> "$SSH_CONFIG"
fi

sshd -t
systemctl restart ssh 2>/dev/null || systemctl restart sshd

echo "OK: Subsystem sftp -> ${SFTP_PATH}"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions