Skip to content

Instantly share code, notes, and snippets.

@chazer
Last active September 29, 2020 10:37
Show Gist options
  • Save chazer/867a20dceb4413e09dd1ba2d025197e3 to your computer and use it in GitHub Desktop.
Save chazer/867a20dceb4413e09dd1ba2d025197e3 to your computer and use it in GitHub Desktop.
SSH cheatsheet

SSH cheats

Copy folder to remote server

REMOTE=server.fqdn
sudo tar -cz -C `pwd` . \
    | ssh "$REMOTE" "sudo mkdir -p $(pwd) && sudo tar --same-owner --same-permissions -xzv -C $(pwd)"

Allow sudo without password on remote server

REMOTE=server.fqdn
USER=username
ssh -tt "$REMOTE" "sudo sh -c 'echo \"${USER} ALL=(ALL) NOPASSWD:ALL\" > /etc/sudoers.d/${USER/./_}'"

Disallow sudo without password on remote server

REMOTE=server.fqdn
ssh -tt "$REMOTE" "sudo sh -c 'echo \"\" > /etc/sudoers.d/${USER/./_}'"

Migrate docker

Copy all docker images to remote server

SSH_TARGET=user@server.fqdn
for image in `docker images --format '{{ .Repository }}:{{ .Tag }}'`; do
  docker save $image | ssh "$SSH_TARGET" docker load
done

Copy docker volume to remote server

SSH_TARGET=user@host
VOLUME_NAME=volume
ssh "$SSH_TARGET" 'docker volume create '"$VOLUME_NAME"'';
docker run --rm -v $VOLUME_NAME:/from alpine ash -c "cd /from ; tar -cf - . " \
  | ssh "$SSH_TARGET" 'docker run --rm -i -v '"$VOLUME_NAME"':/to alpine ash -c "cd /to ; tar -xpvf - " ';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment