用户及文件目录命令

问题一、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。

常用的文件管理类命令:cp、 mv、 rm、file、ls、touch、more、less、head、tail、stat

1、cp命令:

cp--copy的简写,复制文件和目录的命令;

命令格式:

cp [OPTION]... [-T] SOURCE DEST----单源复制

如果DEST不存在,直接创建一个文件,复制源文件的数据流至DEST;

例如,复制/etc/fstab文件到/tmp目录下,如果/tmp没有一个叫fstab的文件,就先创建一个fstab文件,然后将fstab的数据流复制到/tmp的fstab中:

[root@magedu ~]# cp /etc/fstab /tmp/

[root@magedu ~]# ll /tmp/fstab

-rw-r--r-- 1 root root 465 Mar 25 16:51 /tmp/fstab

如果DEST是已存在的非目录文件,则覆盖;

例如,再复制/etc/fstab文件到/tmp目录下,就会覆盖第一次复制的文件,从时间上可看出来:

[root@magedu ~]# ll /tmp/fstab

-rw-r--r-- 1 root root 465 Mar 25 16:51 /tmp/fstab

[root@magedu ~]# cp /etc/fstab /tmp/fstab

cp: overwrite ‘/tmp/fstab’? y

[root@magedu ~]# ll /tmp/fstab

-rw-r--r-- 1 root root 465 Mar 25 16:58 /tmp/fstab

如果DEST是目录文件,则先在DEST目录下创建一个源文件同名的文件,并复制其流。

例如:

[root@magedu ~]# cp /etc/ntp.conf /tmp/

[root@magedu ~]# ll /tmp/ntp.conf

-rw-r--r-- 1 root root 2000 Mar 29 20:49 /tmp/ntp.conf

cp [OPTION]... SOURCE... DIRECTORY---->多源复制

cp [OPTION]... -t DIRECTORY SOURCE... ---->多源复制

[root@magedu ~]# cp /etc/yum.conf /etc/vimrc /tmp

[root@magedu ~]# ll /tmp/yum.conf /tmp/vimrc

-rw-r--r-- 1 root root 1993 Mar 29 20:53 /tmp/vimrc

-rw-r--r-- 1 root root  970 Mar 29 20:53 /tmp/yum.conf

OPTIONS:

-i:交互式复制,覆盖之前询问

[root@magedu ~]# cp -i /etc/ntp.conf /tmp

cp: overwrite ‘/tmp/ntp.conf’? y

[root@magedu ~]# ls /tmp/ntp.conf

/tmp/ntp.conf

-f:强行覆盖,覆盖时不询问

[root@magedu ~]# cp -f /etc/ntp.conf /tmp

[root@magedu ~]#

-r:递归复制目录

[root@magedu ~]# cp -r /etc/yum.repos.d/ /tmp/yum

[root@magedu ~]# ll /tmp/yum

total 0

drwxr-xr-x 2 root root 187 Mar 29 21:08 yum.repos.d

[root@magedu ~]# tree /tmp/yum

/tmp/yum

`-- yum.repos.d

​    |-- CentOS-Base.repo

​    |-- CentOS-CR.repo

​    |-- CentOS-Debuginfo.repo

​    |-- CentOS-fasttrack.repo

​    |-- CentOS-Media.repo

​    |-- CentOS-Sources.repo

​    `-- CentOS-Vault.repo

1 directory, 7 files

-a:复制源文件的所有属性,实现归档,一般用于备份;

-rw-------. 1 root   root        107 Mar 27 13:37 yum.log

[root@magedu ~]# cp -a /var/log/yum.log /tmp

[root@magedu ~]# ll /tmp/yum.log

-rw-------. 1 root root 107 Mar 27 13:37 /tmp/yum.log

\#可看到,yum.log 在源文件中的元数据复制到tmp目录下后,都没变化;
2、mv命令:

​ ---移动或重命名文件;

移动目录时,直接跟目录名,不需要加R选项,也就是移动文件和目录是一样的。

[root@magedu ~]# mv /tmp/yum/ ./

[root@magedu ~]# ll yum

total 0

drwxr-xr-x 2 root root 187 Mar 29 21:08 yum.repos.d

OPTIONS:

-i:交互式,移动覆盖前询问

[root@magedu ~]# mv -i /tmp/yum/yum.repos.d/CentOS-Base.repo ./yum/yum.repos.d/CentOS-Base.repo

mv: overwrite ‘./yum/yum.repos.d/CentOS-Base.repo’? y

[root@magedu ~]#

-f:force 强制覆盖;

[root@magedu ~]# mv -f /tmp/yum/yum.repos.d/CentOS-Sources.repo ./yum/yum.repos.d/

[root@magedu ~]#
3、rm命令:

NAME

​ rm-remove files or directories --删除文件或目录

SYNOPSIS

​ rm [OPTION] ... FILE...

OPTIONS

-r:删除目录

[root@magedu ~]# rm -r /tmp/yum/yum.repos.d/

