mem訪問
一直以root登陸使用linux的人來說很少有許可權被拒這種概念,但某些時候又深受許可權拒絕困擾。
知道為什麼很多程序中需要使用getuid(),setuid()?為什麼以普通許可權登陸的用戶不能進入/root,為什麼在/目錄下執行ls -l後可以顯示root的信息,但ls /root -al卻是許可權不夠?為什麼有些文件夾可以繼續創建文件,但就是不能ls?等等,相信看了此文就能明白。
主要是學習筆記,不足之處請指正。
CentOS 5.4 [testc@xxx opt]$ uname -a Linux xxx 2.6.18-164.el5xen #1 SMP Thu Sep 3 04:47:32 EDT 2009 i686 i686 i386 GNU/Linux
一、口令文件1,格式存儲文件/etc/passwd,格式如下:root:x:0:0:root:/root:/bin/bash aaa:x:501:501:bj, bj, 8111111,136000111:/home/aaa:/bin/bash用戶名:加密密碼:用戶ID:組ID:注釋:工作目錄:shell:
默認情況是第一行的格式;注釋欄位可以自行修改,用逗號隔開,如第二行格式,這主要是給finger命令使用時可解析。
可以vi /etc/passwd修改,但為了保證其格式的正確性,請用vipw命令編譯此文件。
sh-3.2# finger aaa Login: aaa Name: bj Directory: /home/aaa Shell: /bin/bash Office: bj, 8111111 Home Phone: 136000111 Never logged in. No mail. No Plan.
2,編程實例
/*getpwnam_pwuid.c*/ #include #include #include
int main(void)
{ //struct passwd *pwd = getpwnam("aaa");struct passwd *pwd = getpwuid(501);if(pwd == NULL)
{ printf("err.\n");return 1;}
printf("name:%s\n", pwd->pw_name);printf("passwd:%s\n", pwd->pw_passwd);printf("description:%s\n", pwd->pw_gecos);printf("uid:%d\n", pwd->pw_uid);printf("gid:%d\n", pwd->pw_gid);printf("dir:%s\n", pwd->pw_dir);printf("shell:%s\n", pwd->pw_shell);
return 0;}
sh-3.2# gcc getpwnam_pwuid.c -o app sh-3.2# ./app name:aaa passwd:x description:bj, bj, 8111111,136000111 uid:501 gid:501 dir:/home/aaa shell:/bin/bash
二、組文件1,格式存儲文件/etc/group,格式如下root:x:0:root bin:x:1:root,bin,daemon aaa:x:501:組名:加密密碼:組ID:指向的各用戶名
2,改變文件uid和gid.
sh-3.2# pwd /root/study sh-3.2# ls -al -rw-r——r—— 1 root root 397 10-11 03:23 test.c
chgrp 改變所屬組ID,當然只有root許可權才可以修改。
sh-3.2# chgrp aaa test.c sh-3.2# ls -al -rw-r——r—— 1 root aaa 397 10-11 03:23 test.c
這個aaa就是新組名,其在/etc/group中,可以通過adser aaa自行添加sh-3.2# cat /etc/group root:x:0:root bin:x:1:root,bin,daemon daemon:x:2:root,bin,daemon.
gdm:x:42:sabayon:x:86:plmtest:x:500:aaa:x:501:
chown 改變用戶ID或組ID sh-3.2# chown aaa:aaa test.c sh-3.2# ls -al -rw-r——r—— 1 aaa aaa 397 10-11 03:23 test.c
3,編程實例
/*getgrnam.c*/ #include #include
int main(int argc, char *argv[])
{ if(argv[1] == NULL)
{ printf("input error.\n");return 1;}
struct group *gp = getgrnam(argv[1]);if(gp == NULL)
{ printf("err.\n");return 1;}
printf("name:%s\n", gp->gr_name);printf("psswd:%s\n", gp->gr_passwd);printf("gid:%d\n", gp->gr_gid);
int i;for(i = 0; gp->gr_mem[i] != NULL; i++)
{ printf("group name:%s\n", gp->gr_mem[i]);}
return 0;}
sh-3.2# gcc getgrnam.c -o app sh-3.2# ./app bin name:bin psswd:x gid:1 group name:root group name:bin group name:daemon 4,文件許可權不細講了sh-3.2# ls -al總計 483984 drwxr-x—— 13 root root 4096 02-22 00:01 . drwxr-xr-x 32 root root 4096 02-21 21:15 ……
-rw-r——r—— 1 root root 464023491 10-25 22:33 3.3.005-080425.tgz -rw—— 1 root root 9346 02-21 23:16 .bash_history -rw-r——r—— 1 root root 24 2007-01-06 .bash_logout -rw-r——r—— 1 root root 191 2007-01-06 .bash_profile -rw-r——r—— 1 root root 176 2007-01-06 .bashrc drwxrwxrwx 10 1000 users 4096 08-23 20:16 cflow-1.3 -rw-r——r—— 1 root root 759691 08-23 20:13 cflow.tar.gz -rw-r——r—— 1 root root 100 2007-01-06 .cshrc -rwxr-xr-x 1 root root 582 11-11 21:48 delete_M.sh -rw-r——r—— 1 root root 2518 11-11 20:25 .dir_colors
主要是最左邊一列:drwxr-x——10個字元,最左邊是文件類型,-默認為普通文件;d:目錄文件;l符號鏈接……
後面9個,3個一組共三組,分別表示所屬用戶uid的許可權;所屬組或者附屬組gid的許可權;其它許可權。
三個字元分別是讀、寫、執行許可權讀4,寫2, 執行1
所以chmod 777 test.c,提升到讀、寫、執行許可權。
5,組許可權操作實例此節演示相同組的成員之間共享資源,即不同uid但相同gid的用戶共享同一組的資源。
為了方便起見,我同時開了兩個終端。
"sh-3.2#"以root許可權登陸的shell /bin/sh "[testa@xxx root]"以testa用戶登陸的shell
註:下文提到的「用戶」是指/etc/passwd里定義的通過終端登陸的用戶(此文即以下增加的三個賬號名)。
sh-3.2# useradd testa sh-3.2# useradd testb sh-3.2# useradd testc
sh-3.2# tail -f /etc/passwd -n 4 sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin testa:x:500:500::/home/testa:/bin/bash testb:x:501:501::/home/testb:/bin/bash testc:x:502:502::/home/testc:/bin/bash
再開一個終端登陸testa,之前那個終端保持。
sh-3.2# su testa [testa@xxx root]$ id uid=500(testa) gid=500(testa) groups=500(testa)
[testa@xxx home]$ ls -al總計 28 drwxr-xr-x 5 root root 4096 02-21 22:52 . drwxr-xr-x 32 root root 4096 02-21 21:15 ……
drwx—— 3 testa testa 4096 02-21 22:56 testa drwx—— 3 testb testb 4096 02-21 22:48 testb drwx—— 3 testc testc 4096 02-21 22:52 testc
[testa@xxx home]$ cd testb bash: cd: testb: 許可權不夠
通過root修改testb目錄許可權為770,即當前uid或者gid相同的用戶均有讀寫執行許可權。
sh-3.2# cd /home/ sh-3.2# chmod 770 testb
[testa@xxx home]$ ls -al總計 28 drwxr-xr-x 5 root root 4096 02-21 22:52 . drwxr-xr-x 32 root root 4096 02-21 21:15 ……
drwx—— 3 testa testa 4096 02-21 22:56 testa drwxrwx—— 3 testb testb 4096 02-21 22:48 testb (here modify)
drwx—— 3 testc testc 4096 02-21 22:52 testc
[testa@xxx home]$ cd testb bash: cd: testb: 許可權不夠[testa@xxx root]$ id uid=500(testa) gid=500(testa) groups=500(testa)
此時雖然開放了testb的所屬組許可權,但用戶testa的gid=500(testa) groups=500(testa),它還不屬於testb組。
下面修改testa的gid為testb(或者增加其附屬組groups值為testb)
sh-3.2# usermod -G testb testa (增加用戶testa的附屬組testb)
sh-3.2# id testa uid=500(testa) gid=500(testa) groups=500(testa),501(testb)
此時testa終端需要重新登下,使剛才更改生效[testa@xxx root]$ exit exit [root@xxx ~]# su testa [testa@xxx root]$ id uid=500(testa) gid=500(testa) groups=500(testa),501(testb)
[testa@xxx root]$ cd /home/ [testa@xxx home]$ ls -al總計 28 drwxr-xr-x 5 root root 4096 02-21 22:52 . drwxr-xr-x 32 root root 4096 02-21 21:15 ……
drwx—— 3 testa testa 4096 02-21 22:56 testa drwxrwx—— 3 testb testb 4096 02-21 22:48 testb drwx—— 3 testc testc 4096 02-21 22:52 testc [testa@xxx home]$ cd testb [testa@xxx testb]$ pwd /home/testb
以上是增加了用戶testa的附屬組testb,使其對於屬於testb組的資源有了訪問許可權。
下面再使用newgrp切換用戶testa的gid.
[testa@xxx testb]$ id uid=500(testa) gid=500(testa) groups=500(testa),501(testb)
[testa@xxx testb]$ newgrp testb [testa@xxx testb]$ id uid=500(testa) gid=501(testb) groups=500(testa),501(testb)
此時testa用戶的gid已改為501(testb)。
組之前的關系在文件/etc/group sh-3.2# tail -f /etc/group -n 4 sabayon:x:86:testa:x:500:testb:x:501:testa (最後一列:組內用戶列表。即組testb里包含testa,testa屬於testb組,大概就這意思吧……)
testc:x:502:
雖然知道控制組關系的文件,但不能直接修改些文件,否則執行newgrp時會出現"抱歉"錯誤提示。
當然root用戶許可權是無限制的,它訪問文件時不需要進行許可權檢查。
三、相關系統調用getuid();getgid();int setuid(uid_t uid);int setgid(gid_t gid);
只有超級用戶或者需要設置的uid和當前用戶的uid一致才可以設置,否則返回-1,置errno = EPERM, errno可以通過strerror()翻譯。
其它:[testa@xxx home]$ su testa [testa@xxx home]$ sudo touch aa
testa is not in the sudoers file. This incident will be reported.
以root許可權vim /etc/sudoers增加testa ALL=(ALL) ALL
參考:APUE2E,1.8, 4.4, 8.11
② 微機硬體系統中最核心的部件是
中央處理器。
英文全稱central processing unit,簡稱CPU,中央處理器是數據的指揮中心和控制中心,好比司令,具有控制、運算和寄存功能。
所以它是微機系統的核心部件。中央處理器也是主機中比較貴的硬體。一般有INTEL和AMD兩大品牌。
工作原理:
CPU的工作分為以下 5 個階段:取指令階段、指令解碼階段、執行指令階段、訪存取數和結果寫回。
取指令(IF,instruction fetch),即將一條指令從主存儲器中取到指令寄存器的過程。程序計數器中的數值,用來指示當前指令在主存中的位置。當 一條指令被取出後,程序計數器(PC)中的數值將根據指令字長度自動遞增。
指令解碼階段(ID,instruction decode),取出指令後,指令解碼器按照預定的指令格式,對取回的指令進行拆分和解釋,識別區分出不同的指令類 別以及各種獲取操作數的方法。現代CISC處理器會將拆分已提高並行率和效率。
執行指令階段(EX,execute),具體實現指令的功能。CPU的不同部分被連接起來,以執行所需的操作。
訪存取數階段(MEM,memory),根據指令需要訪問主存、讀取操作數,CPU得到操作數在主存中的地址,並從主存中讀取該操作數用於運算。部分指令不需要訪問主存,則可以跳過該階段。
結果寫回階段(WB,write back),作為最後一個階段,結果寫回階段把執行指令階段的運行結果數據「寫回」到某種存儲形式。
結果數據一般會被寫到CPU的內部寄存器中,以便被後續的指令快速地存取;許多指令還會改變程序狀態字寄存器中標志位的狀態,這些標志位標識著不同的操作結果,可被用來影響程序的動作。
在指令執行完畢、結果數據寫回之後,若無意外事件(如結果溢出等)發生,計算機就從程序計數器中取得下一條指令地址,開始新一輪的循環,下一個指令周期將順序取出下一條指令。
③ 怎麼遠程訪問H2資料庫的內存模式
簡單來說就是用jdbc:h2:mem:h2db來建立內存模式,並建表,
然後jdbc:h2:tcp://192.168.20.141:8082/mem:h2db來訪問上面的內存資料庫
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.tools.Server;
public class H2Demo {
private Server server;
private String port = "8082";
private static String sourceURL1 = "jdbc:h2:mem:h2db";
private static String sourceURL2 = "jdbc:h2:tcp://192.168.20.141:8082/mem:h2db";
private String user = "shorturl";
private String password = "123456";
public void startServer() {
try {
System.out.println("正在啟動h2...");
server = Server.createTcpServer(
new String[] { "-tcpPort", port }).start();
} catch (SQLException e) {
System.out.println("啟動h2出錯:" + e.toString());
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void stopServer() {
if (server != null) {
System.out.println("正在關閉h2...");
server.stop();
System.out.println("關閉成功.");
}
}
public void useH2() {
try {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection(sourceURL1,user, password);
Statement stat = conn.createStatement();
// insert data
stat.execute("CREATE MEMORY Table TEST(NAME VARCHAR)");
stat.execute("INSERT INTO TEST VALUES('Hello World')");
//stat.execute("delete mappedURL");
// use data
ResultSet result = stat.executeQuery("select name from test ");
int i = 1;
while (result.next()) {
System.out.println(i++ + ":" + result.getString("name"));
}
result.close();
stat.close();
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void useH2i() {
try {
Class.forName("org.h2.Driver");
//Connection conn = DriverManager.getConnection("jdbc:h2:" + dbDir+";AUTO_SERVER=TRUE;MVCC=TRUE",user, password);
Connection conn = DriverManager.getConnection(sourceURL2,user, password);
Statement stat = conn.createStatement();
// use data
ResultSet result = stat.executeQuery("select name from test");