AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 16336
Accepted
Dima
Dima
Asked: 2010-12-06 12:14:21 +0800 CST2010-12-06 12:14:21 +0800 CST 2010-12-06 12:14:21 +0800 CST

如何通过 ssh 获得彩色终端?

  • 772

当我通过 ssh 连接到远程主机时,一切都只是一种字体/颜色。我想要像我在本地做的颜色,例如绿色代表可执行文件,蓝色代表符号链接等。这样,当我在 ssh 主机上运行 $ git diff 时,它会显示带有颜色的差异 =)

command-line ssh
  • 14 14 个回答
  • 163507 Views

14 个回答

  • Voted
  1. Best Answer
    Dima
    2010-12-06T16:41:37+08:002010-12-06T16:41:37+08:00

    因为它是服务器上的xterm,我认为有问题.bashrc

    确实如此!ls --color=auto仅当您连接到 TTY 时才有效。将所有内容更改为简单地--color在.bashrc远程主机上,现在所有内容都呈现漂亮的颜色。

    • 40
  2. sites
    2016-10-20T16:57:43+08:002016-10-20T16:57:43+08:00

    这对我有用:

    ssh -t my_host my_command
    

    -t是关键。解释:

    -t      Force pseudo-tty allocation.  This can be used to execute arbitrary screen-based programs on a
            remote machine, which can be very useful, e.g. when implementing menu services.  Multiple -t
            options force tty allocation, even if ssh has no local tty.
    
    • 39
  3. dariusz gorczynski
    2015-04-12T10:12:12+08:002015-04-12T10:12:12+08:00

    似乎已经~/.bashrc为我设置了颜色,问题是 ssh 不使用 bashrc 文件。您可以在 ssh 会话中使用 bashrc,方法是将以下内容添加到~/.bash_profile:

    if [ -f ~/.bashrc ]; then
          . ~/.bashrc
    fi
    
    • 27
  4. hinneLinks
    2019-04-15T00:14:47+08:002019-04-15T00:14:47+08:00

    由于直接登录时颜色工作正常,我只是取消注释force_color_prompt=yes文件中的行~/.bashrc,这也给了我在 ssh 上的颜色:

    # uncomment for a colored prompt, if the terminal has the capability; turned
    # off by default to not distract the user: the focus in a terminal window
    # should be on the output of commands, not on the prompt
    force_color_prompt=yes
    

    (Ubuntu 18.04 LTS)

    • 19
  5. Vedran Krivokuća
    2010-12-06T13:09:21+08:002010-12-06T13:09:21+08:00

    当您连接到服务器时,您的 XTERM 环境变量的内容是什么?

    ~ > export | grep -i term
    TERM=xterm
    
    • 8
  6. Stuart Clark
    2017-05-31T23:35:28+08:002017-05-31T23:35:28+08:00

    就我而言,缺少的部分是有彩色的 ls、grep 等,可以通过向 .bashrc 文件添加别名来添加:

    alias ls='ls --color=auto'
    alias grep='grep --color=auto'
    

    等等

    • 5
  7. chris544
    2016-03-08T13:57:40+08:002016-03-08T13:57:40+08:00

    我尝试更改~./bashrc设置(在本地和远程服务器上),但它似乎不起作用。

    然后我注意到~/.bashrc如果我通过 ssh 连接到远程服务器,它甚至不会被执行。所以我通过将远程服务器放入~/.bashrc远程服务器来执行远程服务器。(基于https://stackoverflow.com/questions/820517/bashrc-at-ssh-login)。if [ -f ~/.bashrc ]; then . ~/.bashrc fi~/.bash_profile

    所以这个解决方案不需要~/bashrc直接更改任何文件,但它确实需要更改远程服务器的文件,~/bash_profile以便~/bashrc执行远程服务器的文件。

    • 3
  8. Kevin
    2018-10-17T10:53:18+08:002018-10-17T10:53:18+08:00

    在我的情况下,我最近安装了chef-local它,它要求我在.bash_profile. 当我登录时,.bashrc不再加载,因为它看到了.bash_profile.

    我所做的是在中添加一行.bash_profile:

    source .bashrc
    export PATH="/opt/chefdk/embedded/bin:$PATH"
    

    我注销并重新登录并立即获得我的彩色终端。

    • 3
  9. Mike Wise
    2018-03-16T13:02:58+08:002018-03-16T13:02:58+08:00

    上面的“Mike E”有一条评论包含了我的答案,但它不仅难以阅读,而且如果你不经常使用的话,很难弄清楚他的意思.bashrc——而我没有吨。

    经过一番折腾,我通过在~/.bashrc使用 ssh 登录的机器上更改以下行来获得所需的结果:

    # set a fancy prompt (non-color, unless we know we "want" color)
    case "$TERM" in
        xterm-color|*-256color) color_prompt=yes;;
    esac
    

    至:

    # set a fancy prompt (non-color, unless we know we "want" color)
    case "$TERM" in
        xterm-color|*-256color) color_prompt=yes;;
        xterm) color_prompt=yes;;
    esac
    

    我想我可以在第一行的“color”之后添加“|xterm”,或者挖掘并找出为什么 ssh 使用“xterm”而不是“xterm-color”并改变它,但这有效,我现在还有其他事情要做。

    • 2
  10. ErichBSchulz
    2018-08-28T19:54:43+08:002018-08-28T19:54:43+08:00

    通过代理连接时我失去了颜色,因为TERM=dumb我修复了它:

    ssh myproxy "ssh pi@localhost -p 5000 -tt 'TERM=xterm bash'"
    
    • 1

相关问题

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

  • 如何与无头服务器进行图形交互?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何安装 .run 文件?

    • 7 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    如何获得 CPU 温度?

    • 21 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Marko Smith

    你如何重新启动Apache?

    • 13 个回答
  • Marko Smith

    如何卸载软件?

    • 11 个回答
  • Marko Smith

    如何删除 PPA?

    • 26 个回答
  • Martin Hope
    NES 如何启用或禁用服务? 2010-12-30 13:03:32 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    Olivier Lalonde 如何在结束 ssh 会话后保持进程运行? 2010-10-22 04:09:13 +0800 CST
  • Martin Hope
    David B 如何使用命令行将用户添加为新的 sudoer? 2010-10-16 04:02:45 +0800 CST
  • Martin Hope
    Hans 如何删除旧内核版本以清理启动菜单? 2010-08-21 19:37:01 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve