在使用需要自定义端口号的ssh登录时,有什么方法可以隐藏端口号 [英] is there any way to hide port number while login with ssh that need custom port number

查看:335
本文介绍了在使用需要自定义端口号的ssh登录时,有什么方法可以隐藏端口号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要登录到具有自定义ssh端口的服务器,例如-

I want to login to my server that has custom ssh port like -

ssh -p <my server port number> root@example.com

现在我想要这么短的命令(例如别名)需要一次又一次输入该端口号。像-

Now I want such short command (something like alias) that I don't need to input that port number again and again. Like -

ssh my-short-code

有可能吗?如果是,则欢迎任何提示。

Is it possible? If yes any hint are welcome.

推荐答案

转到您的 ssh 配置文件-我的文件位于 $ HOME / .ssh / config 中,并添加以下内容:

Go to your ssh config file - mine is in $HOME/.ssh/config, and add the following:

Host example
HostName example.com
User root
Port 2229

然后您可以执行以下操作:

Then you can just do:

ssh example

如果您希望能够运行X11应用程序(例如 xclock xterm 或任何图形软件开发工具),但在本地显示图形,则可以通过在上面显示的两行之后添加以下两行来转发X11:

If you want to be able to run X11 applications (e.g. xclock or xterm or any graphical software development tools) on the remote machine, but display the graphics locally, you can forward X11 by adding the following 2 lines after the ones shown above:

ForwardX11 yes
ForwardX11Trusted yes






回答您在评论中的进一步问题。是的,您可以继续添加任意数量的其他服务器,并使用不同的端口,用户名和保持活动间隔:


In response to your further question in the comments. Yes, you can just keep adding as many additional servers as you like, with different ports, usernames, keep-alive intervals for each:

Host example
HostName example.com
User root
Port 2229

Host sercon
HostName sercon.onlinehome-server.info
User u8566723
Port 378
StrictHostKeyChecking no

您还可以输入通用条目首先使用大多数服务器所需的设置,然后在各个服务器上覆盖它们:

You can also put a generic entry at the start with settings you want for most servers, and then override them later on individual servers:

Host *
Username mark
ServerAliveInterval 10

Host XYZ
<specific stuff for this host>

Host <ABC>
<specific stuff for this host>

要获取更多信息,请在终端机中使用:

To get further information, use this in Terminal:

man ssh_config






如果您碰巧在Mac上的 macOS / OSX 下,并且希望X11转发与XQuartz一起使用,请将这3行放在文件顶部:


If you happen to be on a Mac under macOS/OSX and you want X11 forwarding to work with XQuartz, put these 3 lines at the top of the file:

Host *
# https://serverfault.com/a/859370/235272
XAuthLocation /opt/X11/bin/xauth

这篇关于在使用需要自定义端口号的ssh登录时,有什么方法可以隐藏端口号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