rm: descend into directory ‘/tmp/yum/yum.repos.d/’? y

rm: remove regular file ‘/tmp/yum/yum.repos.d/CentOS-CR.repo’? y

rm: remove regular file ‘/tmp/yum/yum.repos.d/CentOS-Debuginfo.repo’? y

rm: remove regular file ‘/tmp/yum/yum.repos.d/CentOS-Media.repo’? y

rm: remove regular file ‘/tmp/yum/yum.repos.d/CentOS-Vault.repo’? y

rm: remove regular file ‘/tmp/yum/yum.repos.d/CentOS-fasttrack.repo’? y

rm: remove directory ‘/tmp/yum/yum.repos.d/’? y

[root@magedu ~]#

-f:强制删除

[root@magedu ~]# rm -rf ./yum/

[root@magedu ~]#
4、file命令:

----查看文件类型命令

[root@magedu ~]# file /etc/passwd

/etc/passwd: ASCII text

[root@magedu ~]# file /etc/

/etc/: directory

[root@magedu ~]# file /dev/tty44

/dev/tty44: character special
5、ls 命令

​ ----list directory contents 列出列表,

SYNOPSIS

​ ls [OPTION]...[FILE]

OPTIONS

-a:列出所有文件,包括隐藏文件

[root@magedu ~]# ls -a /var/log

.                   lastlog            secure-20190317

..                  libvirt            secure-20190324

anaconda            maillog            speech-dispatcher

audit               maillog-20190303   spooler

boot.log            maillog-20190310   spooler-20190303

boot.log-20190228   maillog-20190317   spooler-20190310

boot.log-20190306   maillog-20190324   spooler-20190

-A:显示除.和..的所有文件

[root@magedu ~]# ls -A /var/log

anaconda            libvirt            secure-20190317

audit               maillog            secure-20190324

boot.log            maillog-20190303   speech-dispatcher

boot.log-20190228   maillog-20190310   spooler

boot.log-20190306   maillog-20190317   spooler-20190303

-l:long 长格式列表,显示文件详细属性信息,相当于ll;

[root@magedu ~]# ls -l /usr

total 256

dr-xr-xr-x.   2 root root 49152 Mar 27 13:37 bin

drwxr-xr-x.   2 root root     6 Apr 11  2018 etc

drwxr-xr-x.   2 root root     6 Apr 11  2018 games

drwxr-xr-x.   9 root root  4096 Oct 16 10:29 include

dr-xr-xr-x.  41 root root  4096 Oct 16 10:29 lib

dr-xr-xr-x. 142 root root 77824 Oct 16 10:31 lib64

-h:对文件大小单位换算,一般和-l一块使用

[root@magedu ~]# ls -lh /usr

total 256K

dr-xr-xr-x.   2 root root  48K Mar 27 13:37 bin

drwxr-xr-x.   2 root root    6 Apr 11  2018 etc

drwxr-xr-x.   2 root root    6 Apr 11  2018 games

drwxr-xr-x.   9 root root 4.0K Oct 16 10:29 include

dr-xr-xr-x.  41 root root 4.0K Oct 16 10:29 lib

dr-xr-xr-x. 142 root root  76K Oct 16 10:31 lib64

drwxr-xr-x.  45 root root  12K Oct 16 10:29 libexec

drwxr-xr-x.  12 root root  131 Oct 16 10:25 local

dr-xr-xr-x.   2 root root  20K Oct 16 10:29 sbin

drwxr-xr-x. 231 root root 8.0K Oct 16 10:29 share

-d:查看指定目录的自身属性,一般和-l一块使用

[root@magedu ~]# ls -ld /etc/

drwxr-xr-x. 139 root root 8192 Mar 29 10:11 /etc/

-r:逆序显示

[root@magedu ~]# ll -r /etc

total 1420

drwxr-xr-x.  2 root root      187 Mar 28 15:45 yum.repos.d

-rw-r--r--.  1 root root      970 Apr 13  2018 yum.conf

drwxr-xr-x.  6 root root      100 Oct 16 10:26 yum

drwxr-xr-x.  2 root root       21 Oct 16 10:26 xml

drwxr-xr-x.  2 root root        6 Apr 11  2018 xinetd.d

drwxr-xr-x.  6 root root      116 Oct 16 10:28 xdg

drwxr-xr-x.  6 root root      103 Oct 16 10:26 X11

-rw-r--r--.  1 root root        0 Jun 10  2014 wvdial.conf

drwxr-xr-x.  2 root root       33 Oct 16 10:27 wpa_supplicant

-R:递归显示

[root@magedu ~]# ll -R /var/log

/var/log/samba:

total 0

drwx------. 2 root root 6 Apr 13  2018 old

/var/log/samba/old:

total 0

/var/log/speech-dispatcher:

total 0

/var/log/sssd:

total 0

/var/log/tuned:

total 8

-rw-r--r--. 1 root root 7159 Mar  5 09:24 tuned.log
6、touch 命令

