0

I am trying to set up a GIT server in my LAN & use it with my Android phone through Termux.

My PC runs Fedora Silverblue 40.

Some tracelogs:

  • On my PC (all as user git):
$> ip addr show wlp5s0 | grep "inet " | awk '{print $2}' | cut -d '/' -f1
192.168.245.11
$> systemctl list-units | grep sshd.service
    sshd.service    loaded active running   OpenSSH server daemon
$> cat ~/.ssh/authorized_keys
ssh-ed25519 AAAAC3N[...]KBoG user@lan
$> ls -alps ~/.ssh/
total 4
0 drwx------. 1 git git  30 07-04 12:32 ./
0 drwx------. 1 git git 172 07-04 14:41 ../
4 -rw-------. 1 git git  92 07-04 13:07 authorized_keys
$> ls -alps ~/myrepo/user/
total 0
0 drwxr-xr-x. 1 git git 10 07-04 14:05 ./
0 drwxr-xr-x. 1 git git 12 07-04 14:05 ../
0 drwxr-xr-x. 1 git git 16 07-04 14:40 Notes/
$> ls -alps ~/myrepo/user/Notes/
total 4
0 drwxr-xr-x. 1 git git  16 07-04 14:40 ./
0 drwxr-xr-x. 1 git git  10 07-04 14:05 ../
0 drwxr-xr-x. 1 git git 144 07-05 16:34 .git/
4 -rw-r--r--. 1 git git   2 07-04 14:40 init
$> cd ~/myrepo/tsilvs/Notes/ && git status && cd ~
On branch main
nothing to commit, working tree clean
$> tail -n 4 ~/.bashrc
# Modified part
## Shell Session Start Message
CUR_TTY="$(tty)"
[ "$CUR_TTY" != "$SSH_TTY" ] && echo -e "Hello, $(whoami)!\n"
  • On my Android device in Termux:
~ $ cat ~/.bashrc
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/user.lan.001
~ $ eval "$(ssh-agent -s)"
Agent pid 6236
~ $ ssh-add .ssh/user.lan.001
Identity added: .ssh/user.lan.001 (user@lan)
~ $ cat .ssh/user.lan.001.pub
ssh-ed25519 AAAAC3N[...]KBoG user@lan
~ $ ssh-keygen -y -f .ssh/user.lan.001
ssh-ed25519 AAAAC3N[...]KBoG user@lan
~ $ ls -l .ssh/user.lan.001
-rw------- 1 u0_a196 u0_a196 399 Jul  4 14:03 .ssh/user.lan.001
~ $ ssh [email protected] git-receive-pack /var/home/git/myrepo/user/Notes
00b1e1bbbef5f1220e0dc19be67fc2b6e66c5c5cb4dc refs/heads/mainreport-status report-status-v2 delete-refs side-band-64k quiet atomic ofs-delta object-format=sha1 agent=git/2.44.0
0000^C~ $                                              
~ $ git clone [email protected]:/var/home/git/myrepo/user/Notes
Cloning into 'Notes'...
fatal: protocol error: bad line length character: ?[47
fatal: the remote end hung up unexpectedly
~ $ ssh [email protected]
Last login: Sat Jul  6 12:47:58 2024
git@fed-001:~$>
~ $ ssh [email protected] true
Hello, git!

Everything seems to be configured properly. For example, SSH sessions work perfectly fine.

Why does the git clone terminate with an error?

2 Answers 2

1

The usual reason something like this happens (I've seen this with git, but also with emacs' TRAMP remote access protocol) is that the shell of the authenticating user sends some unexpected bytes, before it invokes the program that the local git asks it to execute.

What you're getting seems to be a terminal escape sequence. (from the top of my head, this would seem like your shell is telling the receiving terminal emulator to set the background-color of the coming characters to white)

Solution is usually

  1. make sure your git server isn't sending some kind of welcome banner/MOTD (set PrintLastLog=no and PrintMotd=no in your sshd_config)
  2. making sure that you don't have a fancy ~/.bashrc or ~/.zshrc (depending on what your shell is) for that user (especially if it's a special-purpose git user, that'd be a mistake; you shouldn't "work" as that user locally, anyways, but sudo -u user <command> things as that user if anything), and if that doesn't help
  3. make sure the shell for that user is as plain as it can be – setting it to /bin/sh using chsh might not be a bad idea.
2
  • sshd doesn't print anything, but I am printing a hello message from ~/.bashrc. I tried to detect if a TTY is SSH, but git clone ignores this check. Maybe it's possible to detect if a TTY is git, or if a connection was initiated by git command? Commented Jul 6, 2024 at 16:37
  • the solution, as said, is simple: don't print anything in the bashrc of your git user. That makes no sense at all – you never run an interactive shell as the git user, so having a bashrc has no benefit, to begin with. Commented Jul 6, 2024 at 17:55
-1

As pointed out in @marcus-müller's answer, apps like git can't handle MOTDs & other sourced messages. Therefore:

  1. Remove any messages from remote shell output
    • e.g. MOTDs configured in bashrc or profile (both in /home/user & /etc)
  2. Configure /etc/ssh/sshd_config (restart your sshd after that):
    • PrintMotd no - removes MOTD message
    • PrintLastLog no - removes Last logged in message

IF you still need to print any session start messages, there are two ways to achieve that:

  1. git UNIX user "denylist"
    1. Make sure you've configured a git UNIX user on your git server for all git operations.
    2. In system's /etc/bashrc or /etc/profile (or git's ~/.bashrc or ~/profile) change the line that prints a message to include this condition: [ "$(tty)" != "$SSH_TTY" ] && [ "$(whoami)" != "git" ] && echo "Your message here!"
    3. Call git clone git@[your remote address]:/path/to/repo.git (where repo.git is a "bare repo"), pay attention to user (git@) part.
      • You can put as much public ssh keys in /home/git/.ssh/authorized_keys as you need for your tasks (e.g. multi-user access), but take care of your security.
  2. UNIX user "allowlist"
    1. change the line that prints a message to include this condition: [ "$(tty)" != "$SSH_TTY" ] && [ "$(whoami)" == "username" ] && echo "Your message here!", where username is the name of a user that needs to see a message.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.