linux-practice(1-19)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux-practice(1-19)相关的知识,希望对你有一定的参考价值。

1、了解GPL(v1、v2、v3)、LGPL、Apache、BSD、MIT等开源协定的具体内容。

答:

BSD:不允许用作者名字打广告,可商业化

Apache Licence2.0:注明修改处,可商业化

GPL:开源、免费。禁止商业化

LGPL:适合作为第三方类库,可商业化


2、使用echo如何显示“The year is 2017.\nToday is monday”为两行?

答:

[[email protected] ~]# echo -e "The year is 2017.\nToday is monday"

The year is 2017.

Today is monday


3、使用printf命令如何显示“ling little”为两行?

答:

[[email protected] ~]# printf "ling\nlittle\n"

ling

little


4、通过man获得帮助,创建目录(如有精力,可以尝试只使用一次mkdir命令解决此问题)

(1)在/mnt下创建boot和sysroot;

(2)在/mnt/boot下创建grub;

(3)在/mnt/sysroot下创建proc, sys, bin, sbin, lib, usr, var, etc, dev, home, root, tmp

a)在/mnt/sysroot/usr下创建bin, sbin, lib

b)在/mnt/sysroot/lib下创建modules

c)在/mnt/sysroot/var下创建run, log, lock

d)在/mnt/sysroot/etc下创建init.d

答:

[[email protected] ~]# mkdir -pv /mnt/{boot/grub,sysroot/{proc,sys,bin,sbin,lib/modules,lib64,usr/{bin,sbin/lib},var/{run,log,lock},etc/init.d,dev,home,root,tmp}}

mkdir: 已创建目录 "/mnt/boot"

mkdir: 已创建目录 "/mnt/boot/grub"

mkdir: 已创建目录 "/mnt/sysroot"

mkdir: 已创建目录 "/mnt/sysroot/proc"

mkdir: 已创建目录 "/mnt/sysroot/sys"

mkdir: 已创建目录 "/mnt/sysroot/bin"

mkdir: 已创建目录 "/mnt/sysroot/sbin"

mkdir: 已创建目录 "/mnt/sysroot/lib"

mkdir: 已创建目录 "/mnt/sysroot/lib/modules"

mkdir: 已创建目录 "/mnt/sysroot/lib64"

mkdir: 已创建目录 "/mnt/sysroot/usr"

mkdir: 已创建目录 "/mnt/sysroot/usr/bin"

mkdir: 已创建目录 "/mnt/sysroot/usr/sbin"

mkdir: 已创建目录 "/mnt/sysroot/usr/sbin/lib"

mkdir: 已创建目录 "/mnt/sysroot/var"

mkdir: 已创建目录 "/mnt/sysroot/var/run"

mkdir: 已创建目录 "/mnt/sysroot/var/log"

mkdir: 已创建目录 "/mnt/sysroot/var/lock"

mkdir: 已创建目录 "/mnt/sysroot/etc"

mkdir: 已创建目录 "/mnt/sysroot/etc/init.d"

mkdir: 已创建目录 "/mnt/sysroot/dev"

mkdir: 已创建目录 "/mnt/sysroot/home"

mkdir: 已创建目录 "/mnt/sysroot/root"

mkdir: 已创建目录 "/mnt/sysroot/tmp"


5、创建目录/backup

   复制目录/etc至/backup目录中,要求保留文件原来的属性,保持链接文件;

答:

[[email protected] ~]# mkdir -pv /backup

mkdir: 已创建目录 "/backup"

[[email protected] ~]# mv /etc /backup

[[email protected] ~]# echo $?

0

[[email protected] ~]# ls /backup/

etc


3、通过man帮助手册,自行总结which、whereis、whatis命令的使用方法;

答:

which:命令的别名

whereis:命令的路径

whatis:命令作用

[[email protected] ~]# which ls

alias ls=‘ls --color=auto‘

  /bin/ls

[[email protected] ~]# whereis ls

ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz


4、通过帮助手册,学习使用du命令;并思考:如何显示某个目录内部的所有文件的整体大小?