NAME

touch - change file timestamps--修改文件的时间戳,可用于创建文件;

[root@magedu ~]# touch /tmp/touch.test

[root@magedu ~]# ll /tmp/touch.test

-rw-r--r-- 1 root root 0 Mar 29 22:06 /tmp/touch.test

SYNOPSIS

touch [OPTION]... FILE...

OPTIONS

-a:仅修改access time;

[root@magedu ~]# stat /tmp/touch.test

  File: ‘/tmp/touch.test’

  Size: 0             Blocks: 0          IO Block: 4096   regular empty file

Device: fd00h/64768d    Inode: 67976088    Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2019-03-29 22:09:05.609128074 +0800

Modify: 2019-03-29 22:09:05.609128074 +0800

Change: 2019-03-29 22:09:05.609128074 +0800

Birth: -

[root@magedu ~]# touch -a /tmp/touch.test

[root@magedu ~]# stat /tmp/touch.test

  File: ‘/tmp/touch.test’

  Size: 0             Blocks: 0          IO Block: 4096   regular empty file

Device: fd00h/64768d    Inode: 67976088    Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2019-03-29 22:09:25.882986471 +0800

Modify: 2019-03-29 22:09:05.609128074 +0800

Change: 2019-03-29 22:09:25.882986471 +0800

Birth: -

-m:仅修改modify time;

[root@magedu ~]# touch -m /tmp/touch.test

[root@magedu ~]# stat /tmp/touch.test

  File: ‘/tmp/touch.test’

  Size: 0             Blocks: 0          IO Block: 4096   regular empty file

Device: fd00h/64768d    Inode: 67976088    Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2019-03-29 22:09:25.882986471 +0800

Modify: 2019-03-29 22:11:12.518241679 +0800

Change: 2019-03-29 22:11:12.518241679 +0800

Birth: -

-t:STAMP,可指定时间戳,格式:[[CC]YY]MMDDhhmm[.ss]

[root@magedu ~]# touch -t 201903290830.33 /tmp/touch.test

[root@magedu ~]# stat /tmp/touch.test

  File: ‘/tmp/touch.test’

  Size: 0             Blocks: 0          IO Block: 4096   regular empty file

Device: fd00h/64768d    Inode: 67976088    Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2019-03-29 08:30:33.000000000 +0800

Modify: 2019-03-29 08:30:33.000000000 +0800

Change: 2019-03-29 22:14:02.791052405 +0800

Birth: -
7、more命令:分屏查看文件内容的命令

格式:

more FILENAME

[root@magedu ~]# more /etc/rc.d/init.d/functions

\# -*-Shell-script-*-

\#

\# functions This file contains functions to be used by most or all

\#       shell scripts in the /etc/init.d directory.

\#

TEXTDOMAIN=initscripts

\# Make sure umask is sane

umask 022

\# Set up a default search path.

PATH="/sbin:/usr/sbin:/bin:/usr/bin"

export PATH

if [ $PPID -ne 1 -a -z "$SYSTEMCTL_SKIP_REDIRECT" ] && \
8、less命令:分屏查看文件内容的命令

格式:

less FILENAME:

[root@magedu ~]# less /etc/rc.d/init.d/functions

注:more和less的区别是 more翻到文件的最后一行后,会自动退出;less而不会,man命令其实调用的是less命令;

10、head:查看文件头几行的命令,默认是前10行;

格式:head FILE

[root@magedu ~]# head /etc/rc.d/init.d/functions

\# -*-Shell-script-*-

\#

\# functions This file contains functions to be used by most or all

\#       shell scripts in the /etc/init.d directory.

\#

TEXTDOMAIN=initscripts

\# Make sure umask is sane

umask 022

OPTIONS

-n #: 指定查看#行;

[root@magedu ~]# head -n 5 /etc/rc.d/init.d/functions

\# -*-Shell-script-*-

\#

\# functions This file contains functions to be used by most or all

\#       shell scripts in the /etc/init.d directory.

\#
11、tail命令:

查看文件的后n行;

格式:

tail [option] ifle

[root@magedu ~]# tail /etc/fstab

\#

\# /etc/fstab

\# Created by anaconda on Tue Oct 16 10:22:23 2018

\#

\# Accessible filesystems, by reference, are maintained under '/dev/disk'

\# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

\#

/dev/mapper/centos-root /                       xfs     defaults        0 0

UUID=9a3bd9e0-b338-4cee-8a0c-ab1ad4d4a80f /boot                   xfs     defaults        0 0

/dev/mapper/centos-swap swap                    swap    defaults        0 0

[root@magedu ~]#

-n # :查看后#行;

[root@magedu ~]# tail -2 /etc/fstab

UUID=9a3bd9e0-b338-4cee-8a0c-ab1ad4d4a80f /boot                   xfs     defaults        0 0

/dev/mapper/centos-swap swap                    swap    defaults        0 0

[root@magedu ~]#

