在 Linux 系统中,可以通过 stty
命令修改终端的 ECHO
模式。ECHO
模式决定了终端是否回显用户的输入。以下是如何使用 stty
命令来修改 ECHO
模式的方法:
禁用 ECHO 模式
要禁用 ECHO
模式,可以使用以下命令:
tty -echo
这条命令将关闭回显功能,使得用户在终端中输入的字符不会显示在屏幕上。这在输入密码时非常有用,以防止旁观者看到输入的密码。
启用 ECHO 模式
要启用 ECHO
模式,可以使用以下命令:
tty echo
这条命令将打开回显功能,使得用户在终端中输入的字符能够显示在屏幕上。
临时修改 ECHO 模式
stty
命令的修改是临时的,只会影响当前终端会话。一旦关闭终端或注销,修改将不再有效。
永久修改 ECHO 模式
如果你想要永久修改 ECHO 模式,可以将其设置添加到用户的 shell 配置文件中,如 .bashrc
或 .profile
。例如,要永久禁用 ECHO 模式,可以添加以下行到 .bashrc
文件:
tty -echo
然后,运行以下命令使更改生效:
ource ~/.bashrc
查看当前 ECHO 模式状态
要查看当前的 ECHO 模式状态,可以使用 stty
命令的 -a
(all)选项:
tty -a
输出将显示所有终端属性的当前设置。在输出中,如果看到 echo
选项,则表示 ECHO 模式被启用;如果看到 -echo
选项,则表示 ECHO 模式被禁用。
注意事项
- 修改 ECHO 模式可能会影响用户的输入体验,特别是在需要输入密码或敏感信息时。
- 在某些情况下,如使用 SSH 连接到远程服务器时,远程服务器的配置可能会影响 ECHO 模式的行为。
- 使用
stty
命令时,确保你有足够的权限来修改终端设置。
通过使用 stty
命令,你可以灵活地控制 Linux 终端的 ECHO 模式,以满足不同的使用场景和安全需求。
实操示例
#### 查看stty配置项目
[test@*** ~]$ stty -a
speed 38400 baud; rows 40; columns 160; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc
#### iexten echo echoe 中的echo 就标识已开启
#### 禁用echo
[test@*** ~]$ stty -echo
#### 输入ls,看看就没有回显了
[test@*** ~]$ 22.txt 222.txt
#### stty -a 在查看配置
[test@*** ~]$ speed 38400 baud; rows 40; columns 160; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc
#### iexten -echo echoe 这会echo就变成了-echo 了,标识echo模式禁用
#### 输入stty echo 开启echo模式
[test@*** ~]$ stty: when specifying an output style, modes may not be set
[test@*** ~]$ [test@*** ~]$
[test@*** ~]$
#### 这时就有回显了
[test@*** ~]$ ls
22.txt 222.txt
#### 查看配置
[test@*** ~]$ stty -a
speed 38400 baud; rows 40; columns 160; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc
#### 配置由-echo变成echo了
[test@*** ~]$