答:

du:显示目录大小

[[email protected] ~]# du 

12  ./test

36  ./apr-util-1.5.4/hooks/.libs

88  ./apr-util-1.5.4/hooks

88  ./apr-util-1.5.4/memcache/.libs

228 ./apr-util-1.5.4/memcache

……

[[email protected] ~]# du ./test

12  ./test


5、通过帮助手册,学习who、w、whoami命令,并对比who和w,思考其区别;

答:

who:登录用户

w:显示用户信息,更加详细

whoami:显示ID为0的用户信息


[[email protected] ~]# who 

little   tty1         2017-11-13 13:27 (:0)

root     pts/0        2017-11-13 13:29 (172.16.1.1)

[[email protected] ~]# w

 13:51:50 up 25 min,  2 users,  load average: 0.01, 0.01, 0.00

USER     TTY      FROM              [email protected]   IDLE   JCPU   PCPU WHAT

[[email protected] ~]# whoami

whoami:无法找到用户ID 为0 的用户名


6、显示/etc目录下,以字母开头,后面跟了一个非字母及其它任意长度任意字符的文件或目录;

答:

[[email protected] ~]# ls /etc | grep "^[[:alpha:]][^[:alpha:]].*"

X11

[[email protected] ~]# ls -d /etc/[[:alpha:]][^[:alpha:]]*

/etc/X11


7、显示/usr/share/man目录下,所有以man开头,后跟一个数字结尾的文件或目录;

答:

[[email protected] ~]# ls -d /usr/share/man/man[0-9]

/usr/share/man/man1  /usr/share/man/man4  /usr/share/man/man7

/usr/share/man/man2  /usr/share/man/man5  /usr/share/man/man8

/usr/share/man/man3  /usr/share/man/man6  /usr/share/man/man9

[[email protected] ~]# 

[[email protected] ~]# ls -d /usr/share/man/man[[:digit:]]

/usr/share/man/man1  /usr/share/man/man4  /usr/share/man/man7

/usr/share/man/man2  /usr/share/man/man5  /usr/share/man/man8

/usr/share/man/man3  /usr/share/man/man6  /usr/share/man/man9


8、复制/etc目录下,所以p,m,r开头的,且以.conf结尾的文件或目录至/tmp/conf.d目录下;

答:

[[email protected] ~]# ls /etc/[pmr]*.conf

/etc/mke2fs.conf                   /etc/pnm2ppa.conf    /etc/request-key.conf

/etc/mtools.conf                   /etc/prelink.conf    /etc/resolv.conf

/etc/pbm2ppa.conf                  /etc/readahead.conf  /etc/rsyslog.conf

/etc/pm-utils-hd-apm-restore.conf  /etc/reader.conf

[[email protected] ~]# 

[[email protected] ~]# cp -a /etc/[pmr]*.conf /tmp

[[email protected] ~]#  

[[email protected] ~]# ls /tmp

keyring-Hw528X  keyring-rjErX0                pnm2ppa.conf

keyring-KphV2t  keyring-sNGWZe                prelink.conf

keyring-NtnoIW  keyring-seupBP                pulse-CCOJSbg2rrUT

keyring-SD8DuM  keyring-va5YXr                pulse-kxE14xec0BgA

keyring-SWLHv8  keyring-w9r8Y2                pulse-nMz1VNv72M1x

keyring-Ta3gEf  keyring-yT4OZn                readahead.conf

keyring-akJZHb  mke2fs.conf                   reader.conf

keyring-dFQOvC  mtools.conf                   request-key.conf

keyring-g403YF  orbit-gdm                     resolv.conf

keyring-hosyK7  orbit-little                  rsyslog.conf

keyring-hpmuXh  pbm2ppa.conf                  virtual-little.QpKRpK

keyring-rEePKI  pm-utils-hd-apm-restore.conf  virtual-little.hPZ8F7