-f:查看完成后不退出,一般用于监控日志等;Ctrl+c退出

[root@magedu ~]# ll /var/log/secure-20190310

-rw------- 1 root root 4140 Mar  8 17:38 /var/log/secure-20190310

[root@magedu ~]# tail -f /var/log/secure-20190310

Mar  5 20:50:25 magedu sshd[10449]: Accepted password for root from 172.16.200.88 port 63255 ssh2

Mar  5 20:50:26 magedu sshd[10449]: pam_unix(sshd:session): session opened for user root by (uid=0)

Mar  5 21:30:46 magedu sshd[10449]: pam_unix(sshd:session): session closed for user root

Mar  6 17:25:58 magedu sshd[2027]: pam_unix(sshd:session): session closed for user root

Mar  7 09:36:33 magedu sshd[31722]: Accepted password for root from 172.16.0.249 port 10712 ssh2

Mar  7 09:36:33 magedu sshd[31722]: pam_unix(sshd:session): session opened for user root by (uid=0)

Mar  7 16:20:13 magedu sshd[18305]: Accepted password for root from 172.16.251.149 port 53805 ssh2

Mar  7 16:20:13 magedu sshd[18305]: pam_unix(sshd:session): session opened for user root by (uid=0)

Mar  7 20:00:39 magedu sshd[18305]: pam_unix(sshd:session): session closed for user root

Mar  8 17:38:08 magedu sshd[31722]: pam_unix(sshd:session): session closed for user root
12、stat命令:显示文件时间戳
[root@magedu ~]# stat /etc/passwd

File: ‘/etc/passwd’

Size: 2837 Blocks: 8 IO Block: 4096 regular file

Device: fd00h/64768d Inode: 67976084 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Access: 2019-03-30 10:11:22.003980561 +0800

Modify: 2019-03-29 10:11:22.332144545 +0800

Change: 2019-03-29 10:11:22.332144545 +0800

Birth: -

问题二、使用命令行展开功能,创建/tmp/a1, /tmp/a2, /tmp/a1/a, /tmp/a1/b,在/tmp目录下创建目录:x_y, x_z, q_y, q_z

[root@magedu ~]# mkdir -p /tmp/{a1/{a,b},a2}

[root@magedu ~]# tree /tmp/

/tmp/

|-- a1

|   |-- a

