2

I am looking for a way to parse a variable to the SSH config file. Currently I have a script that replaces the values before distribution. I'd rather eliminate this step. I've been over the manual several times aswel as the release notes but without succes, currently not sure if this is even possible.

Preferably I'd do something like this:

variable_user = boxuser

Host thisbox Hostname thisbox User $variable_user

Ideally I'd want to include the config with the user variable so that everything else can remain standardised.

Am I asking too much?

Edit:

Passing $USER is not interpreted by the config

Host thisbox    
   Hostname thisbox 
   User $USER

Result:

$ ssh thisbox 
$USER@thisbox's password:

The same goes for %u

proxx
  • 87

2 Answers2

3

Use the $USER environment to set a user

Host MYHOST
    Hostname MYHOST
    User $USER

Check this for more SSH Environment Variables

SSH Environment Variables

Passing env variables to a ssh connection

SSH - set env vairables by every connection

When ssh'ing, how can I set an environment variable on the server that changes from session to session?

Other reasons environment variables are not being interpreted on the client side:

  1. is the environment variable set correctly
  2. is the environment variable exported
  3. Check the ssh_config for SendEnv, is your environment variable listed SendEnv ENV_VARIABLE
  4. Try using the -t option ssh -t ...
  5. Create a ~/.ssh/environment file on the client ENV_VARIABLE=VALUE Set PermitUserEnvironment to yes on the server in sshd_config file

If you are trying to use user environment variables in an SSH session and they are not being interpreted,maybe that your ssh client is not configured to pass your environment variables. To pass environment variables, you can use the -t option when you connect to the host. This option tells SSH to allocate a pseudo-tty on the remote host, which allows environment variables to be passed

You may need to check your SSH client configuration or the documentation of your client

or

Use the SendEnv and AcceptEnv directives

Add this to you /etc/ssh/sshd_config

AcceptEnv SSH_USER

sshd_config(5) — Linux manual page | AcceptEnv

or

Use the $SSH_USER environment to set a use, maybe this will not work on all systems

Host MYHOST
    Hostname MYHOST
    User $SSH_USER

When you run the ssh command, it will read the value of the SSH_USER environment variable to replace the $SSH_USER

You will need to set the SSH_USER environment variable every time you open a new terminal session or add it to your ~/.bashrc to make it persist.

Search in the Bash CookBook for $SSH_USER, there are a view chapters, I only mentioned one:

Check this chapters:

10.3 Using Configuration Files in a Script

15.11 Getting Input from Another Machine

Bash CookBook

10.3 Using Configuration Files in a Script Problem You want to use one or more external configuration files for one or more scripts. Solution You could write a lot of code to parse some special configuration file format. Do yourself a favor and don’t do that. Just make the config file a shell script and use the solution in Recipe 10.2. Discussion This is just a specific application of sourcing a file. However, it’s worth noting that you may need to give a little thought to how you can reduce all of your configuration needs to bash-legal syntax. In particular, you can make use of Boolean flags and optional variables (see Chapter 5 and Recipe 15.11):

# In config file
VERBOSE=0 # 0 or '' for off, 1 for on
SSH_USER='jbagadonutz@' # Note trailing @, set to '' to use the current user
# In script
[ "$VERBOSE" ] || echo "Verbose msg from $0 goes to STDERR" >&2
[...]
ssh $SSH_USER$REMOTE_HOST [...]

Of course, depending on the user to get the configuration file correct can be chancy, so instead of requiring the user to read the comment and add the trailing @, we could do it in the script:

# If $SSH_USER is set and doesn't have a trailing @ add it:
[ -n "$SSH_USER" -a "$SSH_USER" = "${SSH_USER%@}" ] && SSH_USER="$SSH_USER@"

or just use:

ssh ${SSH_USER:+${SSH_USER}@}${REMOTE_HOST} [...]

to make that same substitution right in place. The bash variable operator :+ will do the following: if $SSH_USER has a value, it will return the value to the right of the :+ (in this case we specified the variable itself along with an extra @); otherwise, if unset or empty, it will return nothing. See Also • Chapter 5 • Recipe 10.2, “Reusing Code with Includes and Sourcing” • Recipe 15.11, “Getting Input from Another Machine”

Discussion We do a few interesting things here. First, notice how both $SSH_USER and $SSH_ID work. They have an effect when they have a value, but when they are empty they interpolate to the empty set and are ignored. This allows us to abstract the values in the code, which lends itself to putting those values in a configuration file, putting the code into a function, or both.

Z0OM
  • 3,149
  • @Peregrino69 it was a custom environment variable sry, wrong system i forgot that we adjusted a lot in our ssh environment we also use variable usernames in the scripts – Z0OM Mar 21 '23 at 10:00
  • 3
    Upvoted. Too bad can't do it twice :-) – Peregrino69 Mar 21 '23 at 11:07
  • Passing $USER is not interpreted by the config: ssh thisbox gives: $USER@localhost's password: – proxx Mar 21 '23 at 12:09
1

The $USER doesn't work on all systems or version if you don't have a customized bash/ssh environment, because bash does not interpret your ssh config file

You can fix this with your .bashrc file or create a custom_ssh file, source it over .bashrc file and create alias or variable

~/.bashrc

# add to the end of file
source custom_ssh_config

custom_ssh_config file use alias

#!/bin/bash

USE ALIAS

alias ssh_user_1="ssh ${USER}@HOSTNAME"

user2="myuser2" alias ssh_user_2="ssh -l ${user2} thisbox"

Run: ssh_user_1 or ssh_user_2

custom_ssh_config file use variable

#!/bin/bash

USE VARIABLE

ssh_user_1="${USER}"

user2="myuser2" ssh_user_2="${user2}"

Run:

ssh -l $ssh_user_1 thisbox or ssh -l $ssh_user_2 thisbox

For $USER: ssh -l $USER thisbox

you don't need User $USER in your ssh config file

SyncToy
  • 198
  • 3
  • 13
  • But that would defeat the use of the SSH config file, it's way too complex and long to be rewritten in aliases. Thanks for the suggestion but it's not a solution :) – proxx Mar 23 '23 at 07:03