is there any way to hide port number while login with ssh that need custom port number

前端 未结 2 499
逝去的感伤
逝去的感伤 2021-01-24 05:52

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

ssh -p  root@example.com

Now I want such short comman

相关标签:
2条回答
  • 2021-01-24 05:54

    You can't do exactly that, but you can do something very similar. In your .bashrc add this line:

    export myshortcode='-p <my server port number> root@example.com'
    

    Now you execute the command loke this:

    ssh $myshortcode
    

    Or you add an alias like

    alias ssh_port='ssh -p <my server port number> root@example.com'
    
    0 讨论(0)
  • 2021-01-24 06:04

    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
    

    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
    

    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
    
    0 讨论(0)
提交回复
热议问题