Linux實驗指導
實驗一 Linux基本指令的使用
實驗環境:fedora26 /bin/bash
1、實驗目的
學習和掌握Linux的基本指令。
2、實驗内容和步驟
步驟1:以user_login使用者身份并使用telnet登入Linux伺服器,按照提示建立自己的賬戶和密碼。
步驟 2:使用新建立的使用者賬戶和密碼登入Linux系統,察看登入後的界面。
步驟3:使用pwd指令察看目前的工作目錄,然後用ls指令檢視目前目錄下的内容,嘗試使用-a,-l,-F,-A,-lF等不同選項并比較不同之處。
[[email protected] ~]$ pwd
/home/yan
[[email protected] ~]$ ls
公共 模闆 視訊 圖檔 文檔 下載下傳 音樂 桌面
[[email protected] ~]$ ls-a
. .bash_profile .dbus .java .pki 模闆 下載下傳
.. .bashrc .eclipse .lesshst .sogouinput 視訊 音樂
.bash_history .cache .esd_auth .local .swt 圖檔 桌面
.bash_logout .config .ICEauthority .mozilla 公共 文檔
[[email protected] ~]$ gnome-screenshot
[[email protected] ~]$ gnome-screenshot -a
[[email protected] ~]$clear
[[email protected] ~]$ pwd
/home/yan
[[email protected] ~]$ ls
公共 模闆 視訊 圖檔 文檔 下載下傳 音樂 桌面
[[email protected] ~]$ ls-a
. .bash_profile .dbus .java .pki 模闆 下載下傳
.. .bashrc .eclipse .lesshst .sogouinput 視訊 音樂
.bash_history .cache .esd_auth .local .swt 圖檔 桌面
.bash_logout .config .ICEauthority .mozilla 公共 文檔
[[email protected] ~]$ ls-l
總用量 32
drwxr-xr-x.2 yan yan 4096 6月 1121:27 公共
drwxr-xr-x.2 yan yan 4096 6月 1121:27 模闆
drwxr-xr-x.2 yan yan 4096 6月 1121:27 視訊
drwxr-xr-x.2 yan yan 4096 6月 1423:00 圖檔
drwxr-xr-x.3 yan yan 4096 6月 1300:15 文檔
drwxr-xr-x.2 yan yan 4096 6月 1422:57 下載下傳
drwxr-xr-x.2 yan yan 4096 6月 1121:27 音樂
drwxr-xr-x.2 yan yan 4096 6月 1121:27 桌面
[[email protected] ~]$ ls-A
.bash_history .cache .esd_auth .local .swt 圖檔 桌面
.bash_logout .config .ICEauthority .mozilla 公共 文檔
.bash_profile .dbus .java .pki 模闆 下載下傳
.bashrc .eclipse .lesshst .sogouinput 視訊 音樂
[[email protected] ~]$ ls-lF
總用量 32
drwxr-xr-x.2 yan yan 4096 6月 1121:27 公共/
drwxr-xr-x.2 yan yan 4096 6月 1121:27 模闆/
drwxr-xr-x.2 yan yan 4096 6月 1121:27 視訊/
drwxr-xr-x.2 yan yan 4096 6月 1423:00 圖檔/
drwxr-xr-x.3 yan yan 4096 6月 1300:15 文檔/
drwxr-xr-x.2 yan yan 4096 6月 1422:57 下載下傳/
drwxr-xr-x.2 yan yan 4096 6月 1121:27 音樂/
drwxr-xr-x.2 yan yan 4096 6月 1121:27 桌面/
步驟4:在目前目錄下建立一個名為test的新目錄,然後将工作目錄切換到test下,嘗試将/etc目錄下的檔案passwd拷貝到該目錄下(cp源檔案 目的目錄)。察看目前目錄下的passwd檔案的屬主和檔案權限。
[[email protected] ~]$mkdir test
[[email protected] ~]$ cdtest
[[email protected] test]$cp -r /etc/passwd ./
[[email protected] test]$ls
passwd
[[email protected] test]$ls -l passwd
-rw-r--r--.1 yan yan 2385 6月 1423:09 passwd
步驟5:嘗試向目前目錄下的passwd檔案和/etc/passwd檔案分别寫入一些新内容(可使用echo“字元串”>>檔案的指令),看看操作能否成功,如果不能成功,請說明原因。用cat指令浏覽檔案password的内容,用more指令進行浏覽翻頁操作,再用less指令浏覽檔案的内容。比較這幾個指令的不同之處
[[email protected]]$ echo "你好123">> passwd
[[email protected]]$ echo "你好123">> /etc/passwd
bash: /etc/passwd:Permission denied
目前passwd檔案可寫入,/etc/passwd檔案不可寫入
因為 目前passwd檔案擁有寫權限而目前使用者對/etc/passwd沒有寫權限
[[email protected]]$ cat passwd | more //分屏顯示,空格可以切換下一頁
[[email protected]]$ cat passwd | less //less 在檢視之前不會加載整個檔案
步驟6:用ls指令檢視test下檔案的權限,用mv指令更改檔案password的檔案名為test.txt,嘗試用chown和chgrp更改檔案的屬主為root、組為root,看看能否成功,不成功,請說明原因。嘗試用chomd将檔案權限為“-rw-------”。看看能否成功,不成功,請說明原因。
[[email protected] test]$ls -l passwd
-rw-r--r--.1 yan yan 2406 6月 1423:15 passwd
[[email protected] test]$mv passwd test.txt
[[email protected] test]$ls
test.txt
[[email protected] test]$chown root test.txt
chown:正在更改'test.txt'的所有者:Operation not permitted
[[email protected] test]$chgrp root test.txt
chgrp:正在更改'test.txt'的所屬組:Operation not permitted
[[email protected] test]$chmod -rw------ test.txt
[[email protected] test]$ls -a test.txt
test.txt
步驟7:用rm指令删除test目錄下的所有檔案,再用rmdir指令删除test目錄。 (想一想有沒有一條指令将目錄及目錄下的所有檔案删除,寫出這條指令)
[[email protected] ~]$ rm-f test/*
[[email protected] ~]$rmdir test
[[email protected] ~]$ rm-rf test
步驟8:使用ps指令檢視目前系統内的程序,并利用man指令擷取ps指令的參數,寫出擷取目前終端程序執行情況的ps指令。
[[email protected] ~]$ ps
PIDTTY TIME CMD
3878pts/0 00:00:00 bash
4029pts/0 00:00:00 less
4422pts/0 00:00:00 ps
[[email protected] ~]$ ps-T
PID SPID TTY TIME CMD
3878 3878 pts/0 00:00:00 bash
4029 4029 pts/0 00:00:00 less
4482 4482 pts/0 00:00:00 ps
步驟9:使用df指令檢視目前系統已安裝的檔案系統的空間使用情況,記錄結果。
[[email protected] ~]$ df
檔案系統 1K-塊 已用 可用 已用%挂載點
devtmpfs 1946164 0 1946164 0% /dev
tmpfs 1959560 25940 1933620 2% /dev/shm
tmpfs 1959560 2036 1957524 1% /run
tmpfs 1959560 0 1959560 0% /sys/fs/cgroup
/dev/mapper/fedora-root47122644 8518380 36180868 20% /
tmpfs 1959560 64 1959496 1% /tmp
/dev/sda8 999320 147752 782756 16% /boot
/dev/sda1 98304 43284 55020 45% /boot/efi
tmpfs 391912 12 391900 1% /run/user/42
tmpfs 391912 40 391872 1% /run/user/1000
步驟10:使用du指令檢視使用者的工作目錄占用了多少空間,記錄結果。
[[email protected] ~]$ cdtest
[[email protected] test]$du
4 .
步驟11:使用free指令檢視記憶體資源的使用情況,記錄結果。
[[email protected] ~]$free
total used free shared buff/cache available
Mem: 3919120 1707840 706012 341852 1505268 1785636
Swap: 4059132 0 4059132
步驟12:使用man擷取tar和gzip的幫助資訊,嘗試将test目錄下的檔案打包并壓縮,然後到另外一目錄tmp下解包,寫出這幾條指令。
tar:
[[email protected] ~]$ tarcvf a.tar ./test
./test/
[[email protected] ~]$ ls
a.tar more test 公共 模闆 視訊 圖檔 文檔 下載下傳 音樂 桌面
[[email protected] ~]$ tarxvf a.tar
./test/
[[email protected] ~]$ ls
a.tar more test 公共 模闆 視訊 圖檔 文檔 下載下傳 音樂 桌面
[[email protected] tmp]$tar xvf ~/a.tar
./test/
gzip:
[[email protected] test]$gzip test
[[email protected] test]$ls
test.gz
[[email protected] test]$gzip -d test.gz
[[email protected] test]$ls
test
步驟13:嘗試執行“ls–l> tmp”,看看這條指令的執行會出現什麼結果,解釋一下這條指令。
[[email protected] ~]$ ls-l > tmp
bash: tmp: Is adirectory
步驟14:嘗試執行find/usr/src -name *.c -print | xargs grep“#include”,看看這條指令的執行會出現什麼結果,解釋一下這條指令。
結果:列印出了/usr/src下到所有的頭檔案的包含行
解釋:查詢 /usr/src下到所有.c檔案并篩選出帶#include的行并列印
步驟15:執行cal和date指令,說說這兩條指令的用途。

[[email protected]/]$ date
2018年06月15日星期五 00:16:59CST
步驟16:執行指令clear和logout,退出系統。(想一想有沒有其他的方法,寫出這種方法)
exit
步驟17:執行指令shutdown,關閉系統。(想一想有沒有更簡單的指令,寫出這條指令)
1、halt 立刻關機
2、poweroff 立刻關機
3、shutdown-h now 立刻關機(root使用者使用)
4、shutdown-h 10 10分鐘後自動關機
3、實驗結論
linux内置指令提供了管理系統的很多實用功能,通過使用這些指令可以完成對系統到管理和使用.
實驗二:腳本程式設計
1.目的和要求
加強對腳本程式設計的了解。
2.實驗内容
參數清單,循環。把下面程式的參數清單顯示用for循環實作。
3.實驗提示
salutation=”Hello”
echo$salutation
echo“The program $0 is now running”
echo“The second parameter was $2”
echo“The first parameter was $1”
echo“The parameter list was $*”
echo“The user’s home directory is $HOME”
echo“Please enter a new greeting”
readsalutation
echo$salutation
echo“The script is now complete”
exit0
4.實驗運作結果
改寫後:
#!/bin/sh
salutation=”Hello”
echo$salutation
count=0
str=""
forstr in $*
do
echo"the parameter $count is $str "
count=$(($count+1));
done
echo“The user’shome directory is $HOME”
echo“Please enter a new greeting”
readsalutation
echo$salutation
echo“The script is now complete”
exit0
實驗三檔案操作
1.目的和要求
熟悉檔案操作的系統調用和庫函數
2.實驗内容
比較使用系統調用和庫函數的檔案拷貝程式的速度性能
3.實驗提示//c編譯時的錯誤:程式中有遊離的‘\240’‘\302’‘\XXX 時解決方案
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
intmain()
{
char c;
intin, out;
in= open(“file.in”, O_RDONLY);
out= open(“file.out”, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while(read(in,&c,1)== 1)
write(out,&c,1);
exit(0);
}
#include<stdio.h>
#include<stdlib.h>
intmain()
{
intc;
FILE*in, *out;
in= fopen(“file.in”,”r”);
out= fopen(“file.out”,”w”);
while((c= fgetc(in)) != EOF)
fputc(c,out);
exit(0);
}
4.實驗運作結果
系統調用:
庫函數:
結論:
系統調用比庫函數慢,因為系統調用要進行使用者态到核心态的上下文切換
實驗四 建立子程序
1.目的和要求
熟悉建立子程序的系統調用fork()
2.實驗内容
建立子程序并實作父子程序的同步
3.實驗提示
#include<sys/types.h>
#include<sys/wait.h>
#include<unistd.h>
#include<stdio.h>
intmain()
{
pid_tpid;
char*message;
intn;
intexit_code;
printf(“forkprogram starting\n”);
pid= fork();
switch(pid)
{
case-1:
perror(“forkfailed”);
exit(1);
case0:
message= “This is the child”;
n= 5;
exit_code= 37;
break;
default:
message= “This is the parent”;
n= 3;
exit_code= 0;
break;
}
for(;n > 0; n--) {
puts(message);
sleep(1);
}
if(pid != 0) {
intstat_val;
pid_tchild_pid;
child_pid= wait(&stat_val);
printf(“Childhas finished: PID = %d\n”, child_pid);
if(WIFEXITED(stat_val))
printf(“Childexited with code %d\n”, WEXITSTATUS(stat_val));
else
printf(“Childterminated abnormally\n”);
}
exit(exit_code);
}
4.實驗運作結果
實驗五 SOCKET
1.目的和要求
了解socket的基本原理。
2.實驗内容
利用socket完成本地通信
3.實驗提示
服務端代碼:
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<sys/un.h>
#include<unistd.h>
#include<stdlib.h>
intmain()
{
intserver_sockfd, client_sockfd;
intserver_len, client_len;
structsockaddr_un server_address;
structsockaddr_un client_address;
unlink(“server_socket”);
server_sockfd= socket(AF_UNIX, SOCK_STREAM, 0);
server_address.sun_family= AF_UNIX;
strcpy(server_address.sun_path,“server_socket”);
server_len= sizeof(server_address);
bind(server_sockfd,(struct sockaddr *)&server_address, server_len);
listen(server_sockfd,5);
while(1){
charch;
printf(“serverwaiting\n”);
client_len= sizeof(client_address);
client_sockfd= accept(server_sockfd,
(structsockaddr *)&client_address, &client_len);
read(client_sockfd,&ch, 1);
ch++;
write(client_sockfd,&ch, 1);
close(client_sockfd);
}
}
用戶端代碼:
#include<sys/types.h>
#include<sys/socket.h>
#include<stdio.h>
#include<sys/un.h>
#include<unistd.h>
#include<stdlib.h>
intmain(){
intsockfd;
intlen;
structsockaddr_un address;
intresult;
charch='A';
sockfd=socket(AF_UNIX,SOCK_STREAM, 0);
address.sun_family=AF_UNIX;
strcpy(address.sun_path,"server_socket");
len=sizeof(address);
result=connect(sockfd,(struct sockaddr*) &address, len);
if(result==-1){
perror("oops:client1");
exit(1);
}
write(sockfd,&ch,1);
read(sockfd,&ch,1);
printf("charfrom server=%c\n",ch);
close(sockfd);
exit(0);
}
4.實驗運作結果