|   `-- b

|-- a2

[root@magedu ~]# mkdir -v  /tmp/{x,q}_{y,z}

mkdir: created directory ‘/tmp/x_y’

mkdir: created directory ‘/tmp/x_z’

mkdir: created directory ‘/tmp/q_y’

mkdir: created directory ‘/tmp/q_z’

问题三、文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息。

文件的元数据包括:文件类型、文件权限、文件被链接的次数 、属主、属组、文件大小、时间戳、文件名等;

可以使用 ls -l File_Name 命令查看,例如:

[root@magedu ~]# ll -rd /var/log

drwxr-xr-x. 19 root root 4096 Mar 24 03:10 /var/log

d--是文件类型,代表的是此文件是目录;还有其它的文件类型

​ -:代表的是文件

​ l:的意思是链接文件,相当于Windows中的快捷方式;

​ p:是管道;

​ b:块设备文件;

​ c:字符设备文件;

​ s:套接字文件;

rwxr-xr-x--是文件的权限规定;权限有三类:r--读,w--写,x--执行;分三组:属主、属组、其他,每组三位;-代表的是没有;例如:rwxr-xr-x前三位的rwx代表文件的属主有读写执行的权限。中间三位r-x代表文件属主的属组有读执行的权限;后三位r-x代表即不是属主也不是属组的其他用户权限是读和执行;

19--是代表这个文件被链接的次数;

root--是代表文件属主的名称;

root--是代表文件属主的属组的名称;

4096--是代表文件的大小;

Mar 24 03:10--是代表文件的最后修改的时间;

/var/log--是文件名;

可能通过 stat命令来查看文件的元数据:

例:

[root@magedu ~]# stat /var/log

  File: ‘/var/log’

  Size: 4096          Blocks: 8          IO Block: 4096   directory

Device: fd00h/64768d    Inode: 37059       Links: 19

Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2019-03-24 03:10:02.285254964 +0800

Modify: 2019-03-24 03:10:02.293254907 +0800

Change: 2019-03-24 03:10:02.293254907 +0800

Birth: -

时间戳的时间主要有三个:

访问:Access Time,访问的时间;

修改:Modify Time:修改文件数据的时间;

改变Change Time:元数据发生改变的时间;

如果要修改时间戳的时间,用touch命令:

​ touch -a File_Name 修改访问时间;

​ touch -m File_Name 修改修改时间;

问题四、在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名形如:tfile-2016-05-27-09-32-22。

[root@magedu ~]# touch /tmp/tfile-$(date +%Y-%m-%d-%H-%M-%S)

[root@magedu ~]# ll /tmp/tfile-2019-03-26-21-20-27

-rw-r--r-- 1 root root 0 Mar 26 21:20 /tmp/tfile-2019-03-26-21-20-27

问题五、复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中。

[root@magedu ~]# mkdir /tmp/mytest1

[root@magedu ~]# cp -r /etc/p*[^[:digit:]] /tmp/mytest1/

[root@magedu ~]# ll /tmp/mytest1/

total 48

drwxr-xr-x  2 root root 4096 Mar 26 21:34 pam.d

-rw-r--r--  1 root root 2526 Mar 26 21:34 passwd

-rw-r--r--  1 root root 2485 Mar 26 21:34 passwd-

-rw-r--r--  1 root root 1362 Mar 26 21:34 pbm2ppa.conf

-rw-r--r--  1 root root 2872 Mar 26 21:34 pinforc

drwxr-xr-x 10 root root  116 Mar 26 21:34 pki

drwxr-xr-x  2 root root   28 Mar 26 21:34 plymouth

drwxr-xr-x  5 root root   52 Mar 26 21:34 pm

-rw-r--r--  1 root root 6300 Mar 26 21:34 pnm2ppa.conf

drwxr-xr-x  2 root root    6 Mar 26 21:34 popt.d

drwxr-xr-x  2 root root  154 Mar 26 21:34 postfix

drwxr-xr-x  3 root root  219 Mar 26 21:34 ppp

drwxr-xr-x  2 root root  105 Mar 26 21:34 prelink.conf.d

-rw-r--r--  1 root root  233 Mar 26 21:34 printcap

-rw-r--r--  1 root root 1819 Mar 26 21:34 profile

drwxr-xr-x  2 root root 4096 Mar 26 21:34 profile.d

-rw-r--r--  1 root root 6545 Mar 26 21:34 protocols

drwxr-xr-x  2 root root   79 Mar 26 21:34 pulse

drwxr-xr-x  2 root root   23 Mar 26 21:34 purple

drwxr-xr-x  2 root root   35 Mar 26 21:34 python

问题六、创建用户tom,指定UID为5001,指定家目录为/tmp/tom, 指定shell为/bin/zsh, 指定基本组为tom,附加组为jack

[root@magedu ~]# groupadd jack

[root@magedu ~]# useradd -u 5001 -d /tmp/tom -s /bin/zsh -G jack tom

[root@magedu ~]# tail -1 /etc/passwd

tom:x:5001:5004::/tmp/tom:/bin/zsh

[root@magedu ~]# tail -2 /etc/group

jack:x:5003:tom

tom:x:5004:

[root@magedu ~]#

问题七、常用的用户以及目录管理命令有哪些,并演示命令以及用法。

用户管理类命令:

1、useradd:添加用户

SYNOPSIS

useradd [options] LOGIN_NAME

useradd后面直接跟上要创建的用户名,就可添加一个新用户;

例:

[root@magedu ~]# useradd shared

[root@magedu ~]#

OPTIONS

-u,--uid UID:指定UID,默认自动根据前一个用户名的ID往后增加;

[root@magedu ~]# useradd -u 1345 oneday

[root@magedu ~]# tail -1 /etc/passwd

oneday:x:1345:1345::/home/oneday:/bin/bash

-g,--gid GID:指定GID,指定的组ID必须提前创建;

[root@magedu ~]# useradd -g 1345 twoday

[root@magedu ~]# tail -1 /etc/passwd

twoday:x:5009:1345::/home/twoday:/bin/bash

-G,--groups GROUP1[,GROUP2,...[,GROUPN]]:添加附加组,可同时添加多个;

[root@magedu ~]# useradd --groups oneday,testbash,tom,centos liuyue

[root@magedu ~]# cat  /etc/group

centos:x:4004:liuyue

user1:x:4005:

jack:x:5003:tom

tom:x:5004:liuyue

bash:x:5005:

testbash:x:5006:liuyue

basher:x:5007:

nologin:x:5008:

gaohua:x:5009:

zhouhaicheng:x:5010:

shared:x:5011:

oneday:x:1345:liuyue

liuyue:x:5012:

-c,--comment COMMENT:添加用户的注释信息;

[root@magedu ~]# useradd -c "大管家" songhuizong

[root@magedu ~]# tail -1 /etc/passwd
songhuizong:x:5011:5013:大管家:/home/songhuizong:/bin/bash

-d,--home HOME_DIR--->指定家目录;

[root@magedu ~]# useradd -d /tmp/袁腾飞 yuantengfei

[root@magedu ~]# tail -1 /etc/passwd

yuantengfei:x:5012:5014::/tmp/袁腾飞:/bin/bash

-M,不创建主目录;

[root@magedu ~]# useradd -M weizheng

[root@magedu ~]# tail -1 /etc/passwd

weizheng:x:5013:5015::/home/weizheng:/bin/bash

[root@magedu ~]# cd /home/

[root@magedu home]# ll

total 0

drwx------   3 bash         bash          78 Mar 27 10:43 bash

drwx------   3 basher       basher        78 Mar 27 10:43 basher

drwx------   5 centos       centos       144 Mar 24 15:24 centos

drwx------   3 fedora       fedora        78 Mar 13 12:37 fedora

drwx------   3 gaohua       gaohua        78 Mar 29 10:11 gaohua

drwx------.  3         1000         1000  78 Apr 11  2018 guoxi

drwx------  15 lijianzhi    lijianzhi    316 Mar  5 15:31 lijianzhi

drwx------   3 liuyue       liuyue        78 Mar 30 22:09 liuyue

drwx------   3 nologin      nologin       78 Mar 27 10:44 nologin

drwx------   3 oneday       oneday        78 Mar 30 21:59 oneday

drwx------   3 shared       shared        78 Mar 30 21:54 shared

drwx------   3 slackware    slackware     78 Mar  5 11:50 slackware

drwx------   3 songhuizong  songhuizong   78 Mar 31 09:40 songhuizong

drwx------   3 testbash     testbash      78 Mar 27 10:43 testbash

drwx------   3 twoday       oneday        78 Mar 30 22:04 twoday

drwx------   3 user1        user1         78 Mar 26 12:01 user1

drwx------   3 user3        user3         78 Mar 22 17:18 user3

drwx------   3 zhouhaicheng zhouhaicheng  78 Mar 29 10:11 zhouhaicheng

[root@magedu home]#

-D :查看默认创建用户的参数,也可修改默认参数;默认配置文件----/etc/login.defs,

[root@magedu home]# useradd -D

GROUP=100

HOME=/home

INACTIVE=-1

EXPIRE=

SHELL=/bin/bash

SKEL=/etc/skel

CREATE_MAIL_SPOOL=yes

例如:默认shell 就改为 csh

[root@magedu ~]# useradd -D -s /bin/csh

[root@magedu ~]# useradd -D

GROUP=100

HOME=/home

INACTIVE=-1

EXPIRE=

SHELL=/bin/csh

SKEL=/etc/skel

CREATE_MAIL_SPOOL=yes
2、修改用户命令:usermod

NAME

usermod

​ - modify a user account,修改用户账户

SYNOPSIS

​ -usermod [options] LOGIN

OPTIONS

-u,--uid UID:修改用户的UID ;

[root@magedu home]# grep oneday /etc/passwd

oneday:x:1345:1345::/home/oneday:/bin/bash

[root@magedu home]# usermod -u 2345 oneday

[root@magedu home]# grep oneday /etc/passwd

oneday:x:2345:1345::/home/oneday:/bin/bash

-g,--gid GID:修改基本组ID,必须事先创建;

[root@magedu home]# grep yuantengfei /etc/passwd

yuantengfei:x:5012:5012::/tmp/袁腾飞:/bin/bash

[root@magedu home]# usermod -g 5015 yuantengfei

[root@magedu home]# grep yuantengfei /etc/passwd

yuantengfei:x:5012:5015::/tmp/袁腾飞:/bin/bash

[root@magedu home]#

-G:修改用户附加组,原先附加组会被覆盖;

-a;和-G一起使作,只追加附加组,原先的附加组不会被覆盖;

[root@magedu home]# grep liuyue /etc/group

centos:x:4004:liuyue

tom:x:5004:liuyue

testbash:x:5006:liuyue

oneday:x:1345:liuyue

liuyue:x:5012:

[root@magedu home]# usermod -aG oneday tom

[root@magedu home]# grep liuyue /etc/group

centos:x:4004:liuyue

tom:x:5004:liuyue

testbash:x:5006:liuyue

oneday:x:1345:liuyue,tom

liuyue:x:5012:

-c :修改用户注释信息;

-d:修改用户家目录;

-l:修改用户名;

-s:修改用户默认shell;

[root@magedu home]# grep "songhuizong" /etc/passwd

songhuizong:x:5011:5013:大管家:/home/songhuizong:/bin/bash

[root@magedu home]# usermod -c 大奸臣 -d /home/caijing -l caijing -s /sbin/nologin songhuizong

[root@magedu home]# grep "songhuizong" /etc/passwd

[root@magedu home]# grep "caijing" /etc/passwd

caijing:x:5011:5013:大奸臣:/home/caijing:/sbin/nologin

-r:创建系统用户

[root@magedu home]# useradd -r suse

[root@magedu home]# tail -1 /etc/passwd

suse:x:988:982::/home/suse:/bin/bash
3、删除用户命令:userdel

userdel

[root@magedu home]# userdel caijing

-r:删除用户时一并删除其家目录

[root@magedu home]# cd /home/

[root@magedu home]# ll

total 0

drwx------   3 bash         bash          78 Mar 27 10:43 bash

drwx------   3 basher       basher        78 Mar 27 10:43 basher

drwx------   5 centos       centos       144 Mar 24 15:24 centos

drwx------   3 fedora       fedora        78 Mar 13 12:37 fedora

drwx------   3 gaohua       gaohua        78 Mar 29 10:11 gaohua

drwx------.  3         1000         1000  78 Apr 11  2018 guoxi

drwx------  15 lijianzhi    lijianzhi    316 Mar  5 15:31 lijianzhi

drwx------   3 liuyue       liuyue        78 Mar 30 22:09 liuyue

drwx------   3 nologin      nologin       78 Mar 27 10:44 nologin

drwx------   3 oneday       oneday        78 Mar 30 21:59 oneday

drwx------   3 shared       shared        78 Mar 30 21:54 shared

drwx------   3 slackware    slackware     78 Mar  5 11:50 slackware

drwx------   3         5011 songhuizong   78 Mar 31 09:40 songhuizong

drwx------   3 testbash     testbash      78 Mar 27 10:43 testbash

drwx------   3 twoday       oneday        78 Mar 30 22:04 twoday

drwx------   3 user1        user1         78 Mar 26 12:01 user1

drwx------   3 user3        user3         78 Mar 22 17:18 user3

drwx------   3 zhouhaicheng zhouhaicheng  78 Mar 29 10:11 zhouhaicheng

[root@magedu home]# userdel -r twoday

[root@magedu home]# ll

total 0

drwx------   3 bash         bash          78 Mar 27 10:43 bash

drwx------   3 basher       basher        78 Mar 27 10:43 basher

drwx------   5 centos       centos       144 Mar 24 15:24 centos

drwx------   3 fedora       fedora        78 Mar 13 12:37 fedora

drwx------   3 gaohua       gaohua        78 Mar 29 10:11 gaohua

drwx------.  3         1000         1000  78 Apr 11  2018 guoxi

drwx------  15 lijianzhi    lijianzhi    316 Mar  5 15:31 lijianzhi

drwx------   3 liuyue       liuyue        78 Mar 30 22:09 liuyue

drwx------   3 nologin      nologin       78 Mar 27 10:44 nologin

drwx------   3 oneday       oneday        78 Mar 30 21:59 oneday

drwx------   3 shared       shared        78 Mar 30 21:54 shared

drwx------   3 slackware    slackware     78 Mar  5 11:50 slackware

drwx------   3         5011 songhuizong   78 Mar 31 09:40 songhuizong

drwx------   3 testbash     testbash      78 Mar 27 10:43 testbash

drwx------   3 user1        user1         78 Mar 26 12:01 user1

drwx------   3 user3        user3         78 Mar 22 17:18 user3

drwx------   3 zhouhaicheng zhouhaicheng  78 Mar 29 10:11 zhouhaicheng

[root@magedu home]#
4、修改用用户密码:passwd

NAME

passwd

SYNOPSIS

passwd [--stdin] [USERNAME]

修改当前用户名密码

passwd:

[root@magedu home]# passwd

Changing password for user root.

New password:

Retype new password:

passwd: all authentication tokens updated successfully.

[root@magedu home]#

修改其他用户的密码,只有root用户才能修改其他用户密码

passwd USERNAME

[root@magedu home]# passwd oneday

Changing password for user oneday.

New password:

BAD PASSWORD: The password is shorter than 8 characters

Retype new password:

passwd: all authentication tokens updated successfully.

指定一个字符串输入给用户当密码

echo "PASSWORD" | passwd --stdin USERNAME

[root@magedu home]# echo "gentoo" | passwd --stdin gentoo

Changing password for user gentoo.

passwd: all authentication tokens updated successfully.
5、添加用户组命令:groupadd

NAME

groupadd

SYNOPSIS

groupadd [option] GROUP_NAME

OPTIONS

-g GID :指定组ID;

[root@magedu home]# groupadd -g 2510 tongguan

[root@magedu home]#
6、删除组命令:groupdel

groupdel

[root@magedu home]# groupdel tongguan

[root@magedu home]#
7、修改组命令:groupmod

groupmod

-g:修改组ID;

[root@magedu home]# grep "weizheng" /etc/group

weizheng:x:5015:

[root@magedu home]# groupmod -g 1555 weizheng

[root@magedu home]# grep "weizheng" /etc/group

weizheng:x:1555:

[root@magedu home]#

-n:修改组名

[root@magedu home]# grep "1555" /etc/group

weizheng:x:1555:

[root@magedu home]# groupmod -n caochao weizheng

[root@magedu home]# grep "1555" /etc/group

caochao:x:1555:
8、显示用户ID命令:id

NAME

id

SYNOPSIS

id [OPTION] ... [USERNAME]

-u:只显示UID

-g:只显示GID

-G:显示所有GID

[root@magedu home]# id -u weizheng

5013

[root@magedu home]# id -g weizheng

1555

[root@magedu home]# id -G liuyue

5012 4004 5004 5006 1345
9、切换用户命令

su

su USERNAME :非登录式切换,不会切换目标用户的配置文件进行初始化;

[root@magedu home]# su liuyue

您好,请你每天加油学习,欢迎入坑

[liuyue@magedu home]$

su - USERNAME:登录式切换,会重新读取用户的匹配文件初始化登陆信息;

[root@magedu home]# su - liuyue

Last login: Sun Mar 31 16:02:28 CST 2019 on pts/0

您好,请你每天加油学习,欢迎入坑

[liuyue@magedu ~]$
10、修改权限命令:chmod

NAME

chmod

SYNOPSIS

chmod [OPTION] MODE FILE

chmod --reference=FILE

OPTIONS

递归修改权限

-R,--recursive

[root@magedu tmp]# ll man

total 124

drwxr-xr-x 2 root root  53248 Mar  8 14:37 man1

drwxr-xr-x 2 root root 192512 Mar  8 14:37 man3

drwxr-xr-x 2 root root  16384 Mar  8 14:37 man5

drwxr-xr-x 2 root root    173 Mar  8 14:37 man7

drwxr-xr-x 2 root root  32768 Mar  8 14:37 man8

[root@magedu tmp]# chmod -R 774 man

[root@magedu tmp]# ll man

total 124

drwxrwxr-- 2 root root  53248 Mar  8 14:37 man1

drwxrwxr-- 2 root root 192512 Mar  8 14:37 man3

drwxrwxr-- 2 root root  16384 Mar  8 14:37 man5

drwxrwxr-- 2 root root    173 Mar  8 14:37 man7

drwxrwxr-- 2 root root  32768 Mar  8 14:37 man8

--reference=FILE

[root@magedu tmp]# ll man

total 124

drwxrwxr-- 2 root root  53248 Mar  8 14:37 man1

drwxrwxr-- 2 root root 192512 Mar  8 14:37 man3

drwxrwxr-- 2 root root  16384 Mar  8 14:37 man5

drwxrwxr-- 2 root root    173 Mar  8 14:37 man7

drwxrwxr-- 2 root root  32768 Mar  8 14:37 man8

[root@magedu tmp]# chmod -R --reference=/etc/fstab man

[root@magedu tmp]# ll man

total 124

drw-r--r-- 2 root root  53248 Mar  8 14:37 man1

drw-r--r-- 2 root root 192512 Mar  8 14:37 man3

drw-r--r-- 2 root root  16384 Mar  8 14:37 man5

drw-r--r-- 2 root root    173 Mar  8 14:37 man7

drw-r--r-- 2 root root  32768 Mar  8 14:37 man8
11、修改文件的用户和组:chown

chown

NAME

​ chown - change file owner and group

SYNOPSIS

​ chown [OPTION]... [OWNER][:[GROUP]] FILE...

​ chown [OPTION]... --reference=RFILE FILE...

OPTIONS

-R --recursive:递归修改用户或组

--reference=RFILE:以某个文件为基准,修改用户和组;

[root@magedu tmp]# ll man

total 124

drw-r--r-- 2 root root  53248 Mar  8 14:37 man1

drw-r--r-- 2 root root 192512 Mar  8 14:37 man3

drw-r--r-- 2 root root  16384 Mar  8 14:37 man5

drw-r--r-- 2 root root    173 Mar  8 14:37 man7

drw-r--r-- 2 root root  32768 Mar  8 14:37 man8

[root@magedu tmp]# man chown

[root@magedu tmp]# chown -R liuyue.liuyue /tmp/man

[root@magedu tmp]# ll man

total 124

drw-r--r-- 2 liuyue liuyue  53248 Mar  8 14:37 man1

drw-r--r-- 2 liuyue liuyue 192512 Mar  8 14:37 man3

drw-r--r-- 2 liuyue liuyue  16384 Mar  8 14:37 man5

drw-r--r-- 2 liuyue liuyue    173 Mar  8 14:37 man7

drw-r--r-- 2 liuyue liuyue  32768 Mar  8 14:37 man8

[root@magedu tmp]# ll man

total 124

drw-r--r-- 2 liuyue liuyue  53248 Mar  8 14:37 man1

drw-r--r-- 2 liuyue liuyue 192512 Mar  8 14:37 man3

drw-r--r-- 2 liuyue liuyue  16384 Mar  8 14:37 man5

drw-r--r-- 2 liuyue liuyue    173 Mar  8 14:37 man7

drw-r--r-- 2 liuyue liuyue  32768 Mar  8 14:37 man8

[root@magedu tmp]# chown -R --reference=/etc/passwd  /tmp/man

[root@magedu tmp]# ll man

total 124

drw-r--r-- 2 root root  53248 Mar  8 14:37 man1

drw-r--r-- 2 root root 192512 Mar  8 14:37 man3

drw-r--r-- 2 root root  16384 Mar  8 14:37 man5

drw-r--r-- 2 root root    173 Mar  8 14:37 man7

drw-r--r-- 2 root root  32768 Mar  8 14:37 man8
上一篇:Spring之SpringMVC的Controller(源码)分析


下一篇:【今日CV 计算机视觉论文速览】Tue, 26 Mar 2019