1. 程式人生 > >linux-practice(1-19)

linux-practice(1-19)

practice

1、了解GPL(v1、v2、v3)、LGPL、Apache、BSD、MIT等開源協定的具體內容。

答:

BSD:不允許用作者名字打廣告,可商業化

Apache Licence2.0:註明修改處,可商業化

GPL:開源、免費。禁止商業化

LGPL:適合作為第三方類庫,可商業化


2、使用echo如何顯示“The year is 2017.\nToday is monday”為兩行?

答:

[root@little ~]# echo -e "The year is 2017.\nToday is monday"

The year is 2017.

Today is monday


3、使用printf命令如何顯示“ling little”為兩行?

答:

[root@little ~]# 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

答:

[root@little ~]# 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目錄中,要求保留文件原來的屬性,保持鏈接文件;

答:

[root@little ~]# mkdir -pv /backup

mkdir: 已創建目錄 "/backup"

[root@little ~]# mv /etc /backup

[root@little ~]# echo $?

0

[root@little ~]# ls /backup/

etc


3、通過man幫助手冊,自行總結which、whereis、whatis命令的使用方法;

答:

which:命令的別名

whereis:命令的路徑

whatis:命令作用

[root@little ~]# which ls

alias ls=‘ls --color=auto‘

/bin/ls

[root@little ~]# whereis ls

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


4、通過幫助手冊,學習使用du命令;並思考:如何顯示某個目錄內部的所有文件的整體大小?

答:

du:顯示目錄大小

[root@little ~]# 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

……

[root@little ~]# du ./test

12 ./test


5、通過幫助手冊,學習who、w、whoami命令,並對比who和w,思考其區別;

答:

who:登錄用戶

w:顯示用戶信息,更加詳細

whoami:顯示ID為0的用戶信息


[root@little ~]# who

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

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

[root@little ~]# w

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

USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT

[root@little ~]# whoami

whoami:無法找到用戶ID 為0 的用戶名


6、顯示/etc目錄下,以字母開頭,後面跟了一個非字母及其它任意長度任意字符的文件或目錄;

答:

[root@little ~]# ls /etc | grep "^[[:alpha:]][^[:alpha:]].*"

X11

[root@little ~]# ls -d /etc/[[:alpha:]][^[:alpha:]]*

/etc/X11


7、顯示/usr/share/man目錄下,所有以man開頭,後跟一個數字結尾的文件或目錄;

答:

[root@little ~]# 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

[root@little ~]#

[root@little ~]# 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目錄下;

答:

[root@little ~]# 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

[root@little ~]#

[root@little ~]# cp -a /etc/[pmr]*.conf /tmp

[root@little ~]#

[root@little ~]# 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字母開頭並且不以數字結尾的所有文件;

答:

錯誤創建如下:(空格將其分開,產生錯誤)

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

[root@little ~]#

[root@little ~]# 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 ?????? ??????


正確創建如下:(處理空格,註釋掉空格)

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

[root@little ~]#

[root@little ~]# 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)、

[root@little ~]# ls [am]*

a123 anaconda-ks.cfg m.z

2)、

[root@little ~]# ls *[0-9]*

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

3)、

[root@little ~]# ls *[[:space:]]*[0-9]

k 67

4)、

[root@little ~]# ls ^c*[^[:digit:]]

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

[root@little ~]#

[root@little ~]# 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/目錄下的文件個數;

答:

[root@little ~]# ls /usr/bin | wc -l

1940


11、取出/etc/passwd文件中第9至第15個用戶的信息,並將其保存至/tmp/users文件中;

答:

[root@little ~]# head -n 15 /etc/passwd | tail -n 7 > /tmp/users

[root@little ~]#

[root@little ~]# 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結尾的文件或目錄,並統計其個數。

答:

[root@little ~]# 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

[root@little ~]#

[root@little ~]# ls /etc/*.conf | wc -l

49


13、把/etc/passwd文件最後三行信息中所有小寫字符改為大寫,並且刪除所有的":"、"/"以及數字字符;

答:

[root@little ~]# 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

[root@little ~]#

[root@little ~]# 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

[root@little ~]#

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

LITTLEXLITTLEHOMELITTLEBINBASH

