13

I have a few remote servers which I connect to through the terminal. The servers don't have a domain, only an IP address which is hard to remember when I got a few of them.

Is there a way to permanently store values in the terminal, so that I can do the following?

server1 = 111.222.111.222

And type commands like:

ssh root@server1

instead of:

ssh [email protected]

5 Answers 5

23

For SSH connections, you can create a user configuration file ~/.ssh/config and place the mappings there e.g.

Host server1
  Hostname      111.222.111.222

You can easily add other fields such as Port (for non-standard ports) and User (useful if your username on the remote system differs from that on the local system). See man ssh_config for full details.

14

Yes, just write them down in the file /etc/hosts. It has the following syntax:

1.2.3.4 servername additional_servername

Where:

  • 1.2.3.4 is the IP address
  • servername is the name
  • additional_servername is an optional name

After saving, you can reach the server by its name.

1
  • @theusual If this is the answer you went with, please consider accepting it (check mark below the up/down-vote buttons).
    – jpmc26
    Aug 28, 2015 at 21:17
8

Of course for your particular issue you want to follow @chaos and @steeldriver advices, but in the general case, in order to "permanently store values in the terminal", you are looking to shell variables. How to set them will depend on your shell (I guess echo $SHELL will provide the relevant information).

If by "permanently" you mean "as long as I don't exit this terminal session", then you can simply use the export server1=111.222.111.222 command if you're using a bash-based shell (setenv server1 111.222.111.222 for a csh/tcsh-based shell, if I remember correctly). Then you can access your variable by prefixing it with $ : ssh root@$server1.

If by "permanently" you mean "each time I launch a new terminal", then you will need to set your variable in one of the shell init files. E.g. in $HOME/.bashrc for bash-based shells, or $HOME/.cshrc — presumably using the same syntax as mentionned in the previous paragraph. Note that you will have to source the init file (e.g. source $HOME/.bashrc or open a new shell for changes to be taken into account.

NB : Answer from memory, may require some small adjustments.

2
  • 2
    By permanent I ment even if a restarted the computer. I'll take a look at this, it might come in handy some time.
    – theusual
    Aug 29, 2015 at 13:08
  • Then the second method applies (shell init files). Aug 29, 2015 at 13:21
1

Not a direct answer to the question (chaos and steeldriver have already provided what I think would be the best literal solutions), but if you only need to ssh to the one user at the one address in the majority of cases, then looking at editing the .bash_aliases file in your home directory should be useful, as you can create an alias for the entire terminal command.

0

The specific answers are very good.

In general, though, if you want to have some things defined/setup in a shell, just write a script to do it and then execute it with source or . when you want that setup in effect.

If it's something you want all the time, you can source it in .profile (for login shells) or in .bashrc for all shells (if you use bash).

With an approach like this, you can have multiple scripts to setup different working environments as needed as long as you make sure they don't step on each other if you invoke one of them after invoking another one first.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .