Score:1

How can I create aliases upon connection to a remote machine via ssh?

sa flag

I frequently connect to ephemeral servers. I'd like to be able to set up some commonly-used aliases on those servers upon connection to them. I thought maybe I could create a bash function/alias which took the target machine's hostname as an argument, then did the following

  • open an ssh session to that server
  • copy a file of aliases I'd like to use on that server to the server and source them so they are usable immediately

Is this straight-forward to do? I could probably figure this out but since an answer didn't come up in search it's no harm to ask here and give someone the points, right? :)

Score:0
gt flag

You had the right idea with copying an aliases file. The tricky bit is loading them in the current context. The only way I've found is by running a new instance of bash with the file passed in:

bash --rcfile uploaded_aliases_file

This should work:

function ssh.test {
  TMP_RC=server_aliases
  scp rc_file [email protected]:${TMP_RC}
  ssh [email protected] "bash --rcfile ${TMP_RC} && rm ${TMP_RC}"
}

Or with a kubernetes pod where you pass the pod name:

function kube.bash {
  TMP_RC=server_aliases
  kubectl cp server_rc $0:$TMP_RC
  kubectl exec -it $0 -- bash -c "bash --rcfile ${TMP_RC} && rm ${TMP_RC}"
}

Note that I'm adding a command to delete the aliases, which you may or may not want, and is a bit flaky as it runs after your bash session, and may not run if the connection is interrupted etc...

You could probably make the file delete itself.

Score:0
jp flag

everything has already been done before us.

there is such a funny script as the ssh layer.

In fact, it transfers itself and other files to the server in the same session, which does not require additional confirmation of the use of ssh-key.

https://github.com/cdown/sshrc

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.