1. 程式人生 > 實用技巧 >三、Linux 檔案管理與使用者組許可權

三、Linux 檔案管理與使用者組許可權

1、顯示/etc目錄下,以非字母開頭,後面跟了一個字母以及其它任意長度任意字元的檔案或目錄。

[root@localhost ~]#cd /etc/
[root@localhost /etc]#ls [^[:alpha:]][[:alpha:]]*
9a9b.txt

9a9b:

2、複製/etc目錄下所有以p開頭,以非數字結尾的檔案或目錄到/tmp/mytest1目錄中。

[root@localhost /etc]#cp -rv p*[^0-9] /tmp/mytest1/
‘pam.d’ -> ‘/tmp/mytest1/pam.d’
‘pam.d/config-util’ -> ‘/tmp/mytest1/pam.d/config-util’
‘pam.d/other’ -> ‘/tmp/mytest1/pam.d/other’
‘pam.d/chfn’ -> ‘/tmp/mytest1/pam.d/chfn’
‘pam.d/chsh’ -> ‘/tmp/mytest1/pam.d/chsh’
‘pam.d/login’ -> ‘/tmp/mytest1/pam.d/login’
‘pam.d/remote’ -> ‘/tmp/mytest1/pam.d/remote’
‘pam.d/runuser’ -> ‘/tmp/mytest1/pam.d/runuser’
‘pam.d/runuser-l’ -> ‘/tmp/mytest1/pam.d/runuser-l’
‘pam.d/su’ -> ‘/tmp/mytest1/pam.d/su’
‘pam.d/su-l’ -> ‘/tmp/mytest1/pam.d/su-l’
‘pam.d/systemd-user’ -> ‘/tmp/mytest1/pam.d/systemd-user’
‘pam.d/polkit-1’ -> ‘/tmp/mytest1/pam.d/polkit-1’
‘pam.d/crond’ -> ‘/tmp/mytest1/pam.d/crond’
‘pam.d/vlock’ -> ‘/tmp/mytest1/pam.d/vlock’
‘pam.d/vmtoolsd’ -> ‘/tmp/mytest1/pam.d/vmtoolsd’
‘pam.d/sshd’ -> ‘/tmp/mytest1/pam.d/sshd’
‘pam.d/smtp.postfix’ -> ‘/tmp/mytest1/pam.d/smtp.postfix’
‘pam.d/smtp’ -> ‘/tmp/mytest1/pam.d/smtp’
‘pam.d/sudo’ -> ‘/tmp/mytest1/pam.d/sudo’
‘pam.d/sudo-i’ -> ‘/tmp/mytest1/pam.d/sudo-i’
‘pam.d/passwd’ -> ‘/tmp/mytest1/pam.d/passwd’
‘pam.d/system-auth-ac’ -> ‘/tmp/mytest1/pam.d/system-auth-ac’
‘pam.d/system-auth’ -> ‘/tmp/mytest1/pam.d/system-auth’
‘pam.d/postlogin-ac’ -> ‘/tmp/mytest1/pam.d/postlogin-ac’
‘pam.d/postlogin’ -> ‘/tmp/mytest1/pam.d/postlogin’
‘pam.d/password-auth-ac’ -> ‘/tmp/mytest1/pam.d/password-auth-ac’
‘pam.d/password-auth’ -> ‘/tmp/mytest1/pam.d/password-auth’
‘pam.d/fingerprint-auth-ac’ -> ‘/tmp/mytest1/pam.d/fingerprint-auth-ac’
‘pam.d/fingerprint-auth’ -> ‘/tmp/mytest1/pam.d/fingerprint-auth’
‘pam.d/smartcard-auth-ac’ -> ‘/tmp/mytest1/pam.d/smartcard-auth-ac’
‘pam.d/smartcard-auth’ -> ‘/tmp/mytest1/pam.d/smartcard-auth’
‘pam.d/screen’ -> ‘/tmp/mytest1/pam.d/screen’
‘passwd’ -> ‘/tmp/mytest1/passwd’
‘passwd-’ -> ‘/tmp/mytest1/passwd-’
‘pki’ -> ‘/tmp/mytest1/pki’
‘pki/ca-trust’ -> ‘/tmp/mytest1/pki/ca-trust’
‘pki/ca-trust/README’ -> ‘/tmp/mytest1/pki/ca-trust/README’
‘pki/ca-trust/ca-legacy.conf’ -> ‘/tmp/mytest1/pki/ca-trust/ca-legacy.conf’
‘pki/ca-trust/extracted’ -> ‘/tmp/mytest1/pki/ca-trust/extracted’
‘pki/ca-trust/extracted/README’ -> ‘/tmp/mytest1/pki/ca-trust/extracted/README’
‘pki/ca-trust/extracted/java’ -> ‘/tmp/mytest1/pki/ca-trust/extracted/java’
‘pki/ca-trust/extracted/java/README’ -> ‘/tmp/mytest1/pki/ca-trust/extracted/java/README’
‘pki/ca-trust/extracted/java/cacerts’ -> ‘/tmp/mytest1/pki/ca-trust/extracted/java/cacerts’
‘pki/ca-trust/extracted/openssl’ -> ‘/tmp/mytest1/pki/ca-trust/extracted/openssl’
‘pki/ca-trust/extracted/openssl/README’ -> ‘/tmp/mytest1/pki/ca-trust/extracted/openssl/README’
‘pki/ca-trust/extracted/openssl/ca-bundle.trust.crt’ -> ‘/tmp/mytest1/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt’
‘pki/ca-trust/extracted/pem’ -> ‘/tmp/mytest1/pki/ca-trust/extracted/pem’
‘pki/ca-trust/extracted/pem/README’ -> ‘/tmp/mytest1/pki/ca-trust/extracted/pem/README’
‘pki/ca-trust/extracted/pem/tls-ca-bundle.pem’ -> ‘/tmp/mytest1/pki/ca-trust/extracted/pem/tls-ca-bundle.pem’
‘pki/ca-trust/extracted/pem/email-ca-bundle.pem’ -> ‘/tmp/mytest1/pki/ca-trust/extracted/pem/email-ca-bundle.pem’
‘pki/ca-trust/extracted/pem/objsign-ca-bundle.pem’ -> ‘/tmp/mytest1/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem’
‘pki/ca-trust/source’ -> ‘/tmp/mytest1/pki/ca-trust/source’
‘pki/ca-trust/source/README’ -> ‘/tmp/mytest1/pki/ca-trust/source/README’
‘pki/ca-trust/source/anchors’ -> ‘/tmp/mytest1/pki/ca-trust/source/anchors’
‘pki/ca-trust/source/blacklist’ -> ‘/tmp/mytest1/pki/ca-trust/source/blacklist’
‘pki/ca-trust/source/ca-bundle.legacy.crt’ -> ‘/tmp/mytest1/pki/ca-trust/source/ca-bundle.legacy.crt’
‘pki/java’ -> ‘/tmp/mytest1/pki/java’
‘pki/java/cacerts’ -> ‘/tmp/mytest1/pki/java/cacerts’
‘pki/tls’ -> ‘/tmp/mytest1/pki/tls’
‘pki/tls/cert.pem’ -> ‘/tmp/mytest1/pki/tls/cert.pem’
‘pki/tls/certs’ -> ‘/tmp/mytest1/pki/tls/certs’
‘pki/tls/certs/ca-bundle.crt’ -> ‘/tmp/mytest1/pki/tls/certs/ca-bundle.crt’
‘pki/tls/certs/ca-bundle.trust.crt’ -> ‘/tmp/mytest1/pki/tls/certs/ca-bundle.trust.crt’
‘pki/tls/certs/Makefile’ -> ‘/tmp/mytest1/pki/tls/certs/Makefile’
‘pki/tls/certs/make-dummy-cert’ -> ‘/tmp/mytest1/pki/tls/certs/make-dummy-cert’
‘pki/tls/certs/renew-dummy-cert’ -> ‘/tmp/mytest1/pki/tls/certs/renew-dummy-cert’
‘pki/tls/misc’ -> ‘/tmp/mytest1/pki/tls/misc’
‘pki/tls/misc/CA’ -> ‘/tmp/mytest1/pki/tls/misc/CA’
‘pki/tls/misc/c_hash’ -> ‘/tmp/mytest1/pki/tls/misc/c_hash’
‘pki/tls/misc/c_info’ -> ‘/tmp/mytest1/pki/tls/misc/c_info’
‘pki/tls/misc/c_issuer’ -> ‘/tmp/mytest1/pki/tls/misc/c_issuer’
‘pki/tls/misc/c_name’ -> ‘/tmp/mytest1/pki/tls/misc/c_name’
‘pki/tls/openssl.cnf’ -> ‘/tmp/mytest1/pki/tls/openssl.cnf’
‘pki/tls/private’ -> ‘/tmp/mytest1/pki/tls/private’
‘pki/rpm-gpg’ -> ‘/tmp/mytest1/pki/rpm-gpg’
‘pki/rpm-gpg/RPM-GPG-KEY-CentOS-7’ -> ‘/tmp/mytest1/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7’
‘pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7’ -> ‘/tmp/mytest1/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7’
‘pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7’ -> ‘/tmp/mytest1/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7’
‘pki/nss-legacy’ -> ‘/tmp/mytest1/pki/nss-legacy’
‘pki/nss-legacy/nss-rhel7.config’ -> ‘/tmp/mytest1/pki/nss-legacy/nss-rhel7.config’
‘pki/nssdb’ -> ‘/tmp/mytest1/pki/nssdb’
‘pki/nssdb/cert8.db’ -> ‘/tmp/mytest1/pki/nssdb/cert8.db’
‘pki/nssdb/cert9.db’ -> ‘/tmp/mytest1/pki/nssdb/cert9.db’
‘pki/nssdb/key3.db’ -> ‘/tmp/mytest1/pki/nssdb/key3.db’
‘pki/nssdb/key4.db’ -> ‘/tmp/mytest1/pki/nssdb/key4.db’
‘pki/nssdb/pkcs11.txt’ -> ‘/tmp/mytest1/pki/nssdb/pkcs11.txt’
‘pki/nssdb/secmod.db’ -> ‘/tmp/mytest1/pki/nssdb/secmod.db’
‘pki/CA’ -> ‘/tmp/mytest1/pki/CA’
‘pki/CA/certs’ -> ‘/tmp/mytest1/pki/CA/certs’
‘pki/CA/crl’ -> ‘/tmp/mytest1/pki/CA/crl’
‘pki/CA/newcerts’ -> ‘/tmp/mytest1/pki/CA/newcerts’
‘pki/CA/private’ -> ‘/tmp/mytest1/pki/CA/private’
‘pki/rsyslog’ -> ‘/tmp/mytest1/pki/rsyslog’
‘plymouth’ -> ‘/tmp/mytest1/plymouth’
‘plymouth/plymouthd.conf’ -> ‘/tmp/mytest1/plymouth/plymouthd.conf’
‘pm’ -> ‘/tmp/mytest1/pm’
‘pm/config.d’ -> ‘/tmp/mytest1/pm/config.d’
‘pm/power.d’ -> ‘/tmp/mytest1/pm/power.d’
‘pm/sleep.d’ -> ‘/tmp/mytest1/pm/sleep.d’
‘popt.d’ -> ‘/tmp/mytest1/popt.d’
‘postfix’ -> ‘/tmp/mytest1/postfix’
‘postfix/access’ -> ‘/tmp/mytest1/postfix/access’
‘postfix/canonical’ -> ‘/tmp/mytest1/postfix/canonical’
‘postfix/generic’ -> ‘/tmp/mytest1/postfix/generic’
‘postfix/header_checks’ -> ‘/tmp/mytest1/postfix/header_checks’
‘postfix/main.cf’ -> ‘/tmp/mytest1/postfix/main.cf’
‘postfix/master.cf’ -> ‘/tmp/mytest1/postfix/master.cf’
‘postfix/relocated’ -> ‘/tmp/mytest1/postfix/relocated’
‘postfix/transport’ -> ‘/tmp/mytest1/postfix/transport’
‘postfix/virtual’ -> ‘/tmp/mytest1/postfix/virtual’
‘ppp’ -> ‘/tmp/mytest1/ppp’
‘ppp/ip-down’ -> ‘/tmp/mytest1/ppp/ip-down’
‘ppp/ip-down.ipv6to4’ -> ‘/tmp/mytest1/ppp/ip-down.ipv6to4’
‘ppp/ip-up’ -> ‘/tmp/mytest1/ppp/ip-up’
‘ppp/ip-up.ipv6to4’ -> ‘/tmp/mytest1/ppp/ip-up.ipv6to4’
‘ppp/ipv6-down’ -> ‘/tmp/mytest1/ppp/ipv6-down’
‘ppp/ipv6-up’ -> ‘/tmp/mytest1/ppp/ipv6-up’
‘ppp/peers’ -> ‘/tmp/mytest1/ppp/peers’
‘prelink.conf.d’ -> ‘/tmp/mytest1/prelink.conf.d’
‘prelink.conf.d/nss-softokn-prelink.conf’ -> ‘/tmp/mytest1/prelink.conf.d/nss-softokn-prelink.conf’
‘prelink.conf.d/fipscheck.conf’ -> ‘/tmp/mytest1/prelink.conf.d/fipscheck.conf’
‘prelink.conf.d/grub2.conf’ -> ‘/tmp/mytest1/prelink.conf.d/grub2.conf’
‘printcap’ -> ‘/tmp/mytest1/printcap’
‘profile’ -> ‘/tmp/mytest1/profile’
‘profile.d’ -> ‘/tmp/mytest1/profile.d’
‘profile.d/csh.local’ -> ‘/tmp/mytest1/profile.d/csh.local’
‘profile.d/sh.local’ -> ‘/tmp/mytest1/profile.d/sh.local’
‘profile.d/colorgrep.csh’ -> ‘/tmp/mytest1/profile.d/colorgrep.csh’
‘profile.d/colorgrep.sh’ -> ‘/tmp/mytest1/profile.d/colorgrep.sh’
‘profile.d/colorls.csh’ -> ‘/tmp/mytest1/profile.d/colorls.csh’
‘profile.d/colorls.sh’ -> ‘/tmp/mytest1/profile.d/colorls.sh’
‘profile.d/which2.csh’ -> ‘/tmp/mytest1/profile.d/which2.csh’
‘profile.d/which2.sh’ -> ‘/tmp/mytest1/profile.d/which2.sh’
‘profile.d/less.csh’ -> ‘/tmp/mytest1/profile.d/less.csh’
‘profile.d/less.sh’ -> ‘/tmp/mytest1/profile.d/less.sh’
‘profile.d/256term.csh’ -> ‘/tmp/mytest1/profile.d/256term.csh’
‘profile.d/256term.sh’ -> ‘/tmp/mytest1/profile.d/256term.sh’
‘profile.d/lang.csh’ -> ‘/tmp/mytest1/profile.d/lang.csh’
‘profile.d/lang.sh’ -> ‘/tmp/mytest1/profile.d/lang.sh’
‘profile.d/vim.csh’ -> ‘/tmp/mytest1/profile.d/vim.csh’
‘profile.d/vim.sh’ -> ‘/tmp/mytest1/profile.d/vim.sh’
‘profile.d/path.sh’ -> ‘/tmp/mytest1/profile.d/path.sh’
‘profile.d/env.sh’ -> ‘/tmp/mytest1/profile.d/env.sh’
‘protocols’ -> ‘/tmp/mytest1/protocols’
‘python’ -> ‘/tmp/mytest1/python’
‘python/cert-verification.cfg’ -> ‘/tmp/mytest1/python/cert-verification.cfg’

