5

As the title states, is it possible to have an alias only load when I access the machine through ssh, or better yet, only from a specific computer through ssh?

0

2 Answers 2

7

In .bashrc (or where you define the aliases), you can make it conditional based on the variable SSH_CONNECTION, which is set only for ssh connections. For example:

if [[ $SSH_CONNECTION == *"your-IP"* ]]; then
  alias ll="ls -l"
  # your other aliases
fi
2
  • Is there a way to check the MAC address rather than the IP? Or is that bad practice?
    – jrmo14
    Commented Jul 14, 2017 at 10:34
  • 1
    No, it is not possible unless you configure the clients somehow to send the MAC too. Identifying by mac is not a good idea.,
    – Jakuje
    Commented Jul 14, 2017 at 10:36
0

As i interpret your question, when you ssh to a linux box with an user other than root, the logged in user should have only limited access to the commands and you also want aliases to that commands. The possible solution is

  1. Create a restricted shell by copying bash file in /bin directory cp /bin/bash /bin/rbash
  2. Modify the user bash to the restricted bash useradd -s /bin/rbash , in case of existing user usermod -s /bin/rbash . After this the user will be able to access only his home directory.
  3. Create a directory under /home//commands
  4. Now to restrict the access of the commands add the following lines to /home//.bash_profile file PATH=$HOME/commands export PATH
  5. Create softlinks to the commands in /home//commands folder which you want to give permission to the user e.g: ln -s /bin/date /home//commands/
  6. As logged in user can modify the /home//.bash_profile you have to make it immutable so that user cannot access it. chattr +i /home//.bash_profile
1
  • Sorry, I didn't mean to restrict the permissions of a user to use an alias. The purpose was to have tmux -CC get aliased as tmux when I login over ssh. Thanks for the answer, I didn't know that that was possible.
    – jrmo14
    Commented Jul 14, 2017 at 10:27

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