2

I have two users, that have the same home directory:

useradd -m -d /home/mydir user1
useradd -m -d /home/mydir user2

and they are in the same group.

But I have a problem, when someone logged in the shell, in mydir appears the .bash_history file, of the first user with the commands he made.

Then if the user2 joins and start to make commands, the .bash_history remains of the first user with his commands. Is it possible to create bash history for both users?

  • 5
    "I have two users, that have the same home directory" Please re-evaluate this. What you should do is have 2 homes and share the directories in those home by adding those 2 users to the same group and setting the directories to that group. What you do is going to get messy: there are files in /home/$USER/ that need to be owned by that user and can't be used through the group settings. I have 22 files in /home/$user/ owned by me. Including the infamous .xsession-errors and .ICEauthority that have rw- --- --- as setting. Not going to work with 2 users. – Rinzwind Sep 1 '19 at 13:44
  • 2
    @Rinzwind Yes, and wanting separate bash histories is a case of wanting separate program data for each user, a strong indication that the users need their own individual home directories. If you were to post a new answer about just using separate home directories, I think that would be reasonable and helpful. Whether or nor Romans ends up using that solution, I expect nearly everyone who would describe a situation like the one described here, and who would find this question by searching, to be better served by giving the accounts separate home directories that each can access. – Eliah Kagan Sep 1 '19 at 16:59
  • Either one of the two users cannot use ssh, or the two users are security-equivalent because of .ssh/authorized_keys. There's probably more takeover methods as well. Typically stuff like this appears only when the home directory is owned by root and writable by neither. I can't imagine what you're trying to accomplish. – Joshua Sep 1 '19 at 21:58
  • @Rinzwind: Possible they have the same UID as well? Not that this makes sense, but it's consistent with everything else. – Joshua Sep 1 '19 at 22:00
  • @Joshua then those are 1 user. Not 2. And ssh is a good one and there is likely to be more. How about browser cash and history. – Rinzwind Sep 2 '19 at 2:24
8

I would highly recommend to keep separate user homes for different users and discourage the idea of having a shared home directory for different users.

This makes e.g. custom settings for users impossible, will likely break caches and configs for applications.


Concerning your question and assuming you already have made the according changes to /home/mydir to make user2 able to access and write to this directory, you could add the following line in /home/mydir/.bashrc:

HISTFILE="${HISTFILE}_${USER}"
  • @steeldriver: I did not have any preferences where to place this. '~/.bashrc` would also be an option. Since the HISTFILE assignment is within the bash check I don't think it is set for other shells. Not sure if there are some kind of best practices where to set such things? – Thomas Sep 1 '19 at 13:02
  • 1
    I recommend putting it in ~/.bashrc, though for different reasons than @steeldriver suggested. ~/.profile is only sourced by login shells. If the login shell was not bash--as in most graphical logins--subsequent interactive non-login bash shells won't have it. Also, even if one starts with a bash login shell that sources ~/.profile, running bash from it gives a non-login shell; since HISTFILE is not an environment variable (it's just a shell variable; it's not exported), that won't inherit it, and even if it did, the child shell would source ~/.bashrc which might reassign it. – Eliah Kagan Sep 1 '19 at 13:18
  • @Thomas i'm so sorry i'm not so expert about linux. i have created the two users with useradd -M -N -d /home/mydir user_name. So if i remove the -M it should create the directory, where the history, will be created, but with -d the default dir remains the shared dir (when they connect to shell)? Then i have not the .profile in the dir, only the .bash_history of the first user that i try to logged in – Romans Sep 1 '19 at 13:26
  • @EliahKagan: yes you are right. Will update my answer. Thanks – Thomas Sep 1 '19 at 13:38
  • answer is correct so upvoted but it will not fix upcomming issues with .xsession-errors and .ICEauthority – Rinzwind Sep 1 '19 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.