3、將/etc/issue檔案中的內容轉換為大寫後儲存至/tmp/issue.out檔案中。

[root@localhost /etc]#cat issue
\S
Kernel \r on an \m
tty:\l
hostname:\n
time:\t
[root@localhost /etc]#tr a-z A-Z </etc/issue > /tmp/issue.out
[root@localhost /tmp]#cat issue.out 
\S
KERNEL \R ON AN \M
TTY:\L
HOSTNAME:\N
TIME:\T

4、請總結描述使用者和組管理類命令的使用方法並完成以下練習:

總結:

  • 使用者和組管理命令格式:命令 [options] LOGIN;
  • 使用者和組管理命令都有建立(add)、修改(mod)、刪除(del)三個模式;
  • 使用者和組的ID都是唯一的,不允許重複;
  • 單個使用者可以擁有多個屬組,單個組也可以擁有多個使用者;

(1)建立組distro,其GID為2019;

[root@localhost ~]#groupadd distro -g 2019
[root@localhost ~]#cat /etc/group|tail -1
distro:x:2019:

(2)建立使用者mandriva,其ID號為1005;基本組為distro;

[root@localhost ~]#groupadd distro
[root@localhost ~]#useradd mandriva -u 1005 -g distro
[root@localhost ~]#id mandriva
uid=1005(mandriva) gid=2020(distro) groups=2020(distro)

