用户创建的 MOTD 阻止 SCP 运行

Saf*_*ado 2 scp public-key

我通过执行以下操作创建了要在通过 SSH 登录时显示的自定义 MOTD:

  1. 创建一个文本文件,/etc/usermotd/<username>其中包含消息
  2. 编辑用户的 .bashrc 文件并输入以下代码:
if [ -f /etc/usermotd/`whoami` ]; then
cat /etc/usermotd/<username>;
fi
Run Code Online (Sandbox Code Playgroud)

它像我想要的那样工作。然而...

我现在刚刚发现当我尝试对某些东西进行 SCP 时,它不起作用。我执行了 scp 命令,它退出时没有给出任何进度条或说它已转移。当我执行 scp -vvv 时,我得到

debug1: Authentication succeeded (publickey).
debug2: fd 4 setting O_NONBLOCK
debug2: fd 5 setting O_NONBLOCK
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug2: callback start
debug2: client_session2_setup: id 0
debug1: Sending command: scp -v -t ~
debug2: channel 0: request exec confirm 1
debug2: fd 3 setting TCP_NODELAY
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: exec request accepted on channel 0

WELCOME
debug2: channel 0: read<=0 rfd 4 len 0
debug2: channel 0: read failed
debug2: channel 0: close_read
debug2: channel 0: input open -> drain
debug2: channel 0: ibuf empty
debug2: channel 0: send eof
debug2: channel 0: input drain -> closed
BigBoss ~/.ssh:-$ debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug2: channel 0: rcvd eof
debug2: channel 0: output open -> drain
debug2: channel 0: obuf empty
debug2: channel 0: close_write
debug2: channel 0: output drain -> closed
debug2: channel 0: rcvd close
debug3: channel 0: will not send data after close
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: gc: user detached
debug2: channel 0: send close
debug2: channel 0: is dead
debug2: channel 0: garbage collecting
debug1: channel 0: free: client-session, nchannels 1
debug3: channel 0: status: The following connections are open:
  #0 client-session (t4 r0 i3/0 o3/0 fd -1/-1 cfd -1)

debug3: channel 0: close_fds r -1 w -1 e 6 c -1 
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK 
Transferred: sent 2208, received 3352 bytes, in 0.2 seconds
Bytes per second: sent 13207.5, received 20050.5
debug1: Exit status 0
Run Code Online (Sandbox Code Playgroud)

任何想法为什么它无法发送?或者我还有其他方法可以做到这一点吗?

Der*_*rfK 5

它无法工作,因为 scp 期望远程端的行为完全“如此”,而不是大喊欢迎回来。(这是很好,如果它会给出错误信息,但是......)。

使用sftp(根本不会产生登录外壳)代替scp或让您.bashrc检测它是否在交互式会话中。 似乎有几种方法可以做到这一点:

if [ -n "$PS1" ]; then
  echo WELCOME
fi;
Run Code Online (Sandbox Code Playgroud)

那里给出的用于查看“$-”是否包含“i”的另一种方法似乎已损坏,因为在字符串比较中[不起作用*。这应该可以工作(它会检查以确保 $- 已设置,然后检查删除“i”的 $- 是否与 $- 相同。可能有更好的方法,但我想不出第二个)

if [ -n "$-" -a "${-/i}" != "$-" ]; then
  echo WELCOME
fi; 
Run Code Online (Sandbox Code Playgroud)