9、创建a123, cd6, c78m, c1 my, m.z, k 67, 8yu, 789等文件,并按照下述要求写出相应的命令;

  注意,以上文件是以逗号隔开的,其它符号都是文件名的组成部分;

  1) 显示所有以a或m开头的文件;

  2) 显示所有文件名中包含了数字的文件;

  3) 显示所有以数字结尾且文件名中包含空白字符的文件;

  4) 显示文件名中不以c字母开头并且不以数字结尾的所有文件;

答:

错误创建如下:(空格将其分开,产生错误)

[[email protected] ~]# touch {a123,cd6,c78m,c1 my,m.z,k 67,8yu,789}

[[email protected] ~]#  

[[email protected] ~]# ls 

2.txt            cdrom               //my,m.z,k           ?????????  ??????

3.txt            dest                t1file.txt         ??????     ??????

//67,8yu,789}      install.log         zhengshu           ??????

anaconda-ks.cfg  install.log.syslog  //{a123,cd6,c78m,c1  ??????

boot.iso         ks.cfg              ??????             ??????


正确创建如下:(处理空格,注释掉空格)

[[email protected] ~]# touch {a123,cd6,c78m,‘c1 my‘,m.z,‘k 67‘,8yu,789}

[[email protected] ~]#  

[[email protected] ~]# ls 

2.txt  anaconda-ks.cfg  cdrom               ks.cfg      ?????????  ??????

3.txt  boot.iso         dest                m.z         ??????     ??????

789    c1 my            install.log         t1file.txt  ??????

8yu    c78m             install.log.syslog  zhengshu    ??????

a123   cd6              k 67                ??????      ??????


1)、

[[email protected] ~]# ls [am]*

a123  anaconda-ks.cfg  m.z

2)、

[[email protected] ~]# ls *[0-9]*

2.txt  3.txt  789  8yu  a123  c1 my  c78m  cd6  k 67  t1file.txt

3)、

[[email protected] ~]# ls *[[:space:]]*[0-9]

k 67

4)、

[[email protected] ~]# ls ^c*[^[:digit:]]

ls: cannot access ^c*[^[:digit:]]: No such file or directory

[[email protected] ~]# 

[[email protected] ~]# ls [^c]*[^[:digit:]]

2.txt  8yu              boot.iso     install.log.syslog  m.z

3.txt  anaconda-ks.cfg  install.log  ks.cfg              t1file.txt


10、统计/usr/bin/目录下的文件个数;

答:

[[email protected] ~]# ls /usr/bin | wc -l

1940


11、取出/etc/passwd文件中第9至第15个用户的信息,并将其保存至/tmp/users文件中;

答:

[[email protected] ~]# head -n 15 /etc/passwd | tail -n 7 > /tmp/users

[[email protected] ~]# 

[[email protected] ~]# cat /tmp/users 

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin


12、显示/etc目录下所有以.conf结尾的文件或目录,并统计其个数。

答:

[[email protected] ~]# ls /etc/*.conf

/etc/Trolltech.conf         /etc/nfsmount.conf

/etc/asound.conf            /etc/nsswitch.conf

/etc/autofs.conf            /etc/ntp.conf

/etc/autofs_ldap_auth.conf  /etc/oddjobd.conf

/etc/cas.conf               /etc/openct.conf

/etc/dnsmasq.conf           /etc/pbm2ppa.conf

/etc/dracut.conf            /etc/pm-utils-hd-apm-restore.conf

/etc/fprintd.conf           /etc/pnm2ppa.conf

/etc/gai.conf               /etc/prelink.conf

/etc/grub.conf              /etc/readahead.conf

/etc/gssapi_mech.conf       /etc/reader.conf

/etc/hba.conf               /etc/request-key.conf

/etc/host.conf              /etc/resolv.conf

/etc/idmapd.conf            /etc/rsyslog.conf

/etc/kdump.conf             /etc/sestatus.conf

/etc/krb5.conf              /etc/smartd.conf

/etc/latrace.conf           /etc/sos.conf

/etc/ld.so.conf             /etc/sudo-ldap.conf

/etc/libaudit.conf          /etc/sudo.conf

/etc/libuser.conf           /etc/sysctl.conf

