SSH with a user alias?

8,205

Each user in linux has only one name and that is his only name. you can create aliases for commands not for users.

But you can create a second user with the same UID, home directory and password that would do the trick for you.

Share:
8,205

Related videos on Youtube

dw8547
Author by

dw8547

Updated on September 18, 2022

Comments

  • dw8547
    dw8547 almost 2 years

    This question is not a duplicate. Please read it before you mark it as such.

    Is it possible to assign nicknames or aliases to users on a Linux sever and SSH into the server using the nicknames? I am thinking something along the lines of the aliases for various commands that get added to the .bashrc or the .bash_aliases file, e.g.:

    alias grep='grep --color=auto'
    

    For example, if there is a requirement (business rule) to set up users on the server with their full name, e.g., john_smith instead of just their first name (john) but we want to nickname john_smith fruitloops and we want John to be able to:

    ssh john_smith@ip_address
    

    as well as:

    ssh fruitloops@ip_address
    

    If it is possible, where would the mapping between a user and their nickname be set up? Would the user fruitloops also need to exists on the sever?

    This question is about setting up an alias for a user, not a host.

    • Takkat
      Takkat about 7 years
      With a host alias you could do ssh fruitloop
    • dw8547
      dw8547 about 7 years
      My question is about setting up an alias for a user, not the host. fruitloops refers to the user john_smith, not the IP address.
    • Takkat
      Takkat about 7 years
      That's what a User fruitloop line would be for. You can add several entries in one config file.
    • Ravexina
      Ravexina about 7 years
      I believe these are not the same questions ... +1 to question ;)
    • Takkat
      Takkat about 7 years
      Sorry for my misunderstanding - a SSH host alias would have to be set up on the client but what you need is a solution that was set up on the server. Retracted my close vote.
    • Sumudu Fernando
      Sumudu Fernando about 7 years
      I think this is possible with some PAM trickery (if you're using PAM that is). I may be able to find some old code along these lines (if I do I'll post as an answer).
    • guntbert
      guntbert about 7 years
      I don't really understand your real goal - you want to be able to access a remote account by not using the real (remote) account name ?
    • dw8547
      dw8547 about 7 years
      @guntbert, the requirement is to assign accounts to users on the remove using their full names e.g., john_adam_smith_brown but the users don't want to have to type that in everytime they want to SSH into the remote. They want to type something much shorter, like a nickname they chose for themselves or just their first name. So john_adam_smith_brown would like to SSH in as jon.
    • hanshenrik
      hanshenrik over 6 years
      personally i found it sufficient to do alias sshfoo='ssh [email protected]'
  • dw8547
    dw8547 about 7 years
    So my /etc/passwd entries corresponding to these users would look like: john_smith:x:1001:1001:,,,:/home/john_smith:/bin/bash and fruitloops:x:1001:1001:,,,:/home/john_smith:/bin/bash ?
  • dw8547
    dw8547 about 7 years
    I tried 3 different approaches after adding user john_smith with UID = 1001. 1) adduser --home /home/john_smith --uid 1001 fruitloops, this did not work (adduser failed because userid was taken) 2) useradd --home /home/john_smith --non-unique --uid 1001 fruitloops, this did work but lead to some behaviour that wasn't desirable 3) Finally I added the line fruitloops:x:1001:1001:,,,:/home/john_smith:/bin/bash directly to the /etc/passwd file and this was closest to what I was after. I SSH into ip_address as fruitloops and land in /home/john_smith upon arrival.
  • Monty Harder
    Monty Harder about 7 years
    I'm going to quibble with your wording here. In your scenario, you've created one user (1001) with two different login names. You haven't done it here, but there's no reason the two names can't have different home directories and login shells. Log in as "fruitloops" and create a file. Now check the directory and you'll see that john_smith is shown as the file's owner, because that's the first name found in /etc/passwd for uid 1001. We can argue about whether john_smith is the name and fruitloops is an alias, or john_smith is the primary name and fruitloops secondary, but that's semantic.
  • David Foerster
    David Foerster about 7 years
    "you can create a second user with the same UID, home directory and password" – that sounds like a terrible hack that's not guaranteed to work across different POSIX-compliant implementations. POSIX requires that the relationship between user names and user IDs be bidirectionally distinct.
  • Monty Harder
    Monty Harder about 7 years
    @dw8547 Can you explain a bit more about the undesirable behavior from the useradd command?
  • dw8547
    dw8547 about 7 years
    @MontyHarder, when the user arrives at the host, we want the default shell to be bash. When testing this out with approach 2) above without any additional changes to the configuration files for the user, when we arrive at the host, we land in the POSIX shell. In this shell, the prompt looks like $ so the user doesn't see the helpful information that the bash shell displays at the prompt, i.e., user_name@host_name:current_directory$.
  • dw8547
    dw8547 about 7 years
    @MontyHarder, yes, if the user logs in as fruitloops and say creates a file, the file owner and author is showing up as john_smith. That is in fact what we want. We only want to be able to SSH in with what is supposed to be a shorter alias. In the example I made up fruitloops isn't really that much shorter but we want to come up with a solution that will allow john_adam_smith_brown to log in as jon and daniel_john_smith_white to log in as dan, etc.