Linux 下经常使用命令集锦--待续

一、查看系统版本及位数linux

系统版本
方法一:
#lsb_release -a
--该命令适用于全部的linux,包括Redhat、SuSE、Debian等发行版
方法二:
#cat /etc/xxx-release 
---XX为发行版名称。如 centos-release
方法三:
#cat /etc/issue
shell


位数
方法一:
#uname -r
方法二:
#more /proc/version

编程

二、修改文件归属用户和组centos

1、基本知识函数

  在Linux中,建立一个文件时,该文件的拥有者都是建立该文件的用户。该文件用户能够修改该文件的拥有者及用户组,固然root用户能够修改任何文件的拥有者及用户组。在Linux中,对于文件的权限(rwx),分为三部分,一部分是该文件的拥有者所拥有的权限,一部分是该文件所在用户组的用户所拥有的权限,另一部分是其余用户所拥有的权限。对于文件的权限请参考《Linux的chmod命令ui

   文件(含文件夹,下同)的权限,在shell中能够经过chmod命令来完成,关于此请参考Linux的chmod命令在 shell 中,能够使用chown命令来改变文件全部者及用户组,chgrp命令来改变文件所在用户组。在 Linux的C程序中,能够使用chown函数来改变文件全部者,所在用户组。spa

  另外,在shell中,要修改文件当前的用户必须具备管理员root的权限。能够经过su命令切换到root用户,也能够经过sudo得到root的权限。code

2、使用chown命令更改文件拥有者blog

在 shell 中,能够使用chown命令来改变文件全部者。chown命令是change owner(改变拥有者)的缩写。须要要注意的是,用户必须是已经存在系统中的,也就是只能改变为在 /etc/passwd这个文件中有记录的用户名称才能够递归

chown命令的用途不少,还能够顺便直接修改用户组的名称。此外,若是要连目录下的全部子目录或文件同时更改文件拥有者的话,直接加上 -R的参数便可。

基本语法:

chown [-R] 帐号名称 文件目录

chown [-R帐号名称:用户组名称 文件目录

参数

-R : 进行递归( recursive )的持续更改,即连同子目录下的全部文件、目录

都更新成为这个用户组。经常用在更改某一目录的状况。

示例1

[root@localhost home]# touch testfile //由 root 用户建立文件 

[root@localhost home]# ls testfile –l 

-rw--w--w- 1 root root 0 Jun 7 19:35 testfile //文件的拥有者及拥有者级均为 root 

[root@localhost home]# chown yangzongde testfile //修改文件拥有者为 yangzongde 

[root@localhost home]# ls testfile -l 

-rw--w--w- 1 yangzongde root 0 Jun 7 19:35 testfile //查看文件拥有者为 yangzongde,但组仍为 root 

示例2

chown bin install.log

ls -l

-rw-r--r--  1 bin  users 68495 Jun 25 08:53 install.log

chown root:root install.log

ls -l

-rw-r--r--  1 root root 68495 Jun 25 08:53 install.log

3、使用chgrp命令更改文件所属用户组

在shell中,能够使用chgrp命令来改变文件所属用户组,该命令就是change group(改变用户组)的缩写。须要注意的是要改变成为的用户组名称,必须在 /etc/group里存在,不然就会显示错误。

基本语法:

chgrp [-R用户组名称 dirname/filename ...

参数:

-R : 进行递归( recursive )的持续更改,即连同子目录下的全部文件、目录

都更新成为这个用户组。经常用在更改某一目录的状况。

示例3

[root@localhost home]# ls testfile -l 

-rw--w--w- 1 yangzongde root 0 Jun 7 19:35 testfile //查看文件拥有者为 yangzongde,但组为 root 

[root@localhost home]# chgrp yangzongde testfile //修改拥有者组为 yangzongde 

[root@localhost home]# ls testfile -l 

-rw--w--w- 1 yangzongde yangzongde 0 Jun 7 19:35 testfile 

[root@localhost home]# chown root:root testfile // 使用 chown 一次性修改拥有者及组 

[root@localhost home]# ls testfile -l 

-rw--w--w- 1 root root 0 Jun 7 19:35 testfile 

示例4

[root@linux ~]# chgrp users install.log

[root@linux ~]# ls -l

-rw-r--r--  1 root users 68495 Jun 25 08:53 install.log

示例5

更改成一个 /etc/group不存在的用户组

[root@linux ~]# chgrp testing install.log

chgrp: invalid group name `testing' <== 出现错误信息~找不到这个用户组名~

4、chown 函数的使用

在Linux 的C 应用编程中,能够使用 chown 函数来修改文件的拥有者及拥有者组。此函数声明以下: 

/usr/include/unistd.h文件中

/* Change the owner and group of FILE. */ extern int chown (__const char *__file, __uid_t __owner, __gid_t __group)__THROW __nonnull ((1)) __wur;

此函数的第一个参数为欲修改用户的文件,第二个参数为修改后的文件拥有者,第三个参数为修改后该文件拥有者所在的组。


对于已打开的文件,使用 fchown 函数来修改。其第一个参数为已打开文件的文件描述符,其余同 chown 函数。该函数声明以下: 

/* Change the owner and group of the file that FD is open on. */ extern int fchown (int __fd, __uid_t __owner, __gid_t __group) __THROW __wur;

对于链接文件,则能够使用 lchown 函数。其参数同于 chown 函数。 

/* Change owner and group of FILE, if it is a symbolic link the ownership of the symbolic link is changed. */ extern int lchown (__const char *__file, __uid_t __owner, __gid_t __group) __THROW __nonnull ((1)) __wur;

以上这 3 个函数若是执行成功,将返回 0,不然返回-1。

相关文章
相关标签/搜索