/etc/logrotate.conf         /etc/updatedb.conf

/etc/ltrace.conf            /etc/warnquota.conf

/etc/mke2fs.conf            /etc/yp.conf

/etc/mtools.conf            /etc/yum.conf

/etc/named.conf

[[email protected] ~]# 

[[email protected] ~]# ls /etc/*.conf | wc -l

49


13、把/etc/passwd文件最后三行信息中所有小写字符改为大写,并且删除所有的":"、"/"以及数字字符;

答:

[[email protected] ~]# cat /etc/passwd | tail -n 3

little:x:500:500:little:/home/little:/bin/bash

mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash

named:x:25:25:Named:/var/named:/sbin/nologin

[[email protected] ~]# 

[[email protected] ~]# cat /etc/passwd | tail -n 3 | tr a-z A-Z  

LITTLE:X:500:500:LITTLE:/HOME/LITTLE:/BIN/BASH

MYSQL:X:27:27:MYSQL SERVER:/VAR/LIB/MYSQL:/BIN/BASH

NAMED:X:25:25:NAMED:/VAR/NAMED:/SBIN/NOLOGIN

[[email protected] ~]# 

[[email protected] ~]# cat /etc/passwd | tail -n 3 | tr a-z A-Z | tr -d "‘:‘;‘/‘;[0-9]"

LITTLEXLITTLEHOMELITTLEBINBASH

MYSQLXMYSQL SERVERVARLIBMYSQLBINBASH

NAMEDXNAMEDVARNAMEDSBINNOLOGIN


14、取出/etc/fstab的第6行; //注意,空行也算一行

答: 

[[email protected] ~]# cat /etc/fstab | head -n 6 | tail -n 1

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


15、取出/etc目录下所有以s开头的文件或目录,将其中最前面的8个文件保存到/tmp/start_with_s.out并同时进行标准输出;

答:

[[email protected] ~]# ls -d /etc/s*

/etc/samba          /etc/shadow       /etc/statetab.d

/etc/sane.d         /etc/shadow-      /etc/sudo-ldap.conf

/etc/sasl2          /etc/shells       /etc/sudo.conf

/etc/scl            /etc/skel         /etc/sudoers

/etc/securetty      /etc/smartd.conf  /etc/sudoers.d

/etc/security       /etc/sos.conf     /etc/sysconfig

/etc/selinux        /etc/sound        /etc/sysctl.conf

/etc/services       /etc/ssh          /etc/system-release

/etc/sestatus.conf  /etc/ssl          /etc/system-release-cpe

/etc/setuptool.d    /etc/sssd

/etc/sgml           /etc/statetab

[[email protected] ~]# 

[[email protected] ~]# ls -d /etc/s* | head -8

/etc/samba

/etc/sane.d

/etc/sasl2

/etc/scl

/etc/securetty

/etc/security

/etc/selinux

/etc/services

[[email protected] ~]# ls -d /etc/s* | head -8 > /tmp/start_with_s.out

[[email protected] ~]# 

[[email protected] ~]# cat /tmp/start_with_s.out 

/etc/samba

/etc/sane.d

/etc/sasl2

/etc/scl

/etc/securetty

/etc/security

/etc/selinux

/etc/services


16、将目录/etc备份至/backup目录中,并重命名为“etc-当前日期_当前时间”,如etc-2013-02-26_20:46:30;要求保留文件原来的属性,保持链接文件;

答:

[[email protected] ~]# cp -a /etc /backup/etc-2017-11-15_16:43:00

