Modify permissions to -rwxr-xr-x [duplicate]

Solution 1:

Lets analyze your homework: -rwxr-xr-x, break them into three parts -rwx|r-x|r-x:

  1. rwx: read, write and execute for user or owner
  2. r-x: read, no write, and execute for group members
  3. r-x: same as in 2 but this time for every other person aka others.

No you will need to run the command using the change permission program chmod like so:

chmod 755 filename

Explained:

  • change the permission to:

    - user: 7 => r(4), w(2), x(1)
    - group: 5 => r(4), -, x(1)
    - others: r(4), -, x(1)
    

Now you have to own that file or folder to do this or be in the sudo group.

Solution 2:

There's also a more "talkative" version to set permissions:

 chmod u=rwx,g=rx,o=rx filename

This means:

  • give the user (u=) rwx permissions, and
  • give the group (g=) rx permissions, and
  • give others (o=) rx permissions.

You can leave out the parts that you do not want to change, e.g.:

 chmod u=rwx filename

will set the user's permissions and leave the others as they are.

You can even "add" or "substract" permissions:

 chmod g-x filename

will remove the group's x permission, and

chmod o+w filename

will add write permission for others.

To set SUID, SGID, and sticky bit use the s and t permissions:

  • chmod u+s filename -- sets SUID
  • chmod g+s filename -- sets SGID
  • chmod a+t filename -- sets sticky bit (a means "all", i.e. u and g and o)

Solution 3:

Obligatory Preamble

Modifying permissions of important files and directories in your system can break things badly. If you would find it inconvenient to have to reinstall your system or retrieve data from a live session, make sure you experiment with permissions and ownership safely. While you're learning your way around the filesystem and basic commands, you should avoid running chmod and chown with sudo or in a root shell. That way, a permission error will let you know that you might be doing something risky and you should check the command again.

We change file permissions with chmod. This command has a recursive flag -R. This flag is often used unnecessarily, and it is rarely useful, because we don't usually want directories to have the same permissions as the files inside them. Directories need execute permission to be entered, so aren't useful without it, while files only need execute permission if they are programs. Without the -R flag, chmod can do only limited and repairable damage, but with -R you can make a terrible mess of your system in a single command. If you are using -R, consider whether you really need it, and check the file path for typos.


The mode you have been asked to give the file is a common one

-rwxr-xr-x

The leading dash indicates that this is a regular file. The x bits in three places indicate that it needs to be executable by any user. So, this mode is appropriate for programs that anyone can execute. Most programs on your system have this mode, as you can see by examining the files in /usr/bin, /bin and so on (I explain how to examine permissions later in this answer). It's important for security that only the owner, root, can write to (modify) these files, so the programs you run can't be accidentally (or otherwise) changed by users without root privileges.

As pointed out in George Udosen's answer the permission bits are set separately for user (the owner of the file), group, and others. This is explained, for example, in our tag wiki for chmod and in many other places on the internet, and even on your system; you can read about it by running man chmod. You have been asked to make the file readable, writeable and executable by its owner, and readable and writeable for its group and others. You can see the owner and group of a file by running ls -ld file (the -d is only necessary if it's a directory; without that flag, ls shows the content of the directory rather than the directory itself) or if you want less irrelevant information, use stat:

stat -c '%U %G' file

-c means control the output. %U = file owner, %G = group. file should be replaced with the path to the file whose metadata you want to examine. If it's in the current directory, its name is sufficient, but you can give a path for files elsewhere:

$ stat -c '%U %G' /var/log/syslog
syslog adm

The stat command can also show file permissions in octal and in "human readable" form:

stat -c '%n %a %A' *

(I used %n to display the filenames, and * to run stat on every non-hidden file in the current directory.)

This provides a very useful reference if you ever forget what permission bits a particular octal number represents to chmod, though vidarlo's answer provides a nice way to recall them.

I strongly recommend examining permissions of any file you care about before you change them, since you won't have any way to "reset" the original mode except setting it manually.

Other answers have explained how to set the file mode you need in octal notation. chmod accepts file mode in symbolic notation as well as octal, and this is useful when you only want to modify one permission bit (one letter in that rwxr-xr-x string). Perl Duck's answer explains how to do this to set the mode you want.

When I create a script and I want to make it executable, which is the main reason I use chmod, I use symbolic notation to give myself execute permission and make no other changes, with this command:

chmod u+x file

If the script needs to be executable by all users, I would run that without u:

chmod +x file

This would probably be sufficient in your current situation, since files are often created by default with permissions that allow reading and writing by the owner and only readable for group and others, but it's also likely that they are created with write permission for the group. This depends on the umask setting a user has when they create the file. Let's create a file and take a look at it!

$ touch somefile
$ stat -c '%a %A' somefile 
664 -rw-rw-r--

OK. So to get mode 755 I need to remove the w bit from the g section, and add the x bit everywhere.

In symbolic notation, I can do that this way:

$ chmod +x,g-w somefile 
$ stat -c '%a %A' somefile 
755 -rwxr-xr-x

Another way to write +x in chmod is a+x. a stands for all, which is assumed if no letter is given. Important to note: u stands for user, which means the owner, while o stands for others (not owner).

You can also set all the bits for a u g or o with the = sign. Here's me playing around with useless permission settings:

$ chmod =x somefile 
$ stat -c '%a %A' somefile 
111 ---x--x--x
$ chmod u=r,g=rwx,o= somefile 
$ stat -c '%a %A' somefile 
470 -r--rwx---

You can use both octal and symbolic notation to add special bits like setuid (this allows running a program as its owner, so is dangerous) and setgid (this causes files created in a directory with setgid subsequently to inherit the directory's group ownership, which is useful because it allows you to control permissions for a group of users in a certain filesystem location without having to constantly fiddle with it).

Some folks like octal notation, some like symbolic. The latter is a little more flexible and maybe a little more intuitive for some. I recommend testing and learning both (safely!) so you'll be able to figure out what any chmod command will do when you see it, and solve any homework problem about it with flair :)

TL;DR

  • Be careful with chmod. Don't use sudo chmod unless you know what you're doing. Think carefully before using the -R flag.
  • Look at the existing permissions first. stat is your friend
  • Learn both symbolic and octal notation.

Solution 4:

The syntax for chmod command is :

chmod [options] {permissions} file-name

Before setting the file/folder permissions you need to be in the Parent Directory of the file/folder.

 r(read) - 4 
 w(write) - 2
 x(execute) - 1

Now, analyzing the set from your work: (-rwxr-xr-x)

Divide it into four parts as :

1. - File Type

2. rwx User

3. r-x Group

4. r-x Others

Thus, Finally to give permision to your file :

2. rwx : 4+2+1 = 7

3. r-x : 4+0+1 = 5

4. r-x : 4+0+1 = 5

Hence, The command is

  chmod 755 *filename*