0% found this document useful (0 votes)
13 views

Permissions Exercises (1)

fklrl

Uploaded by

moussakhanfri04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Permissions Exercises (1)

fklrl

Uploaded by

moussakhanfri04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Activity 1: changing file permissions

$ cd
$ echo “test file” > working.txt
$ chmod 444 working.txt

What are the permissions for our new file?

$ ls -lh working.txt

In spite of the fact that the file does not have write permission for the owner, the owner can still
change the file’s permissions so that they can make it possible to write to it.

$ chmod 644 working.txt

Or, you can do this by using this form of chmod:

$ chmod u+w working.txt

To remove the read permission for the user on a file you would do

$ chmod u-r working.txt

Or, you can do something like:

$ chmod 344 working.txt

Try reading your file:

$ cat working.txt

What happened? You can’t read your file. Make the file again readable by you by completing the
following command (replace ???)

$ chmod ??? working.txt

Activity 2: give group access to a file


First create a new group and add your account to the created group

$ sudo groupadd team

Check that it really exists:

$ grep team /etc/group


Checks the groups you belong to:

$ groups

You can see that user_name is a member of the groups:

user_name adm dialout cdrom floppy sudo audio dip video plugdev netdev

Let’s add our user to the team group - the ‘-a’ is important!

$ sudo usermod -a -G team user_name

You won’t be able to use your new group until you have logged in and out from your account, or
have simulated this process by doing this:

$ su - user_name

(type your own password)

Now try typing:

$ groups

You should see something like this:

user_name adm dialout cdrom floppy sudo audio dip video plugdev netdev team

user_name is now a member of the team1 group.

Now we will give group access to a file

Do the following:

$ cd
$ echo “This is our group test file” > group.txt
$ chgrp team group.txt

What permissions does the file have now?

$ ls -l group.txt

You should see something like:

-rw-r--r-- 1 user_name team 28 2021-04-16 01:32 group.txt

How would you give members of the group team read/write access to this file? Before you look
below try solving this on your own.
We’ll use the numeric chmod functionality.

$ chmod 664 group.txt

Alternatively, you could use symbolic functionality by typing:

$ chmod g+w group.txt

Look at the file’s permissions:

$ ls -l group.txt

You should see something like:

-rw-rw-r-- 1 user_name team 28 2021-04-16 01:32 group.txt

Activity 3: make a file executable


Create a new fine in your home directory

$ cd
$ touch hello

Type the following command to edit your file

$ echo "echo 'Hello, world'" > hello

NOTE: you can use file editors to edit the file. Besides, you can add any other sequence of
commands to this file.

Let’s try to run this file:

$ ./hello

You’ll probably see something like:

bash: ./hello: Permission denied

This implies that the file is not executable. We need to set the file’s permission to be executable
by our user. How would you do this?

$ chmod 755 hello

would work. Now try running the file:

$ ./hello
On your screen, you should see …

Hello, world

…which is the result of running hello file

Congratulations: you’ve just written your first script!

Now set your hello file to be readable by everyone, NOT executable by you, and executable by
the Group and by Other.

Look at the file’s permissions to get started:

$ ls -l hello

-rwxr-xr-x 1 user_name user_name 20 2021-04-16 01:38 hello

You want the permission to be:

-rw-r-xr-x 1 user_name user_name 20 2021-04-16 01:38 hello

There are several ways you can do this with the chmod command.

Once you have set the permissions like this, what happens if you now type?

$ ./hello

Why does this happen? If you execute the file as a different user it will still work! Does this seem
odd? (Hint: think “left to right”)

You can get the file to execute, for example, by typing:

$ sudo ./hello

Now set the file back so that you can execute it. Verify that this works.

You might also like