Load some alias just after SSH login [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
In my daily work I often need to use a ssh connection on a device (which you can consider as read only), and the commands I write are long.
That's why I would like to load some alias just after ssh login.
But when I try something as follow, it don't works:
ssh name#ipAdress "bash -l ; alias short='veryLongCommandThatIWriteOften'"
I guess that's because bash stop the processing of the other commands which are just after.
So is it possible to set aliases directly as an argument of bash, or is there another solution to do what I want?

Instead of an alias, you can use a shell function, which bash allows you to export. This way, you first define the function, then export its name, and finally start a new interactive shell which inherits your function. For example:
ssh -t name#ipAddress "short () { veryLongCommandThatIWriteOften; }; export -f short; bash"
The -t is necessary to set up the pseudo terminal for the interactive bash shell, as ssh won't do it automatically for an apparently non-interactive command.
Note that you many need to be careful about quoting, depending on what the body of short is.

Edit the file ~/.bashrc
nano ~/.bashrc
It has examples of how to set aliases also. Log back in to have new alias work.

Related

Alternatives to eval `ssh-agent` and exec ssh-agent bash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
Running either of these commands seems to start an SSH agent process successfully:
eval `ssh-agent`
OR
exec ssh-agent bash
I'm partial to the first one, because the second exec replaces the shell. Obviously the second, uses eval which is frowned upon by some, but I don't see alternatives.
My questions are:
Does exec have any negative side effects when replacing the shell or indeed any side effects at all? Are my concerns about using exec warranted?
I don't have an issue using eval but, out of interest what alternative commands are there without scripts or functions (and without exec or eval) to start an ssh-agent process in one line?
This is a "safe" use of eval, at least to the extent that you trust ssh-agent to output nothing but simple, hard-coded assignments similar to
SSH_AUTH_SOCK=/var/folders/...; export SSH_AUTH_SOCK;
SSH_AGENT_PID=xxxxx; export SSH_AGENT_PID;
echo Agent pid xxxxx;
The output of ssh-agent is specifically designed to be passed to eval, and let's face it: if ssh-agent wanted to do harm, it could do so in a quieter fashion.
The downside to using exec is that the new shell that replaces the original shell may not be identical; the environment is inherited, but some shell settings not found in .bashrc may be different. However, if you put exec ssh-agent bash in your .bashrc (especially as the last line), there there isn't really any opportunity for your shell's configuration to diverge from whatever .bashrc did. (There is also the possibility that you have non-idempotent code in your .bashrc, meaning that executing it twice will result in different behavior than having only executed it once. But again, that's unlikely and easily auditable.)

Using bash alias beyond one level (shell in shell) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I am looking to execute a alias on my terminal which will login to another shell and execute a command there.
For example,
sh = 'ssh admin#x.x.x.x'
shls = 'sh;ls' (also tried 'sh && ls')
In this scenario, when i give 'shls', i want to ssh to (pwd less entry enabled) x.x.x.x and then execute ls command over there. But the 'ls' part is not working.
I understand the shell changed and hence its no longer in parent shell's scope to trigger the ls, but just wondering if there is a way to push it to the logged in shell and execute there.
Infact, i wanted to use another alias which is avaiable in x.x.x.x in place of 'ls' but as a first step i want to atleast get this working.
Hope i could put it clearly, Thanks in advance for your help.
You can pass a command to ssh as an argument (after the various connection parameters):
alias shls='ssh admin#x.x.x.x ls'
BTW, I'd recommend against aliasing sh -- that's a commonly used command to run a shell script(*), and giving it a different meaning could cause confusion.
(* Though instead using the sh command, it's generally better to give the script a proper shebang line, and just enter its path.)

Is it possible to update a varible from one shell to another shell? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
How can we update a variable from one shell to another shell ?
Suppose , I am having 2 Putty sessions opened , I want to set a variable in the first SHELL and I need that variable to access from the 2nd SHELL .
Is is possible ?
You can save the variable to a script.
Then source the script in the 2nd session.
For example:
# session 1
hello=world
echo "hello=$hello" > /tmp/var.sh
# session 2
. /tmp/var.sh
echo $hello
As each process' environment is protected, there's no way to share environment variables. I would suggest using a file on a shared filesystem to store the variable you want and reading that file in whenever you'd need to know what the new value is.
It is usually not possible, because each shell (and each process) has its own environment. See execve(2).
However, you might want to switch to the fish shell. It gives you so called universal variables which might be shared between several instances of (i.e. processes running) the fish shell. This is implemented thru the fishd user daemon (with which every fish process communicates).

SSH : how to assign specifics directories to each user? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
No, it is not a duplicate of that, because I want to assign one directory to one (or more) user, when they log in throught SSH.
For example, according to user's loggin :
userFoo => I want him to arrive /somewhere/here/ after SSH login
userBar => I want him to arrive /somewhere/here/ after SSH login
userOof => I want him to arrive /anywhere/ after SSH login
And so on.
How can I do that ?
(They are connection with PuTTY and I can't force them to connect with additionnal data like ssh -t user#server 'cd /home/some/dir ; exec "$SHELL"'
Change the home directories of your users, as that's the place where they land after connecting.
The second to last entry in /etc/passwd holds this information, and can be edited for example from
userFoo:x:1000:1000:,,,:/home/userFoo:/bin/bash
to
userFoo:x:1000:1000:,,,:/somewhere/here:/bin/bash
When you log in a remote machine via SSH, it starts a shell, in most cases it's bash. It has a script, which executes every time it starts, can be found at ~/.bashrc. You may edit them, append a cd /somewhere to them.
Anyway, it's probably not a good idea, it's better to link appropiate folders to the users:
ln -s /somewhere/here ~/userFoo/comehere
ln -s /somewhere/here ~/user/comehere
ln -s /anywhere ~/userOof/comehere
So, you may just tell the users "please perform a cd comehere after you log in", it will drop them to the specified folders.

Why does my root user have a different shell prompt to other users with the same shell? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
The root user on my CentOS 6 distro has a different shell prompt to other users that I create.
If I
echo $PS1
for root user, I get [\u#\h \W]\$
If I do the same for another user, I get \s-\v\$
If I run
which bash
I can see that both users are using the same shell (/bin/bash/).
Is the $PS1 variable being set differently for individual users or groups somehow?
Usually it sets a different PS1 for root in /etc/profile or /etc/bash.bashrc.
Run echo $SHELLto identify your shell (which bash tells you where Bash is, not whether you're using it).
To clearly tell if you at each and every command line whether you have super-user (root) privileges or not
Yes, it is set different (for users). The PS1 variable is read from the .bashrc of your home directory or /etc/profile or /etc/bash.bashrc.

Resources