(3)建立使用者mageia.其ID號為1100,家目錄為/home/linux;

[root@localhost ~]#useradd mageia -u 1100 -d /home/linux
[root@localhost ~]#id mageia
uid=1100(mageia) gid=1100(mageia) groups=1100(mageia)
[root@localhost ~]#cat /etc/passwd |tail
varnish:x:1011:1012::/home/varnish:/bin/bash
mysql:x:1012:1013::/home/mysql:/sbin/nologin
docker:x:1013:1014::/home/docker:/bin/bash
zabbix:x:1016:1017::/home/zabbix:/bin/bash
tomcat:x:1017:1018::/home/tomcat:/bin/bash
git:x:1018:1020::/home/git:/bin/bash
abd:x:1020:1024::/home/abd:/bin/bash
abc:x:1021:1021::/home/abc:/bin/bash
mandrixa:x:1005:2020::/home/mandrixa:/bin/bash
mageia:x:1100:1100::/home/linux:/bin/bash

(4)給使用者mageia新增密碼,密碼為mageedu,並設定使用者密碼7天后過期;

[root@localhost ~]#echo mageedu | passwd --stdin mageia
Changing password for user mageia.
passwd: all authentication tokens updated successfully.
[root@localhost ~]#cat /etc/shadow |tail -1
mageia:$6$NkaUZvZW$TLSDTpERLjwTWuzEfnXiFWSZzqcRrqWOLxM28AfgORy5aOPn0swzHoqPQoTEqDpI4rVNgDPJhwm08v8xxKg7D/:18491:0:99999:7:::
[root@localhost ~]#chage -M 7 mageia
[root@localhost ~]#cat /etc/shadow |tail -1
mageia:$6$NkaUZvZW$TLSDTpERLjwTWuzEfnXiFWSZzqcRrqWOLxM28AfgORy5aOPn0swzHoqPQoTEqDpI4rVNgDPJhwm08v8xxKg7D/:18491:0:7:7:::