cp: cannot create directory `/backup/etc-2017-11-15_16:43:00‘: No such file or directory

[[email protected] ~]# mkdir /backup 

[[email protected] ~]# ls /

backup  boot  etc   lib    lost+found  misc  net  proc  sbin     srv  tmp  var

bin     dev   home  lib64  media       mnt   opt  root  selinux  sys  usr

[[email protected] ~]# cp -a /etc /backup/etc-2017-11-15_16:43:00

[[email protected] ~]#

[[email protected] ~]# ls /backup/

etc-2017-11-15_16:43:00


17、使用touch命令基于花括号展开的方式创建如下文件:ace  acf  acg  ade  adf  adg  bce  bcf  bcg  bde  bdf  bdg

答:

[[email protected] ~]# touch {a,b}{c,d}{e,f,g} 

[[email protected] ~]# ls 

2.txt  acf              bce  boot.iso  install.log         zhengshu   ??????

3.txt  acg              bcf  c1 my     install.log.syslog  ??????     ??????

789    ade              bcg  c78m      k 67                ?????????  ??????

8yu    adf              bde  cd6       ks.cfg              ??????

a123   adg              bdf  cdrom     m.z                 ??????

ace    anaconda-ks.cfg  bdg  dest      t1file.txt          ??????


18、创建用户xiaoding,使用centos和varnish作为附属组,并将其家目录设置于/ysu;使用xiaoding用户登录到系统,在其家目录中创建名为projects的目录,并在该目录中创建demand、design、plan、project、report_bugs等文件。后来出于统一管理需要,再将其家目录迁移至/home/ysu目录中;

答:

[[email protected] ~]# groupadd centos        

[[email protected] ~]# groupadd varnish

[[email protected] ~]# tail -2 /etc/group

centos:x:501:

varnish:x:502: 

[[email protected] ~]# useradd xiaoding -G centos,varnish -d /ysu   

[[email protected] ~]# 

[[email protected] ~]# echo "18716022695" | passwd --stdin xiaoding

Changing password for user xiaoding.

passwd: all authentication tokens updated successfully.

[[email protected] ~]# 

[[email protected] ~]# tail -3 /etc/group

centos:x:501:xiaoding

varnish:x:502:xiaoding

xiaoding:x:503:

[[email protected] ~]# id xiaoding

uid=501(xiaoding) gid=503(xiaoding) groups=503(xiaoding),501(centos),502(varnish)

[[email protected] ~]# 

[[email protected] ~]# su - xiaoding

-bash: warning: setlocale: LC_CTYPE: cannot change locale (en_CN.UTF-8): No such file or directory

-bash: warning: setlocale: LC_COLLATE: cannot change locale (en_CN.UTF-8): No such file or directory

-bash: warning: setlocale: LC_MESSAGES: cannot change locale (en_CN.UTF-8): No such file or directory

-bash: warning: setlocale: LC_NUMERIC: cannot change locale (en_CN.UTF-8): No such file or directory

-bash: warning: setlocale: LC_TIME: cannot change locale (en_CN.UTF-8): No such file or directory

[[email protected] ~]$ 

[[email protected] ~]$ ls                 

[[email protected] ~]$ 

[[email protected] ~]$ mkdir -pv projects

mkdir: created directory `projects‘

[[email protected] ~]$ ls 

projects

[[email protected] ~]$ touch projects/{demand,design,plan,project,report_bugs}

[[email protected] ~]$ ls projects/

demand  design  plan  project  report_bugs

[[email protected] ~]$ 


19、以下所有命令均为使用ling用户登录后,临时以root用户的身份完成,命令执行成功之后立即返回ling用户环境;

  1)创建用户gentoo,UID为5000,基本组为centos,附加组为distro和peguin;

  2)创建用户fedora,其全名为"Fedora Core",附加组为distro和peguin,默认shell为/bin/tcsh;

  3)将用户gentoo的附加组修改为bin和root,默认shell为/bin/csh,注释信息为"Gentoo Distribution";

  4)为用户fedora添加新的附属组centos,使得其隶属于四个组中;

  5)创建系统用户iscsi,指定其UID为808,其主组是GID为808的系统组mydisk,并使其不可交互登录系统,且不为其创建家目录;

  6)由于gentoo用户出差一个月,所以直到其回公司之前,禁止其登录系统,你有几种方式解决此问题?请尽可能多的写出来。

答:

