问题原文
原帖链接 · Joel G Mathew · CC BY-SA 4.0
My problem is that I need to set a few variables, and output a few lines every time I login to the ssh shell, and at the same time I have to be able to use sftp to tarnsfer files via Filezilla.
Now, as per the openssh FAQ at http://www.openssh.org/faq.html, if your startup scripts echo any kind of output, it messes up with sftp. So it either delays indefinitely, or errors out with a "Connection closed by server with exit code 128".
I have tried solutions like moving .bashrc to .bash_profile, or using the following code in .bashrc:
if [ "$TERM" != "dumb" ]
then
source .bashc_real
fi
And:
if [ "$TERM" = "xterm" ]
then
source .bashc_real
fi
However, nothing works. My shell terminal is bash, and I connect to sftp with filezilla.
社区高分解答
原帖链接 · Insyte · CC BY-SA 3.0
Mike's answer will probably work. But it's worth pointing out that you can accomplish this carefully selecting which startup files to put the verbose stuff in. From the bash man page:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
The sftp/scp tools start an interactive non-login shell, so .bashrc will be sourced. Many distributions source .bashrc from .bash_profile or vice versa, so it can get confusing. A good trick for testing the cleanliness of your login environment is to ssh in with a command, which simulates the same way scp/sftp connect. For example: ssh myhost /bin/true will show you exactly what scp/sftp sees when they connect.
A simple demo:
insyte@mazer:~$ echo "echo Hello from .profile" > .profile
insyte@mazer:~$ echo "echo Hello from .bashrc" > .bashrc
sazerac:~ insyte$ ssh mazer /bin/true
Hello from .bashrc
sazerac:~ insyte$
insyte@mazer:~$ rm .bashrc
sazerac:~ insyte$ ssh mazer /bin/true
sazerac:~ insyte$
The first test will cause scp/sftp/rsync etc. to break. The second version will work just fine.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
