Linux 基本命令;chmod用于改变文件或目录的访问权限

linux下命令和文件及目录名都分大小写
//移动文件
mv .*.swp 123.bak

//看性能
top //shift键+"m"是按"memory"排序

//查看环境变量
env

//浏览文件和目录
ls//列表; ll//详细列表

//查看当前目录
pwd

//创建目录
mkdir
//用内存创建目录
mkdir -p /tmp/cache
mount -t tmpfs -o size=1512M,noexec,noatime,noexec none /tmp/cache


装载自:http://download.csdn.net/source/1847100




chmod
chmod用于改变文件或目录的访问权限,由所有者使用。该命令有两种用法:

  1.用包含字母和操作符表达式的文字设定法

  其语法格式为:chmod [who] [opt] [mode] 文件/目录名

  其中who表示对象,是以下字母中的一个或组合:

u:表示文件所有者
g:表示同组用户
o:表示其它用户
a:表示所有用户

opt则是代表操作,可以为:
+:添加某个权限
-:取消某个权限
=:赋予给定的权限,并取消原有的权限
而mode则代表权限:
r:可读
w:可写
x:可执行

  例如:增加对所有者和其他用户对名为shm的文件的读写权
   执行chmod前,先看一下ls -l shm的结果:
  -rw-r-xr--   1 exp   staff         18607 jun 02 17:21 shm

   执行chmod uo+rw shm后,则ls -l shm为:
   -rwxr-xr-x   1 exp     staff         18607 jun 02 17:21 shm

  例如:为同组用户增加对文件a.txt的读写权限:

  chmod g+rw a.txt

  2.用数字设定法

  chmod [mode] 文件名

  关键是mode的取值,将rwx看成二进制数,如果有则用1表示,没有则用0表示,那么rwx r-x r- -则可以表示成为:

  111 101 100

  再将其每三位转换成为一个十进制数,就是754。

  例如,我们想让a.txt这个文件的权限为:

  自己 同组用户 其他用户
  可读   是     是   是
  可写   是     是   可执行

  那么,我们先根据上表得到权限串为:rw-rw-r--,那么转换成二进制数就是110 110 100,再每三位转换成为一个十进制数,就得到664,因此我 们执行命令:

  chmod 664 a.txt

利用数字更改权限可以实现一次为ugo等多个组群分配不同的权限。
chmod a=rwx file

chmod 777 file
效果相同
chmod ug=rwx,o=x file

chmod 771 file
效果相同
注意:在对目录操作时,如果想要开放某个目录让其他人进来时,一定要对该目录的赋予 x 属性。
对任何人对bak目录赋所有操作权限,
chmod 777 bak
drwxrwxrwx   2 exp     staff          4096 may 28 10:34 bak

如果对所有者和同者用户赋rwx权限,对其他用户赋rx权限,则为
chmod u+rwx,g+rwx,o+rx-w bak
这时bak目录的详细信息为
drwxrwxr-x   2 exp     staff          4096 may 28 10:34 bak

chown

chown将指定文件的拥有者改为指定的用户或组。

使用权限 : root

使用方式 : chmod [-cfhvr] [--help] [--version] user[:group] file...

说明 : linux/unix 是多人多工作业系统,所有的档案皆有拥有者。利用 chown 可以将档案的拥

有者加以改变。一般来说,这个指令只有是由系统管理者(root)所使用,一般使用者没有权限可以

改变别人的档案拥有者,也没有权限可以自己的档案拥有者改设为别人。只有系统管理者(root) 才

有这样的权限。

user : 新的档案拥有者的使用者 idgroup : 新的档案拥有者的使用者群体 (group)-c : 若该档案

拥有者确实已经更改,才显示其更改动作-f : 若该档案拥有者无法被更改也不要显示错误讯息-h :

只对于连结(link) 进行变更,而非该 link 真正指向的档案-v : 显示拥有者变更的详细资料-r : 对目

前目录下的所有档案与子目录进行相同的拥有者变更(即以递回的方式逐个变更)--help : 显示辅助

说明--version : 显示版本


-rw------- (600) -- 只有属主有读写权限。

-rw-r--r-- (644) -- 只有属主有读写权限;而属组用户和其他用户只有读权限。

-rwx------ (700) -- 只有属主有读、写、执行权限。

-rwxr-xr-x (755) -- 属主有读、写、执行权限;而属组用户和其他用户只有读、执行权限。

-rwx--x--x (711) -- 属主有读、写、执行权限;而属组用户和其他用户只有执行权限。

-rw-rw-rw- (666) -- 所有用户都有文件读、写权限。这种做法不可取。

-rwxrwxrwx (777) -- 所有用户都有读、写、执行权限。更不可取的做法。

以下是对目录的两个普通设定:

drwx------ (700) - 只有属主可在目录中读、写。

drwxr-xr-x (755) - 所有用户可读该目录,但只有属主才能改变目录中的内容

suid的代表数字是4,比如4755的结果是-rwsr-xr-x

sgid的代表数字是2,比如6755的结果是-rwsr-sr-x

sticky位代表数字是1,比如7755的结果是-rwsr-sr-t

(当然7755这个chmod设置没多大意义,这里只是演示一下)


chown命令将 file 参数指定的文件的所有者更改为 owner 参数指定的用户。owner 参数的值可以是可在 /etc/passwd 文件中找到的用户标识或登录名。还可以选择性地指定组。group 参数的值可以是可在 /etc/group 文件中找到的组标识或组名。

