Solved SSH: How to land into a specific folder?

Hello folks,

I wonder how can I land into a specific folder on my pet server while I ssh into… ?‍♂️
I searched on the internet but as usual come out information about Linux & Bash… ?‍♂️

Also my preferred method is using midnight-commander because what I do mostly is uploading files, but I am wondering if maybe using a sshfs is a solution. ?

To finish, which is the method to store the ssh password for session? I ended up typing the password hundreds times at day… ?

Thanks! ?
 
And why do you think this makes any difference?

Perhaps I do not understand very well the matter, and skimming the commands suggested I found always a bash command attached somewhere, and I ended up to the conclusion those suggestions were too much linux-y... ?
 
Hello folks,

I wonder how can I land into a specific folder on my pet server while I ssh into… ?‍♂️
I searched on the internet but as usual come out information about Linux & Bash… ?‍♂️

Also my preferred method is using midnight-commander because what I do mostly is uploading files, but I am wondering if maybe using a sshfs is a solution. ?

To finish, which is the method to store the ssh password for session? I ended up typing the password hundreds times at day… ?

Thanks! ?

ssh -t [IP] 'cd [DIRECTORY] && bash'
I use bash

some google help do it :
How can I ssh directly to a particular directory?
 
If you just need to upload files, look for a GUI sftp application like WinSCP for your client that will remember passwords and the last directory you were in. If you're doing this on a local network, you could also try creating a SAMBA or NFS local network share on the server.

The easiest way to learn ssh from scratch is to read a short book like SSH Mastery by Lucas.

The commands you need to learn for login without a password are
  1. ssh-keygen (run once on the client to generate client-keys)
  2. ssh-copy-id (run once on the client to copy the client's public key to the server)
  3. ssh-agent (run every time you log into the client or use something like keychain to do it only once after the client reboots)
You can restrict sftp users into new directories created for them by adding them to a group, Matching that group in the sshd configuration, using ChrootDirectory to specify a root-owned parent-folder, then specify the user's folder below that using ForceCommand internal-sftp. I do not know if there is a way to restrict a user to their own /home directory for general ssh logins.

Code:
# tail /etc/ssh/sshd_config
[...]
# restrict each <user> in the backup group to directory /backup/<user>
Match Group backup
        ChrootDirectory /backup/
        ForceCommand internal-sftp -d %u
 
ssh host 'cd /dir; /bin/sh -i'
ssh -t [IP] 'cd [DIRECTORY] && bash'
I use bash

ssh -t myhost 'cd <somedir> && exec $SHELL -i' ($SHELL will point to whatever shell you've set on that account). Note that you're going to need -t or else it's going to complain about not having a proper TTY.

Code:
dice@molly:~ % ssh -t williscorto 'cd test && exec $SHELL -i'
dice@williscorto:~/test %
 
Don't want to hijack this thread, but I want this result with a server configuration. The aim is to avoid typing always the same commands once connected. No point to type them before connection.

I tested ChrootDirectory and ForceCommand without luck. I will try again following this example.
 
ChrootDirectory is something else entirely. This puts the user in a chroot(8) environment. ForceCommand is a script or executable that gets executed when a user logs in. ForceCommand is going to pose a problem if the user wants to execute something else remotely (think ssh myhost 'tar -C /somedir -cf -' | tar -C /localdir -xvf-' for example).
 
Yes, I recall that with ChrootDirectory, I got an error, it can't find some binaries. And ForceCommand doesn't work when executing cd.
 
to finish, which is the method to store the ssh password for session? I ended up typing the password hundreds times at day…
Use keys. See ssh-keygen(1).

The aim is to avoid typing always the same commands once connected.
Why don't you simply add those commands to .login, .profile, .bash_profile or .cshrc (depends on your shell which one to use)? That's what they're for.
 
Concerning ssh, modifying ~/.profile works (the shell is /bin/sh).
Code:
cd /TheDirIwant
#if [ "$PWD" != "$HOME" ] && [ "$PWD" -ef "$HOME" ] ; then cd ; fi

But I use also (and mainly) sftp and ~/.profile has no effect. I eventually found a simple addition to make in /etc/ssh/sshd_config:
Subsystem sftp /usr/libexec/sftp-server
Becomes:
Subsystem sftp /usr/libexec/sftp-server -d /TheDirIwant
This last impact all the users but you can use tokens, see: sshd_config(5).
 
Last edited:
A friend from OpenBSD told me how to open a remote folder with mc!

It works only from cli:
Code:
mc sftp://user@doma.in/usr/home/your-home/

?
 
An office server is running FreeBSD 12.1 (newer versions do not boot; AMD Opteron 6174) and there are jails for older versions; here is what's at the bottom of /etc/ssh/sshd_config:
Code:
Match Group freebsd112
        ChrootDirectory /srv/jails/freebsd-11.2
Match Group freebsd113
        ChrootDirectory /srv/jails/freebsd-11.3
Match Group freebsd120
        ChrootDirectory /srv/jails/freebsd-12.0
Match Group freebsd121
        ChrootDirectory /srv/jails/freebsd-12.1
Output from id freebsd112: uid=0(freebsd112) gid=0(wheel) groups=0(wheel),5(operator),1002(freebsd112), id freebsd113: uid=0(freebsd113) gid=0(wheel) groups=0(wheel),5(operator),1003(freebsd113), ... you got the idea.

Logging in as respective user will land you in respective jail. There are some quirks to have ssh-agent(1) running (null mounts and full system trust), and also there is no way of entering the jail -- no such option in sshd_config(5) (anyone?), only ChrootDirectory.
 
Back
Top