MYSQLXMYSQL SERVERVARLIBMYSQLBINBASH

NAMEDXNAMEDVARNAMEDSBINNOLOGIN


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

答:

[root@little ~]# 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並同時進行標準輸出;

答:

[root@little ~]# 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

[root@little ~]#

[root@little ~]# ls -d /etc/s* | head -8

/etc/samba

/etc/sane.d

/etc/sasl2

/etc/scl

/etc/securetty

/etc/security

/etc/selinux

/etc/services

[root@little ~]# ls -d /etc/s* | head -8 > /tmp/start_with_s.out

[root@little ~]#

[root@little ~]# 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;要求保留文件原來的屬性,保持鏈接文件;

答:

[root@little ~]# 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

[root@little ~]# mkdir /backup

[root@little ~]# 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

[root@little ~]# cp -a /etc /backup/etc-2017-11-15_16:43:00

[root@little ~]#

[root@little ~]# ls /backup/

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


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

答:

[root@little ~]# touch {a,b}{c,d}{e,f,g}

[root@little ~]# 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目錄中;

答:

[root@little ~]# groupadd centos

[root@little ~]# groupadd varnish

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

centos:x:501:

varnish:x:502:

[root@little ~]# useradd xiaoding -G centos,varnish -d /ysu

[root@little ~]#

[root@little ~]# echo "18716022695" | passwd --stdin xiaoding

Changing password for user xiaoding.

passwd: all authentication tokens updated successfully.

[root@little ~]#

[root@little ~]# tail -3 /etc/group

centos:x:501:xiaoding

varnish:x:502:xiaoding

xiaoding:x:503:

[root@little ~]# id xiaoding

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

[root@little ~]#

[root@little ~]# 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

[xiaoding@little ~]$

[xiaoding@little ~]$ ls

[xiaoding@little ~]$

[xiaoding@little ~]$ mkdir -pv projects

mkdir: created directory `projects‘

[xiaoding@little ~]$ ls

projects

[xiaoding@little ~]$ touch projects/{demand,design,plan,project,report_bugs}

[xiaoding@little ~]$ ls projects/

demand design plan project report_bugs

[xiaoding@little ~]$


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

答:

[root@little ~]# groupadd distro;groupadd peguin

[root@little ~]#

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

distro:x:505:

peguin:x:506:

[root@little ~]#

[root@little ~]# useradd gentoo -u 5000 -g centos -G distro,peguin

[root@little ~]# tail -3 /etc/group

ling:x:504:

distro:x:505:gentoo

peguin:x:506:gentoo

[root@little ~]# echo "18716022695" | passwd --stdin gentoo

Changing password for user gentoo.

passwd: all authentication tokens updated successfully.

[root@little ~]#

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

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

[root@little ~]#

[root@little ~]# tail -5 /etc/group

varnish:x:502:xiaoding

xiaoding:x:503:

ling:x:504:

distro:x:505:gentoo

peguin:x:506:gentoo

[root@little ~]#

[root@little ~]# id gentoo

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

[root@little ~]#

[root@little ~]# useradd fedora -c "Fedora Core" -G distro,peguin -s /bin/tcsh

[root@little ~]#

[root@little ~]# echo "18716022695" | passwd --stdin fedora

Changing password for user fedora.

passwd: all authentication tokens updated successfully.

[root@little ~]#

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

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

[root@little ~]# tail -3 /etc/group

distro:x:505:gentoo,fedora

peguin:x:506:gentoo,fedora

fedora:x:5001:

[root@little ~]# id fedora

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

[root@little ~]#

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

[root@little ~]#

[root@little ~]# id gentoo

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

[root@little ~]#

[root@little ~]# usermod -aG centos fedora

[root@little ~]#

[root@little ~]# id fedora

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

[root@little ~]#

[root@little ~]# groupadd -rg 808 mydisk

[root@little ~]#

[root@little ~]# tail -1 /etc/group

mydisk:x:808:

[root@little ~]#

[root@little ~]# useradd iscsi -u 808 -g mydisk -rM

[root@little ~]#

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

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

[root@little ~]#

[root@little ~]# tail -1 /etc/group

mydisk:x:808:

[root@little ~]#

[root@little ~]# id iscsi

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

[root@little ~]#


linux-practice(1-19)