How to Set Permission-based access on Linux

Search for a command to run...

No comments yet. Be the first to comment.
Share files from Linux to Windows

Are you tired of waiting forever for large files to download on Linux? The terminal has some powerful tools that can help you maximize your bandwidth.

In Linux, an alias is a command that executes another command or series of commands using a different name. Aliases are typically used to make it easier to run frequently used or complex commands by typing a shorter, easier-to-remember version. The a...

This blog post explores the benefits of microservices in building a scalable and maintainable application.

Convert sheng to shembeteng using nodejs.

In the previous article, we created a new user and enabled ssh key access to the user. This guide covers how to restrict access using permissions.
To log in as root, retrieve the root password from Vultr on the specific instance.

On your terminal, log in as root using.
ssh root@ip_address

To demonstrate how permissions work on Linux, we create a testfolder in the new user's directory while logged in as root, then change permissions for the user in that folder.
Navigate to the new user home directory
cd /home/wamaitha
Create the testfolder
mkdir testfolder

Navigate to the testfolder, create a text file hello.txt, and populate the txt file with "Hello from root".
# cd into folder
cd testfolder
# create file and populate with echo
echo "Hello from root!" > hello.txt
# view file content of the hello.txt using cat
cat hello.txt

Check folder permissions as shown below
ls -l

The permissions are sectioned into 4; file type, user permissions, group permissions, and other user permissions. The permissions are a combination of read, write and execute. No permissions are denoted by a dash(-),
[file type][user][group][other users]

Root's permissions on the testfolder are
drwxr-xr-x
We want to add the user wamaitha to this directory and give them read and write permissions. By default, a new user is created and added to a group of the same name. Check this using the group's command
groups wamaitha

Permissions on Linux are given using the chmod command followed by the permission number.
Permission numbers are:
0 = --- (no permissions at all)
1 = --x (execute permission only)
2 = -w- (write permission only)
3 = -wx (write and execute permission only)
4 = r- (read permission only)
5 = r-x (read and execute only)
6 = rw- (read and write permission only)
7 = rwx (read,write and execute permissions)
With this, we can combine these numbers to change permissions. Chmod accepts three numbers. The first number represents the user's permission. The second represents the group permissions, and the third represents permission for all other users.
chmod 744 testfolder
For succinctness, the command above will yield:
Execute the command and check the permissions again.

Switch to the new user. If prompted for a password, use the password you used when creating the new user. The new user only has read permissions on the test folder. Thus any execution commands will be denied due to insufficient permissions.
su wamaitha

Resources