LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Starting shell dir when home dir is a symlink (https://www.linuxquestions.org/questions/linux-general-1/starting-shell-dir-when-home-dir-is-a-symlink-4175497974/)

oryan_dunn 03-12-2014 03:30 PM

Starting shell dir when home dir is a symlink
 
My setup, home dirs are /users/%username%, /users is a symlink to /mnt/users.

When you start a shell locally, the shell starts in /mnt/users/%username%, $HOME is /users/%username% and if you cd ~, you are in /users/%username%.

If you ssh into the box, you start in ~ (pwd shows /users/%username%). It looks like local terminals are following the symlink. I could always add a cd ~ or similar to a startup script in my homedir, but I'd like to solve this globally. The issue is that /mnt/users/%username% paths are creaping into some programs (notably local eclipse installs).

notKlaatu 03-12-2014 04:38 PM

Is there a reason you are symlinking home dirs? why not just define a user's homedir as /mnt/users/$USER when you add the user to your system? ie, `useradd --base-dir /mnt/users/ --create-home USERNAME`

Assuming you have your reasons, however, I am assuming you created your symlinks with `ln -s`
Try just `ln` instead.

rknichols 03-12-2014 06:02 PM

The shell remembers how it got to the current directory, and when you use the builtin pwd command or use the internal PWD shell variable it just displays that remembered path, which might include symlinks. The cd command (a shell builtin) also uses that remembered path, so "cd .." might not get you to the directory that is physically the parent of your current directory.

Any external program that starts in the current directory and uses ".." to refer to the parent will always get the physical parent directory. This can lead to confusion, as in your case, where "cd ../..; ls" would yield entirely different results from "ls ../..". Similarly, any program that uses the getcwd() system call will get the physical path from the root to the cwd, and no symlinks.

Instead of using a symlink, I suggest making /users a directory and bind-mounting /mnt/users there. In /etc/fstab it would look like this:
Code:

/mnt/users  /users  none  bind  0  0
The command to do that mount manually would be
Code:

mount --bind /mnt/users /users
Now everyone will see /users/USERNAME as a consistent path to the home directory, "cd ../.." will get you to the root directory and not /mnt, and getcwd() will return the "/users/USERNAME" path.

FWIW, on my own system the home directories are physically in /var/home which is bind-mounted to /home. I've been using it that way for years with no confusion occurring. Backup scripts do have to know not to descend into the /home mount point, but that's no different from avoiding other mount points when backing up a filesystem.

@notKlaatu -- Not even root is allowed to make a hard link to a directory. Many parts of the system make the tacit assumption that the directory structure is a tree, and hard linked directories would break that.

oryan_dunn 03-12-2014 06:24 PM

Quote:

Originally Posted by rknichols (Post 5133561)
The shell remembers how it got to the current directory, and when you use the builtin pwd command or use the internal PWD shell variable it just displays that remembered path, which might include symlinks. The cd command (a shell builtin) also uses that remembered path, so "cd .." might not get you to the directory that is physically the parent of your current directory.

Any external program that starts in the current directory and uses ".." to refer to the parent will always get the physical parent directory. This can lead to confusion, as in your case, where "cd ../..; ls" would yield entirely different results from "ls ../..". Similarly, any program that uses the getcwd() system call will get the physical path from the root to the cwd, and no symlinks.

You are spot on. I had never noticed that before. When I ssh in and bash shows me at my 'home' dir of ~, ls ../.. and cd ../..;ls do give different results.

I looked into it, pwd -P give the physical path; I bet bash uses the C getcwd() as you say and hence why it starts in /mnt/users/%username%

Quote:

Originally Posted by rknichols (Post 5133561)
Instead of using a symlink, I suggest making /users a directory and bind-mounting /mnt/users there.

That sounds like an excelent solution. Does the bind mount have to appear after the actual mount to /mnt/users? It'll be no problem, just wondering if mount can sort things out.

rknichols 03-12-2014 10:53 PM

Quote:

Originally Posted by oryan_dunn (Post 5133568)
Does the bind mount have to appear after the actual mount to /mnt/users? It'll be no problem, just wondering if mount can sort things out.

I have to confess I've never really thought about that. I always just added it at the bottom of my /etc/fstab, which of course put it after the other mounts.

oryan_dunn 03-14-2014 12:41 PM

Well, from the command line, it works great. However, it doesn't appear to be working from fstab. I've tried putting the bind mount before and after the /mnt/users nfs mount. It looks like the bind mount is happening before the nfs mount of the /mnt/users, so /users is still pointing to the empty /mnt/users dir on the local machine instead of the remote mount. I'm pretty sure the fstab options are correct, since I can call
Code:

# mount /users
and the system bind mounts as you'd expect.

It seems like the nfs mount it taking too long and in either case of the bind mount appearing before or after the nfs mount in fstab, it gets mounted before the nfs mount.

Any ideas?

rknichols 03-14-2014 01:07 PM

You had not mentioned that NFS was involved, but indeed, that won't work with an NFS mount. Local filesystems get mounted first. NFS mounts come later. Actually, I'm a bit surprised that bind-mounts of NFS directories work at all.

oryan_dunn 03-14-2014 01:16 PM

My appologies. I'm looking into why the nfs mount was moved and whether we can just nfs mount in both places.


All times are GMT -5. The time now is 07:31 AM.