(6):

 1):# usermod -L gentoo

 2): # passwd -l gentoo

            //用来锁账户

 3): # vim /etc/shadow

     gentoo:!$6$ata(fd$adfa

 4): # usermod -s /sbin/nologin gentoo

 5): # chsh -s /sbin/nologin gentoo

            //改变登录shell

 6): # usermod -e 2017-02-04 gentoo

答:

[[email protected] ~]# groupadd distro;groupadd peguin

[[email protected] ~]# 

[[email protected] ~]# tail -2 /etc/group

distro:x:505:

peguin:x:506:

[[email protected] ~]# 

[[email protected] ~]# useradd gentoo -u 5000 -g centos -G distro,peguin

[[email protected] ~]# tail -3 /etc/group

ling:x:504:

distro:x:505:gentoo

peguin:x:506:gentoo

[[email protected] ~]# echo "18716022695" | passwd --stdin gentoo

Changing password for user gentoo.

passwd: all authentication tokens updated successfully.

[[email protected] ~]# 

[[email protected] ~]# tail -1 /etc/passwd

gentoo:x:5000:501::/home/gentoo:/bin/bash

[[email protected] ~]# 

[[email protected] ~]# tail -5 /etc/group

varnish:x:502:xiaoding

xiaoding:x:503:

ling:x:504:

distro:x:505:gentoo

peguin:x:506:gentoo

[[email protected] ~]# 

[[email protected] ~]# id gentoo

uid=5000(gentoo) gid=501(centos) groups=501(centos),505(distro),506(peguin)

[[email protected] ~]# 

[[email protected] ~]# useradd fedora -c "Fedora Core" -G distro,peguin -s /bin/tcsh

[[email protected] ~]# 

[[email protected] ~]# echo "18716022695" | passwd --stdin fedora

Changing password for user fedora.

passwd: all authentication tokens updated successfully.

[[email protected] ~]# 

[[email protected] ~]# tail -1 /etc/passwd

fedora:x:5001:5001:Fedora Core:/home/fedora:/bin/tcsh

[[email protected] ~]# tail -3 /etc/group 

distro:x:505:gentoo,fedora

peguin:x:506:gentoo,fedora

fedora:x:5001:

[[email protected] ~]# id fedora

uid=5001(fedora) gid=5001(fedora) groups=5001(fedora),505(distro),506(peguin)

[[email protected] ~]# 

[[email protected] ~]# useradd -G bin,root -s /bin/csh -c "Gentoo Distribution" gentoouseradd: user ‘gentoo‘ already exists

[[email protected] ~]# 

[[email protected] ~]# id gentoo

uid=5000(gentoo) gid=501(centos) groups=501(centos),505(distro),506(peguin)

[[email protected] ~]# 

[[email protected] ~]# usermod -aG centos fedora

[[email protected] ~]# 

[[email protected] ~]# id fedora

uid=5001(fedora) gid=5001(fedora) groups=5001(fedora),501(centos),505(distro),506(peguin)

[[email protected] ~]# 

[[email protected] ~]# groupadd -rg 808 mydisk

[[email protected] ~]# 

[[email protected] ~]# tail -1 /etc/group

mydisk:x:808:

[[email protected] ~]# 

[[email protected] ~]# useradd iscsi -u 808 -g mydisk -rM

[[email protected] ~]# 

[[email protected] ~]# tail -1 /etc/passwd

iscsi:x:808:808::/home/iscsi:/bin/bash

[[email protected] ~]# 

[[email protected] ~]# tail -1 /etc/group

mydisk:x:808:

[[email protected] ~]# 

[[email protected] ~]# id iscsi 

uid=808(iscsi) gid=808(mydisk) groups=808(mydisk)

[[email protected] ~]# 


以上是关于linux-practice(1-19)的主要内容,如果未能解决你的问题,请参考以下文章

linux-practice(25-30)

linux-practice(20-22)

《Patterns, Principles, and Pract》— chapter15 Value Objects

《Patterns, Principles, and Pract》— chapter15 Value Objects

《Patterns, Principles, and Pract》— chapter15 Value Objects

《Patterns, Principles, and Pract》— chapter14 Introducing the Domain Modeling Building Blocks