For most people when they first come to Unix (or Unix-based, Unix-liked OS such as MacOS, Linux,…), usually one of the most confusing thing is the user account (home directory, permission,…). This article will provide an overview of some basic features about user account as well as some useful tips to take advantages of these features in Unix system.

Home directory

What is Home directory

First, let me explain what is Home directory in Unix system. In multi-user Unix systems, each user has it own home directory. It’s the folder which that user has the full read-write access. That user’s files is protected from other users in the same system, except for root. It’s also the place for the system to store that particular user’s preferences, habits and profile for applications. This mean the system as well as the applications can behave differently, apply different settings for each user thank to the config files stored in each user’s home directory. Windows users have not been familiar with the term Home directory until those recent version of Windows. In the past, the home folder for each Windows user is the My Document folder. In Windows 7, each user have there own home folder user C:\Users. However, the design of home folder on Windows is not as good as one on Unix system.

Keep your home directory safe

Since home directory contains all of your current user’s documents and settings, it is important to backup your home folder regularly, especially when you want to reinstall your computer. After finishing reinstalling, just copy all the content from your old home folder to the new one and all your configurations and files will be restored, you don’t need to adjust all the setting again. However, the problem may occur when you are backing up those files because you are manipulating them (the copy process may not happen). As a result, you need another method for backing up and restoring your home folder. The solution is to separate it from the OS. Here is an example:

table

As you can see in the picture above, I devided my HDD into 2 partitions, one for the OS (mounted on /) and one for my user account directory (mounted on ~). With this partition table, I can easily format and reinstall my OS (partition /dev/disk0s2) without having to worry about my data and preferences since they are all located in another separate partition. After the installation process finish, what I need to do is just relocate my home directory location and all my old workspaces is restored automatically so I can start working immediately.

Run as another user

For some applications (such as Dropbox, Google Drive,…), they do not allow multiple process of the same app to run at one time. Also, they do not permit you to have multiple profiles (to login into multiple Dropbox, Google Drive accounts at the same time). Or sometimes you may want an application to have many profiles, preferences for different purposes (eg. one for working and one for personal usage). If you’re on a Unix machine, the answer is really simple: Use another user account. However, you don’t have to logout of the current user and login to another user account to do that. Instead, Unix system offers a handy command to deal with that problem, the su command. Here are some examples for basic su usage. To enter another user login shell, simply type this command and enter that user’s password

$ su username

To enter another user login shell using sudo, simply executing this command and enter sudo password

$ sudo su username

After successfully logged into that user’s login shell, you can just open a program that you want and that program will run with another separate profile.

shell

To directly execute a command as another user without having to log into that user’s shell, use this one. Replace the username with that user’s account name, command with the command or program you want to execute. After that, input that user’s password

$ su username -c "command"

Adding sudo before that command will let you execute it using sudo password

$ sudo su username -c "command"

Extra: Some applications that I have tested successfully with this method

  • MacOS (10.8):

Dropbox 2.0.22 Google Drive 1.10.4769.0632

  • Linux mint 15 Cinnamon

Dropbox 2.0

Source: Truongtx.me blog


Trần Xuân Trường