(5)刪除mandriva,但保留其家目錄;

[root@localhost ~]#ll /home/
total 0
drwx------. 2 abc      abc      62 Aug 12 17:35 abc
drwx------. 2 abd      abd      62 Aug 12 17:27 abd
drwx------. 2 bash     bash     62 Aug  5 14:17 bash
drwx------. 2 basher   basher   62 Aug  5 14:18 basher
drwx------. 2 docker   docker   83 Aug  6 13:55 docker
drwx------. 2 gentoo   gentoo   62 Aug  5 16:55 gentoo
drwx------. 2 git      git      62 Apr 11  2018 git
drwx------. 2 mageia   mageia   62 Aug 17 16:10 linux
drwx------. 2 mage     mage     62 Aug  5 15:06 mage
drwx------. 2 mandriva distrg   62 Aug 17 16:35 mandriva
drwx------. 2 mysql    mysql    62 Aug  5 17:18 mysql
drwx------. 2 nginx    nginx    62 Aug  5 17:15 nginx
drwx------. 2 nologin  nologin  62 Aug  5 14:21 nologin
drwx------. 2 rpc      rpc      62 Aug  4 17:52 rpc
drwx------. 2 testbash testbash 62 Aug  5 14:18 testbash
drwx------. 2 tomcat   tomcat   62 Aug  6 14:38 tomcat
drwx------. 2 varnish  varnish  62 Aug  5 17:15 varnish
drwx------. 2 wang     wang     62 Aug  5 15:06 wang
drwx------. 2 wensijia wensijia 83 Aug  3 17:17 wensijia
drwx------. 2 zabbix   zabbix   99 Aug  6 14:36 zabbix
[root@localhost ~]#userdel mandriva
[root@localhost ~]#ll /home/
total 0
drwx------. 2 abc      abc      62 Aug 12 17:35 abc
drwx------. 2 abd      abd      62 Aug 12 17:27 abd
drwx------. 2 bash     bash     62 Aug  5 14:17 bash
drwx------. 2 basher   basher   62 Aug  5 14:18 basher
drwx------. 2 docker   docker   83 Aug  6 13:55 docker
drwx------. 2 gentoo   gentoo   62 Aug  5 16:55 gentoo
drwx------. 2 git      git      62 Apr 11  2018 git
drwx------. 2 mageia   mageia   62 Aug 17 16:10 linux
drwx------. 2 mage     mage     62 Aug  5 15:06 mage
drwx------. 2     1005 distrg   62 Aug 17 16:35 mandriva
drwx------. 2 mysql    mysql    62 Aug  5 17:18 mysql
drwx------. 2 nginx    nginx    62 Aug  5 17:15 nginx
drwx------. 2 nologin  nologin  62 Aug  5 14:21 nologin
drwx------. 2 rpc      rpc      62 Aug  4 17:52 rpc
drwx------. 2 testbash testbash 62 Aug  5 14:18 testbash
drwx------. 2 tomcat   tomcat   62 Aug  6 14:38 tomcat
drwx------. 2 varnish  varnish  62 Aug  5 17:15 varnish
drwx------. 2 wang     wang     62 Aug  5 15:06 wang
drwx------. 2 wensijia wensijia 83 Aug  3 17:17 wensijia
drwx------. 2 zabbix   zabbix   99 Aug  6 14:36 zabbix

(6)建立使用者slackware,其ID號為2002,基本組為distro,附加組 peguin;

[root@localhost ~]#useradd slackware -u 2002 -g distro -G peguin
useradd: group 'peguin' does not exist
[root@localhost ~]#groupadd peguin
[root@localhost ~]#useradd slackware -u 2002 -g distro -G peguin
[root@localhost ~]#id slackware
uid=2002(slackware) gid=2021(distro) groups=2021(distro),2022(peguin)

(7)修改slackware的預設shell為/bin/tcsh;

[root@localhost ~]#usermod -s /bin/tcsh slackware
[root@localhost ~]#cat /etc/passwd |tail -1
slackware:x:2002:2021::/home/slackware:/bin/tcsh

(8)為使用者slackware,新增附加組admins;

[root@localhost ~]#groupadd admins
[root@localhost ~]#usermod -aG admins slackware
[root@localhost ~]#id slackware
uid=2002(slackware) gid=2021(distro) groups=2021(distro),2022(peguin),2023(admins)