Linux

常见Linux操作:https://www.cnblogs.com/cainiao-chuanqi/p/13372581.html

VMware下NAT无法分配网址问题  https://www.cnblogs.com/cainiao-chuanqi/p/13155985.html

软件包安装  https://www.cnblogs.com/cainiao-chuanqi/p/13373169.html

命令

    • cd /home 进入 '/ home' 目录'
    • cd .. 返回上一级目录
    • cd ../.. 返回上两级目录
    • cd 进入个人的主目录
    • cd ~user1 进入个人的主目录
    • cd - 返回上次所在的目录
    • pwd 显示工作路径
    • ls 查看目录中的文件
    • ls -F 查看目录中的文件
    • ls -l 显示文件和目录的详细资料
    • ls -a 显示隐藏文件
    • ls *[0-9]* 显示包含数字的文件名和目录名
    • tree 显示文件和目录由根目录开始的树形结构(1)
    • lstree 显示文件和目录由根目录开始的树形结构(2)
    • mkdir dir1 创建一个叫做 'dir1' 的目录'
    • mkdir dir1 dir2 同时创建两个目录
    • mkdir -p /tmp/dir1/dir2 创建一个目录树
    • rm -f file1 删除一个叫做 'file1' 的文件'
    • rmdir dir1 删除一个叫做 'dir1' 的目录'
    • rm -rf dir1 删除一个叫做 'dir1' 的目录并同时删除其内容
    • rm -rf dir1 dir2 同时删除两个目录及它们的内容
    • mv dir1 new_dir 重命名/移动 一个目录
    • cp file1 file2 复制一个文件
    • cp dir/* . 复制一个目录下的所有文件到当前工作目录
    • cp -a /tmp/dir1 . 复制一个目录到当前工作目录
    • cp -a dir1 dir2 复制一个目录
    • ln -s file1 lnk1 创建一个指向文件或目录的软链接

chmod:修改文件权限

三种身份(user,group,others)各自的三个权限(r,w,x)

使用八进制修改

chmod [OPTION]... OCTAL-MODE FILE...

 -R 递归修改权限

[root@localhost ~]# chmod -R 700 /tmp/test
[root@localhost ~]# ls -ld /tmp/test
drwx------ 2 root root 4096 Apr 2 09:41 /tmp/test
[root@localhost ~]# ll /tmp/test
total 0
-rwx------ 1 root root 0 Apr 2 09:41 abc

 使用八进制数修改文件或目录权限

[root@localhost ~]# chmod 640 /tmp/fstab
[root@localhost ~]# ll /tmp/fstab
-rw-r----- 1 root root 621 Mar 29 19:56 /tmp/fstab

使用MODE修改

 chmod [OPTION]... MODE[,MODE]... FILE...

MODE:
修改一类或多类用户的所有权限,若没有任何权限,则留空,a表示所有三类用户

1
2
3
4
5
6
u= 属主
g= 属组
o= 其他
ug= 属主、属组
a= 所有用户
u= ,g= 多个用户指定权限 使用逗号隔开
复制代码
[root@localhost ~]# chmod u=rwx /etc/fstab
[root@localhost ~]# ll /etc/fstab
-rwxr--r-- 1 root root 621 Mar 23 15:04 /etc/fstab

[root@localhost ~]# chmod g=r /etc/fstab
[root@localhost ~]# ll /etc/fstab
-rwxr--r-- 1 root root 621 Mar 23 15:04 /etc/fstab

[root@localhost ~]# chmod o= /etc/fstab
[root@localhost ~]# ll /etc/fstab
-rwxr----- 1 root root 621 Mar 23 15:04 /etc/fstab

[root@localhost ~]# chmod ug=rwx /etc/fstab
[root@localhost ~]# ll /etc/fstab
-rwxrwx--- 1 root root 621 Mar 23 15:04 /etc/fstab

[root@localhost ~]# chmod u=rwx,g=r /etc/fstab
[root@localhost ~]# ll /etc/fstab
-rwxr----- 1 root root 621 Mar 23 15:04 /etc/fstab

[root@localhost ~]# chmod a=rwx /etc/fstab
[root@localhost ~]# ll /etc/fstab
-rwxrwxrwx 1 root root 621 Mar 23 15:04 /etc/fstab
复制代码

修改一类用户某位或某些权限

1
2
u+: +表示增加权限
u-: -表示移除权限
复制代码
[root@localhost ~]# chmod u+r /tmp/fstab
[root@localhost ~]# ll /tmp/fstab
-rw-r--r-- 1 root root 621 Mar 29 19:56 /tmp/fstab

[root@localhost ~]# chmod u-r /tmp/fstab
[root@localhost ~]# ll /tmp/fstab
--w-r--r-- 1 root root 621 Mar 29 19:56 /tmp/fstab
复制代码

参考RFILE修改

chmod [OPTION].. --reference=RFILE FILE...

 参考RFILE文件的权限,修改FILE的权限和RFILE一样

复制代码
[root@localhost ~]# ll /tmp/fstab
-rw-r--r-- 1 root root 0 6月   2 16:57 /tmp/fstab
[root@localhost ~]# ll /tmp/issue
-r--r--r-- 1 root root 0 6月   2 16:58 /tmp/issue
[root@localhost ~]# chmod --reference=/tmp/issue /tmp/fstab
[root@localhost ~]# ll /tmp/{issue,fstab}
-r--r--r-- 1 root root 0 6月   2 16:57 /tmp/fstab
-r--r--r-- 1 root root 0 6月   2 16:58 /tmp/issue
复制代码

4.修改文件的属主,属组

仅root有使用权限    

chown:修改文件的属主

chown [OPTION]...[OWNER][:[GROUP]] FILE... 
1
2
3
4
5
6
7
8
9
10
用法:
 OWNER: 修改文件属主
 
 OWNER:GROUP: 修改文件属主、属组
 
 :GROUP: 修改文件属组
 
   注意:命令中的 : 可用 . 替换
 
      -R 递归修改文件属主、属组
复制代码
[root@localhost ~]# ll /tmp/x
总用量 0
-rwxr--r-- 1 root root 0 6月   2 16:06 a.txt
-rw-r--r-- 1 root root 0 6月   2 16:19 d
[root@localhost ~]# chown -R user2.user2 /tmp/x
[root@localhost ~]# ll /tmp/x
总用量 0
-rwxr--r-- 1 user2 user2 0 6月   2 16:06 a.txt
-rw-r--r-- 1 user2 user2 0 6月   2 16:19 d
复制代码
chown [option]... --reference=rfile file... 参考rfile的属主属组,修改file的属主属组 
[root@localhost ~]# chown --reference=/tmp/issue /tmp/fstab
[root@localhost ~]# ll /tmp/{issue,fstab}
--w-r--r-- 1 tom root 621 Mar 29 19:56 /tmp/fstab
-rw-r--r-- 1 tom root 74 Mar 29 22:38 /tmp/issue

chgrp: 修改文件的属组

chgrp [OPTION]... GROUP FILE...
[root@localhost ~]# chgrp tim /tmp/fstab
[root@localhost ~]# ll /tmp/fstab
--w-r--r-- 1 tom tim 621 Mar 29 19:56 /tmp/fstab
chgrp [option]... --reference=rfile file... 参考rfile的属组,修改file的属组
[root@localhost ~]# chgrp --reference=/tmp/fstab /tmp/issue
[root@localhost ~]# ll /tmp/{fstab,issue}
--w-r--r-- 1 tom tim 621 Mar 29 19:56 /tmp/fstab
-rw-r--r-- 1 tom tim 74 Mar 29 22:38 /tmp/issue

 -R 递归修改目录文件的属组

[root@localhost ~]# chgrp -R centos /tmp/test
[root@localhost ~]# ll -d /tmp/test
drwx------ 2 tom centos 4096 Apr 2 09:41 /tmp/test
[root@localhost ~]# ll /tmp/test
total 0
-rwx------ 1 tom centos 0 Apr 2 09:41 abc

5.umask: 文件或目录创建的遮罩码

创建文件: 666-umask

创建文件的默认权限

[root@localhost ~]# umask
0022   
[root@localhost ~]# touch /tmp/test.txt
[root@localhost ~]# ll /tmp/test.txt
-rw-r--r-- 1 root root 0 Apr 2 10:21 /tmp/test.txt
*这里的默认umask不管第一位,为022,减后为755,这说明存在x权限,将权限加一,则为133,所以为644,-rw-r--r--*

注意:如果某类的用户的权限减得的结果中存在x执行权限,则将其权限+1

[root@localhost ~]# umask 023
[root@localhost ~]# touch /tmp/test1.txt
[root@localhost ~]# ll /tmp/test1.txt
-rw-r--r-- 1 root root 0 Apr 2 10:23 /tmp/test1.txt

6.创建目录: 777-umask

 创建目录的默认权限

[root@localhost ~]# umask
0022
[root@localhost ~]# mkdir /tmp/test
[root@localhost ~]# ll -d /tmp/test
drwxr-xr-x 2 root root 4096 Apr 2 10:27 /tmp/test

umask 查看umask

  [root@localhost ~]# umask
  0022

umask # 设定umask

  [root@localhost ~]# umask 023
  [root@localhost ~]# umask
  0023
复制代码
用法:useradd [选项] 登录      useradd -D      useradd -D [选项]
选项:  -b, --base-dir BASE_DIR
新账户的主目录的基目录  -c, --comment COMMENT         
新账户的 GECOS 字段  -d, --home-dir HOME_DIR       
新账户的主目录  -D, --defaults        
显示或更改默认的 useradd 配置 -e, --expiredate EXPIRE_DATE  新账户的过期日期  -f, --inactive INACTIVE       
新账户的密码不活动期  -g, --gid GROUP        
新账户主组的名称或 ID  -G, --groups GROUPS    
新账户的附加组列表  -h, --help                    
显示此帮助信息并推出  -k, --skel SKEL_DIR    
使用此目录作为骨架目录  -K, --key KEY=VALUE          
不使用 /etc/login.defs 中的默认值  -l, --no-log-init    
不要将此用户添加到最近登录和登录失败数据库  -m, --create-home    
创建用户的主目录  -M, --no-create-home        
不创建用户的主目录  -N, --no-user-group    
不创建同名的组  -o, --non-unique        
允许使用重复的 UID 创建用户  -p, --password PASSWORD        
加密后的新账户密码  -r, --system                  
创建一个系统账户  -R, --root CHROOT_DIR         
chroot 到的目录  -s, --shell SHELL        
新账户的登录 shell  -u, --uid UID            
新账户的用户 ID  -U, --user-group        
创建与用户同名的组  -Z, --selinux-user SEUSER        
为 SELinux 用户映射使用指定 SEUSER 
复制代码
作者:拾瑾
个性签名:愿历经千帆,归来仍少年.
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文地址:https://www.cnblogs.com/ayoung/p/14306153.html