{"id":7879,"date":"2019-06-21T08:51:44","date_gmt":"2019-06-21T00:51:44","guid":{"rendered":"http:\/\/rmohan.com\/?p=7879"},"modified":"2019-06-21T08:51:45","modified_gmt":"2019-06-21T00:51:45","slug":"create-an-ssh-server-alias-on-a-linux-system","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7879","title":{"rendered":"Create an SSH server alias on a Linux system"},"content":{"rendered":"\n<ul><li><\/li><\/ul>\n\n\n\n<p>If you frequently access many different remote systems via SSH, this technique will save you some time. You can create SSH aliases for frequently accessed systems via SSH, so you don&#8217;t have to remember all the different usernames, hostnames, SSH port numbers, and IP addresses. In addition, it avoids  repeatedly entering the same username, hostname, IP address, and port number when SSHing to a  Linux server.<\/p>\n\n\n\n<p>Create an SSH alias in Linux<br>\nBefore I know this trick, I usually use one of the following methods to connect to a remote system via SSH.<\/p>\n\n\n\n<p>Use IP address:<\/p>\n\n\n\n<p>$ ssh 192.168.225.22<br>\nOr use the port number, username, and IP address:<\/p>\n\n\n\n<p>$ ssh -p 22 ec2-user@192.168.225.22<br>\nOr use the port number, username, and hostname:<\/p>\n\n\n\n<p>$ ssh -p 22 ec2-user@server.example.com<br>\nHere<\/p>\n\n\n\n<p>22 is the port number,<br>\nec2-user is the username of the remote system.<br>\n192.168.225.22 is the IP of my remote system,<br>\nServer.example.com is the host name of the remote system.<br>\nI believe that most Linux novices and\/or some administrators will connect to remote systems via SSH in this way. However, if you connect to multiple different systems via SSH, remembering all hostnames or IP addresses, as well as usernames, is difficult unless you write them on paper or save them in a text file. do not worry! This can be easily solved by creating an alias (or shortcut) for the SSH connection.<\/p>\n\n\n\n<p>We can create aliases for SSH commands in two ways  .<\/p>\n\n\n\n<p>Method 1 \u2013 Use an SSH Profile<br>\nThis is my preferred method of creating an alias.<\/p>\n\n\n\n<p>We can use the SSH default configuration file to create an SSH alias. To do this, edit the ~\/.ssh\/config file (if this file doesn&#8217;t exist, just create one):<\/p>\n\n\n\n<p>$ vi ~\/.ssh\/config<br>\nAdd details for all remote hosts as follows:<br>\nHost webserver<br>\n    HostName 192.168.225.22<br>\n    User ec2-user<\/p>\n\n\n\n<p>Host dns<br>\n    HostName server.example.com<br>\n    User root<\/p>\n\n\n\n<p>Host dhcp<br>\n    HostName 192.168.225.25<br>\n    User ec2-user<br>\n    Port 2233<\/p>\n\n\n\n<p>Create an SSH alias in Linux using an SSH configuration file<\/p>\n\n\n\n<p>Replace the values ??of the Host, Hostname, User, and Port configuration with your own values. After adding the details of all remote hosts, save and exit the file.<\/p>\n\n\n\n<p>Now you can access the system via SSH using the following command :<\/p>\n\n\n\n<p>$ ssh webserver<br>\n$ ssh dns<br>\n$ ssh dhcp<br>\nIt&#8217;s that simple!<\/p>\n\n\n\n<p>Access remote systems using SSH aliases<\/p>\n\n\n\n<p>see it? I only use an alias (such as webserver) to access a remote system with an IP address of 192.168.225.22.<\/p>\n\n\n\n<p>Please note that this is only for the current user. If you want to provide an alias for all users (system-wide), add the above line to the \/etc\/ssh\/ssh_config file.<\/p>\n\n\n\n<p>You can also add a lot of other content to your SSH configuration file. For example, if you have configured SSH key-based authentication, the location of the SSH key file is as follows:<\/p>\n\n\n\n<p>Host Ubuntu<br>\nHostName 192.168.225.50<br>\nUser senthil<br>\nIdentityFIle ~\/.ssh\/id_rsa_remotesystem<br>\nMake sure you have replaced your hostname, username, and SSH key file path with your own values.<br>\nNow connect to the remote server using the following command:<\/p>\n\n\n\n<p>$ ssh ubuntu<br>\nThis way, you can add as many remote hosts you want to access via SSH and quickly access them using aliases.<\/p>\n\n\n\n<p>Method 2 &#8211; Use a Bash Alias<br>\nThis is an emergency workaround for creating SSH aliases that speed up communication. You can make this taec2-user easier with the alias command.<\/p>\n\n\n\n<p>Open the ~\/.bashrc or ~\/.bash_profile file:<\/p>\n\n\n\n<p>Alias ??webserver=&#8217;ssh ec2-user@server.example.com&#8217;<br>\nAlias ??dns=&#8217;ssh ec2-user@server.example.com&#8217;<br>\nAlias ??dhcp=&#8217;ssh ec2-user@server.example.com -p 2233&#8242;<br>\nAlias ??ubuntu=&#8217;ssh ec2-user@server.example.com -i ~\/.ssh\/id_rsa_remotesystem&#8217;<br>\nAgain, make sure you have replaced the host, hostname, port number, and IP address with your own values. Save the file and exit.<br>\nThen, use the command to apply the changes:<\/p>\n\n\n\n<p>$ source ~\/.bashrc<br>\nor<br>\n$ source ~\/.bash_profile<br>\nIn this method, you don&#8217;t even need to use the ssh alias command. Instead, just use an alias as shown below.<br>\n$ webserver<br>\n$ dns<br>\n$ dhcp<br>\n$ ubuntu<\/p>\n\n\n\n<ul><li><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"\n<p>If you frequently access many different remote systems via SSH, this technique will save you some time. You can create SSH aliases for frequently accessed systems via SSH, so you don&#8217;t have to remember all the different usernames, hostnames, SSH port numbers, and IP addresses. In addition, it avoids repeatedly entering the same username, [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7879"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7879"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7879\/revisions"}],"predecessor-version":[{"id":7880,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7879\/revisions\/7880"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}