UNIX chmod

chmod

 

To set/modify a file's permissions you need to use the  chmod program. Of course, only the owner of a file may use  chmod to alter a file's permissions.  chmodhas the following syntax:  chmod [options] mode file(s)

 

The 'mode' part specifies the new permissions for the file(s) that follow as arguments. A mode specifies which user's permissions should be changed, and afterwards which access types should be changed. Let's say for example: chmod a-x socktest.plThis means that the e xecute bit should be cleared (-) for  all users. (owner, group and the rest of the world) The permissions start with a letter specifying what users should be affected by the change, this might be any of the following:
  • u the owner user
  • g the owner group
  • o others (neither u, nor g)
  • a all users
This is followed by a change instruction which consists of a  +(set bit) or  -(clear bit) and the letter corresponding to the bit that should be changed.

 

Let's see some examples: $ ls -l socktest.pl -rwxr-xr-x 1 nick users 1874 Jan 19 10:23 socktest.pl* $ chmod a-x socktest.pl $ ls -l socktest.pl -rw-r--r-- 1 nick users 1874 Jan 19 10:23 socktest.pl $ chmod g+w socktest.pl $ ls -l socktest.pl -rw-rw-r-- 1 nick users 1874 Jan 19 10:23 socktest.pl $ chmod ug+x socktest.pl $ ls -l socktest.pl -rwxrwxr-- 1 nick users 1874 Jan 19 10:23 socktest.pl* $ chmod ug-wx socktest.pl $ ls -l socktest.pl -r--r--r-- 1 nick users 1874 Jan 19 10:23 socktest.pl

 

Strange numbers...

 

You might have encountered things like  chmod 755 somefile and of course you will be wondering what this is. The thing is, that you can change the entire permission pattern of a file in one go using one number like the one in this example. Every mode has a corresponding code number, and as we shall see there is a very simple way to figure out what number corresponds to any mode.

 

Every one of the three digits on the mode number corresponds to one of the three permission triplets. (u, g and o) Every permission bit in a triplet corresponds to a value: 4 for r, 2 for w, 1 for x. If the permission bit you add this value to the number of the permission triplet. If it is cleared, then you add nothing. (Some of you might notice that in fact, the number for a triplet is the  octal value corresponding to the three-bit pattern - if you don't know what an octal value is, it doesn't really matter, just follow the intstructions) So if a file has  rwxr-xr-x permissions we do the following calculation:

 

Triplet for u: rwx => 4 + 2 + 1 =  7
Triplet for g: r-x => 4 + 0 + 1 =  5
Tripler for o: r-x => 4 + 0 + 1 =  5
Which makes : 755

 

So, 755 is a terse way to say  'I don't mind if other people read or run this file, but only I should be able to modify it' and 777 means  'everyone has full access to this file'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值