What is the command that changes the directory from the current directory to the home directory of a user named Bob?
Linux is a multi user OS which means that it supports multiple users at a time. As many people can
access the system simultaneously and some resources are shared, Linux controls access through ownership and permissions. In Linux, there are three types of owners: user, group, and others . A user is the default owner and creator of the file. So this user is called owner as well. A user-group is a collection of users. Users that belonging to a group will have the
same Linux group permissions to access a file/ folder. You can use groups to assign permissions in a bulk instead of assigning them individually. A user can belong to more than one group as well. Any users that are not part of the user or group classes belong to this class. File permissions fall in three categories: read, write, and execute. For regular files, read permissions allow users to open and read the file only. Users can't modify the file. Similarly for directories, read permissions allow the listing of directory content without any modification in the directory. When files have write permissions, the user can modify (edit, delete) the file and save it. For folders, write permissions
enable a user to modify its contents (create, delete, and rename the files inside it), and modify the contents of files that the user has write permissions to. For files, execute permissions allows the user to run an executable script. For directories, the user can access them, and access details about files in the directory. Below is the symbolic representation of permissions to user, group, and others. Note that we can find permissions of files and folders using long listing (ls -l) on a Linux terminal. In the output
above, d represents a directory and- represents a regular file. Now that we know the basics of ownerships and permissions, let's see how we can modify permissions using the
chmod command. Syntax of chmod: Where, We can change permissions using two modes: Now, let's see them in detail. The table below summarize the user representation: We can use mathematical operators to add, remove, and assign permissions. The table below shows the summary: Suppose, I have a script and I want to make it executable for owner of the file zaira. Current file permissions are as follows: Let's split the permissions like this: To add execution rights (x) to owner (u) using symbolic mode, we can use the command below: Output: Now, we can see that the execution permissions have been added for owner zaira. Additional examples for changing permissions via symbolic method: Absolute mode uses numbers to represent permissions and mathematical operators to modify them. The below table shows how we can assign relevant permissions: Permissions can be revoked using subtraction. The below table shows how you can remove relevant permissions. Example: chmod 451 file-name This is how we performed the calculation: Note that this is the same as r--r-x--x. To remove execution from other and group, subtract 1 from the
execute part of last 2 octets. This
would be the same as rwxr-xr--. Next, we will learn how
to change the ownership of a file. You can change the ownership of a file or folder using the chown command. In some cases, changing ownership requires sudo permissions. Syntax of chown: Let's transfer the ownership from user zaira to user news. chown news mymotd.sh Command to change ownership: sudo chown news mymotd.sh Output: How to change user and group ownership simultaneouslyWe can also use chown to change user and group simultaneously. chown user:group filenameHow to change directory ownershipYou can change ownership recursively for contents in a directory. The example below changes the ownership of the /opt/script folder to allow user admin. chown -R admin /opt/scriptHow to change group ownershipIn case we only need to change the group owner, we can use chown by preceding the group name by a colon : chown :admins /opt/scriptLinux Permissions Guided ExerciseUp until now we have explored permissions, ownerships, and the methods to change them. Now we will reinforce our learning with a guided exercise. Goal: To create groups and assign relevant permissions to its members. Verify access by accessing it from unauthorized users. Task: Create a group called dev-team and add two members (John and Bob) to it. Create a folder /home/dev-team and change ownership to group dev-team. Verify that both users in the dev-team group have read and write access to the folder. Create another group project-manager and add a user Fatima to it. Verify if the folder /home/dev-team is accessible by Fatima. Visualization of the problemWe can visualize the problem like this: Step 1: Switch to root user. Use the sudo command with flag i. If you have the root password, you can login using that as well. Show solutionEnter sudo -i to switch to the root user. Enter whoami to find out if you are the root user: If you do not have root access, use the commands with appending sudo. Step 2: Create a group dev-team Show hintUse the groupadd command. Syntax: groupadd group-name Show solutionEnter groupadd dev-team to create the dev-team group Verify: cat /etc/group | grep dev-team Step 3: Create two new users John and Bob and add them to the dev-team group Show hintUse command useradd. useradd creates a new user and adds to the specified group. Syntax: useradd -G groupname username Where -G specifies the group. Show solutionuseradd -G dev-team John useradd -G dev-team Bob Verify: cat /etc/group | grep dev-team Step 4: Provide passwords for users John and Bob Show hintUse command passwd passwd creates a password for users. Syntax: passwd username Show solutionpasswd John passwd Bob Step 5: Create a directory in /home and name it dev-team Show hintUse command mkdir mkdir creates a directory. Syntax: mkdir directory-name Show solutionmkdir /home/dev-team Verify: Step 6: Change the group ownership of the folder dev-team to group dev-team Use command chown Syntax: chown :group-name folder Show solutionchown :dev-team /home/dev-team/ Step 7: Make sure the permissions of folder dev-team allow group members to create and delete files. Show hintUse command chmod Write permissions allow users and groups to create and delete files. Syntax: chmod permissions folder Show solutionchmod g+w /home/dev-team/ Step 8: Ensure that 'others' don't have any access to the files of dev-team folder. Show hintUse command chmod Remove read, write, execute permissions from 'others' if they exist. Syntax: chmod permissions folder Show solutionchmod o-rx dev-team Step 9: Exit the root session and switch to John Show hintUse command exit to logout of the root user. Use su to switch users. Syntax: su - user To confirm current user, use command whoami. Show solutionexit su - John Verify with command whoami. Step 10: Navigate to folder: /home/dev-team Show hintUse command cd to switch folders. Syntax: cd /path/to/folder Confirm current path with pwd. Show solutioncd /home/dev-team Step 11: Create an empty file in the folder: /home/dev-team Show hintUse command touch to create an empty file. Syntax: touch filename Show solutiontouch john-file.txt Verify: ls -lrt Step 12: Change the group ownership of the created file to dev-team and verify. Show hintUse command chown to change ownership. Syntax: chown :group file-name Show solutionchown :dev-team john-file.txt Once group ownership is modified, all members of the group can access this file. Verify ls -lrt Step 13: Exit the shell and switch to user Bob Show hintUse command exit to exit the terminal. Use su to switch users. Syntax: su - user To confirm current user, use command whoami. Show solutionexit su - Bob Verify the current user with command whoami. Step 14: Navigate to the path /home/dev-team Show hintUse command cd to switch folders. Syntax: cd /path/to/folder Confirm current path with pwd. Show solutioncd /home/dev-team Step 15: Find out Bob's privileges to access john-file.txt Show hintUse command ls -l for long listing. Syntax: ls -l | grep file-name Does group have rw- permissions? Show solutionls -l | grep john-file.txt Step 16: Modify the file john-file.txt while logged in as Bob Show hintUse command echo to add some text to the file. Syntax: echo "Some text" >>file-name This would redirect the quoted text to end of the file. Show solutionecho "This is Bob's comment" > john-file.txt If all the permissions are correctly set, Bob would be allowed to edit and save this file. Otherwise you would get an error like this: Permission denied. Verify cat john-file.txt Step 17: Create another group project-manager and assign a member Fatima to it Show hintUse command groupadd to add a new group. Syntax: groupadd group-name Create a new user with command useradd. Use flag -G to assign a user to it. Show solution groupadd project-manager useradd -G project-manager Fatima passwd FatimaStep 18: Navigate to folder /home/dev-team and verify if Fatima can access it Show hintUse cd to navigate to /home/dev-team. Show solutioncd /home/dev-team. We get this error: This is because, others don't have any access to the folder dev-team. If we recall, below are the rights of the dev-team folder. Wrapping upPermissions and ownerships are useful concepts for enforcing security across multi-user operating systems. I hope you were able to learn about changing permissions and ownerships in depth. What’s your favorite thing you learned from this tutorial? Let me know on Twitter! You can also read my other posts here. Thanks to Tom Mondloch for his help with the guided exercise. Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started What command will change the current directory to your home directory?cd ~ : this command is used to change directory to the home directory.
Which command is used to change directory to another directory?The cd command, also known as chdir (change directory), is a command-line shell command used to change the current working directory in various operating systems.
How do I change my working directory to my home directory?If we type cd followed by nothing, cd will change the working directory to our home directory. A related shortcut is to type cd ~user_name . In this case, cd will change the working directory to the home directory of the specified user. Typing cd - changes the working directory to the previous one.
What Linux command can be used to change the directory from current to home directory?The cd (“change directory”) command is used to change the current working directory in Linux and other Unix-like operating systems. It is one of the most basic and frequently used commands when working on the Linux terminal.
|