How to use SSH with alias - bash

I have an alias to a server machine:
alias myserver='user#server'
however, I cannot ssh into it by using the alias:
ssh myserver
ssh: Could not resolve hostname myserver: Name or service not known
the alias is correctly set:
myserver
returns bash: user#server: command not found.
Obviously, it works when I do it extensively:
ssh user#server
what is going on? I am pretty sure it has been working until today... But not sure what I changed.
Any help?

you can either have an Alias in your bash configuration which includes the actual ssh command aswell ...
alias myserver='ssh user#myserver'
... or have an alias in your ssh config file.
Host myserver
HostName 10.10.20.20
User user
ssh myserver should work.
A more extensive answer can be found for example here.

Not sure about the alias, but I use ~/.ssh/config:
Host myserver
HostName server
User user
Port 22
Then ssh myserver is just like your alias for ssh user#server.

Related

SCP does not recognize configuration from .ssh/config

I've configure the file .ssh/config to make easier connections:
Host MyHost
HostName my.univ.edu
User me
Port 22
Host cluster
HostName thecluster
User me
ProxyJump MyHost
When I do ssh cluster everything works well. However, scp commands do not work:
$ scp cluster:/path/to/my/file .
zsh: no matches found: cluster:/path/to/my/file
Any idea why?
zsh seems to be doing file globbing here, can you do :
scp "cluster:/path/to/my/file" .

Make a parameter subsitution in bash aliases for ssh

Is there a way I can create a alias for this command and have it ask for the host.
ssh -i .ssh/name.pem root#
Thx
Something like the following should work (not tested)
sshfunction(){
echo "Specify your hostname:"
read host
ssh -i .ssh/name.pem root#"$host"
}
Then:
$ sshfunction
Though if it was me, I'd just provide the hostname as a variable and cut-out the middle man.
Better yet, populate your ~/.ssh/config file (if it doesn't exist you can just create it):
host MyHostName
Hostname 123.456.7.89
User username
Then:
$ ssh MyHostName

Hosts declared in ssh configuration on Windows are not visible

I am using Git on Windows with Git Bash.
In C:\Users\myuser\.ssh I created a config file, where I declared a host
Host my-host along with some configuration.
The config file has the following content:
Host my-host
User my-username
Hostname my-repo.com
Port 7999
IdentityFile id_rsa
In theory, I should be able to connect to this host from the command line:
ssh my-host.
However, that doesn't happen, if try to run this command from Git Bash, I get:
ssh: Could not resolve hostname my-host: Name or service not known
So how can I make this host visible in Git Bash or in Windows Command Line?
Check port number, 7999 is not usual for ssh
set full path for id_rsa, like ~/.ssh/id_rsa
maybe you should use alias name without "-"
check connection with your parameters without alias, like "ssh -i~/.ssh/id_rsa -p7999 user#host"
check you id_rsa, is it private key?
check rights on folder and file: ~/.ssh - 700, ~/.ssh/config - 600

SSH setup issues

I am trying to use ssh however when I try and set it up by typing
ssh - host - config
I get the errot "ssh: Could not resolve hostname -: Name or service not known"
What should I do?
Error is either in server name or in the way you call it!
ssh user#server

BASH label for IP addresses

My fault: I have been so busy learning other linux stuff that I completely neglected the power of bash.
I have a number of systems to access remotely for very simple operations. The problem is that I need to remember each single IP address. And they are a lot.
Using aliases in ~./bashrc is an option:
alias ssh_customer1='ssh root#10.X.X.X'
alias ssh_customer2='ssh root#10.X.X.Y'
alias copy_customer1='scp * root#10.X.X.X:/etc/example/'
alias copy_customer2='scp * root#10.X.X.Y:/etc/example/'
alias get_customer1='scp root#10.X.X.X:/etc/example/* .'
alias get_customer2='scp root#10.X.X.Y:/etc/example/* .'
but the flexibility is minimal.
Another possibility is to define functions using the name of system as a parameter but I don't like this:
sshx('customer1')
scpx('customer2')
I would prefer to just replace a label with the corresponding IP address without the need to remember it, and use standard commands:
ssh root#ip_customer1
scp root#ip_customer2:/etc/example/* .
Is this possible?
Setup a ~/.ssh/config file:
$ cat ~/.ssh/config
Host cust1
HostName 10.X.X.X
User root
Host cust2
HostName 10.X.X.Y
User root
Now you can use:
ssh cust1
Another cool thing is that you can now attach identity files to each server:
$ cat ~/.ssh/config
Host cust1
HostName 10.X.X.X
User root
IdentityFile ~/.ssh/cust1.id_rsa
Host cust2
HostName 10.X.X.Y
User root
IdentityFile ~/.ssh/cust2.id_rsa
This will let you use ssh and scp without password, assuming the key is without password or ssh-agent is used.

Resources