ssh -tオプションがリダイレクト出力にcrとlfを追加するのはなぜですか?

ssh -tオプションがリダイレクト出力にcrとlfを追加するのはなぜですか?

私はWindowsboxからLinux Boxに接続するためにパテを使用します。その後、私は次のことを行います。

サーバーAから:

serverA: file /etc/motd
/etc/motd: : ASCII English text

サーバーBで:

serverB: ssh -t user@serverA "cat /etc/motd" > /etc/motd.serverA
serverB: file /etc/motd.serverA
/etc/motd.serverA:  ASCII text, with CRLF line terminators

今、リダイレクトされた出力にCRとLFがあるのはなぜですか?これは ssh に -t オプションを使用する場合にのみ発生します。 SSHログインでコマンドを実行するためにsudoを使用する必要がある場合は、-tが必要です。たとえば、

serverB: ssh -t user@serverA "sudo cat /etc/shadow" > /etc/shadow

ご提案いただきありがとうございます。

ベストアンサー1

TTY設定を確認してください。

$ ssh -t somewhere 'stty -a' | grep cr
iflags: -istrip icrnl -inlcr -igncr -iuclc ixon -ixoff ixany imaxbel
oflags: opost onlcr -ocrnl -onocr -onlret -olcuc oxtabs -onoeot
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -mdmbuf

ssh -tこれはさまざまですが、これはデフォルトで「igncrCR無視」が入力およびonlcr設定された出力(NLをCR-NLにマッピング)に対して無効になっており、CRが削除または省略されていないことを示しています。これらの用語はマニュアルで参照または表示stty(1)できますtermios(4)(Linuxは他のマニュアルセクションにそれらの用語を置くこともできます)。

これらの設定は変更される可能性があります(ただし、何らかのonlcr理由で設定する必要がある場合は中断される可能性があります)。

$ ssh -t somehost 'stty onlcr; cat /etc/motd' > x ; file x
x: ASCII English text, with CRLF line terminators
$ ssh -t somehost 'stty -onlcr; cat /etc/motd' > x ; file x
x: ASCII English text
$ 

(医師)tty CR / NL処理によってファイルの内容が変更される危険性を排除するには、データを使用またはscpコピーする方が賢明です。sftp

おすすめ記事