只有 root 用户可以更改文件的所有者。只在您是 root 用户或拥有该文件的情况下才可以更改文件的组。如果拥有文件但不是 root 用户,则只可以将组更改为您是其成员的组。

虽然 -h、-l 和 -p 标志是互斥的,指定不止一个也不认为是错误。指定的最后一个标志确定命令拟稿将演示的操作。

标志

-f  禁止除用法消息之外的所有错误消息。
# -h  更改遇到的符号链接的所有权,而非符号链接指向的文件或目录的所有权。 chmod命令允许或拒绝其他用户访问 program.c。 要将目录 /tmp/src 中所有文件的所有者和组更改为用户 john 和组 build:

chown -r john:build /tmp/src

文件

/usr/bin/chown  chown命令
/etc/group  包含组标识的文件
/etc/passwd  包含用户标识的文件
示例
 将档案 file1.txt 的拥有者设为 users 群体的使用者 jessie
chown jessie:users file1.txt    
将目前目录下的所有档案与子目录的拥有者皆设为 users 群体的使用者 lamport
chown -r lamport:users *   

例如root用户把自己的一个文件 拷贝给用户xu,为了让用户xu能够存取这个文件,root用户应该把这个文件的属主设为xu,否则,用户xu无法存取这个文件。  

用户可以是用户名或用户i d。组可以是组名或组id。文件是以空格分开的要改变权限的文件列表,支持通配符。 该命令的各选项含义如下:   
-r 递归式地改变指定目录及其下的所有子目录和文件的拥有者。   
-v 显示chown命令所做的工作。   

下面是在aix中给出的说明
chown command

purpose

changes the owner or group associated with a file.

syntax

chown [ -f ] [ -h ] [ -r ] owner [ :group ] { file ... | directory ...
}

chown -r  [ -f ] [ -h | -l | -p ] owner [ :group ] { file ... |
directory ... }
description

the chown command changes the owner of the file or directory specified
by the file or directory parameter to the user specified by the owner
parameter. the value of the owner parameter can be a user name from the
user database or a numeric user id. optionally, a group can also be
specified. the value of the group parameter can be a group name from
the group database or a numeric group id.

only the root user can change the owner of a file. you can change the
group of a file only if you are a root user or if you own the file. if
you own the file but are not a root user, you can change the group only
to a group of which you are a member.

although the -h, -l and -p flags are mutually exclusive, specifying
more than one is not considered an error. the last flag specified
determines the behavior that the command will exhibit.
when a symbolic link is encountered and you have not specified the -h
flag, the chown command changes the ownership of the file or directory
pointed to by the link and not the ownership of the link itself.

if you specify the -h flag, the chown command has the opposite effect
and changes the ownership of the link itself and not that of the file
or directory pointed to by the link.

if you specify the -r flag, the chown command recursively descends the
specified directories.

if you specify both the -h flag and the -r flag, the chown command
descends the specified directories recursively, and when a symbolic
link is encountered, the ownership of the link itself is changed and
not that of the file or directory pointed to by the link.
flags

-f
suppresses all error messages except usage messages.
-h
changes the ownership of an encountered symbolic link and not that
of the file or directory pointed to by the symbolic link.
-h

if the -r option is specified and a symbolic link referencing a
file of type directory is specified on the command line, the chown
command shall change the user id (and group id, if specified) of
the directory referenced by the symbolic link and all files in the
file hierarchy below it.
-l
if the -r option is specified and a symbolic link referencing a
file of type directory is specified on the command line or
encountered during the traversal of a file hierarchy, the chown
command shall change the user id (and group id, if specified) of
the directory referenced by the symbolic link and all files in the
file hierarchy below it.        -p
if the -r option is specified and a symbolic link is specified on
the command line or encountered during the traversal of a file
hierarchy, the chown command shall change the owner id (and group
id, if specified) of the symbolic link if the system supports this
operation. the chown command shall not follow the symbolic link to
any other part of the file hierarchy.
-r
descends directories recursively, changing the ownership for each
file. when a symbolic link is encountered and the link points to a
directory, the ownership of that directory is changed but the
directory is not further transversed. if the -h, -h, -l or -p
flags are not also specified, when a symbolic link is encountered
and the link points to a directory, the group ownership of that
directory is changed but the directory is not traversed further.
examples
1    to change the owner of the file program.c:

chown jim program.c

the user access permissions for program.c now apply to jim. as the
owner, jim can use the chmod command to permit or deny other users
access to program.c.
2    to change the owner and group of all files in the directory
/tmp/src to owner john and group build:

chown -r john:build /tmp/src



所有用户均可读、可写但不能执行的权:
chmod 666 a
文件的所属用户均变更为user:
chown user a
文件的属组变更为user:
chgrp user a

(文件的属组和所属用户均变更为user的命令: chown user:user a)

reference
zhidao.baidu.com/question/1954081.html
blog.csdn.net/warden2010/archive/2010/03/25/5417059.aspx
baike.baidu.com/view/1229019.htm
转载请注明出处:代码王国:http://code.jiaosha.org/question.aspx?url=explorewen/blog/item/916a2dd31f035e39970a16fa.html

装载自:http://code.jiaosha.org/question.aspx?url=explorewen/blog/item/916a2dd31f035e39970a16fa.html

猜你喜欢

转载自black-test.iteye.com/blog/812036