Gnu/LinuxシステムCプログラミング--ユーザーとグループ
Linuxユーザーとグループ
各ユーザーには、ユニークなログイン名と関連するデジタルIDがあります.このデジタルIDは、私たちがよく言っているUIDです.各ユーザは、1つのグループのメンバーであってもよいし、複数の他のグループのメンバーであってもよい.しかし、各グループにも唯一の名前と数字の識別があります.この数字の識別は私たちがよく言っているGIDです.
ユーザーとグループを設計するIDsの主な目的は2つあります.1つは、システムリソースがどのシステムユーザーに属しているかを決定することです.2つ目は、プロセスがこれらのシステムリソースにアクセスするときにどのような権限制御を付与すべきかです.たとえば、各ファイルは特定のユーザーとグループに属し、各プロセスには、プロセスとこれらのプロセスがファイルにアクセスするときにどのような権限を持っているかを決定するユーザーとグループIDsがあります.
この章では、/etc/passwd、/etc/groupdなどのシステムファイルについて検討します.これらのファイルには、システムのユーザーとグループの情報が定義されています.次に、これらのファイルからユーザー情報を取得するライブラリ関数について説明します.終了時にはcrypt()関数も紹介します.これはログインパスワードを暗号化および認証するために使用されます.
システムのパスワードファイルは/etc/passwdで、ユーザーのアカウント情報が含まれており、一行はユーザーを表しています.各行は半角のセミコロンで区切られ、7つのフィールドがあります.
各フィールドの意味を順に説明します.
getpwnam関数の簡単な使用、
コンパイルして実行し、
各ユーザーには、ユニークなログイン名と関連するデジタルIDがあります.このデジタルIDは、私たちがよく言っているUIDです.各ユーザは、1つのグループのメンバーであってもよいし、複数の他のグループのメンバーであってもよい.しかし、各グループにも唯一の名前と数字の識別があります.この数字の識別は私たちがよく言っているGIDです.
ユーザーとグループを設計するIDsの主な目的は2つあります.1つは、システムリソースがどのシステムユーザーに属しているかを決定することです.2つ目は、プロセスがこれらのシステムリソースにアクセスするときにどのような権限制御を付与すべきかです.たとえば、各ファイルは特定のユーザーとグループに属し、各プロセスには、プロセスとこれらのプロセスがファイルにアクセスするときにどのような権限を持っているかを決定するユーザーとグループIDsがあります.
この章では、/etc/passwd、/etc/groupdなどのシステムファイルについて検討します.これらのファイルには、システムのユーザーとグループの情報が定義されています.次に、これらのファイルからユーザー情報を取得するライブラリ関数について説明します.終了時にはcrypt()関数も紹介します.これはログインパスワードを暗号化および認証するために使用されます.
システムのパスワードファイルは/etc/passwdで、ユーザーのアカウント情報が含まれており、一行はユーザーを表しています.各行は半角のセミコロンで区切られ、7つのフィールドがあります.
lavenliu:x:1000:1000:Laven Liu:/home/lavenliu:/bin/bash
各フィールドの意味を順に説明します.
1. - lavenliu
2. - x
3. UID - 1000
4. GID - 1000
5. - Laven Liu
6. - /home/lavenliu
7. shell - /bin/bash
getpwnam関数の簡単な使用、
[root@python users_groups]# cat my_getpwnam.c
#include
#include
#include
int main(int argc, char *argv[])
{
struct passwd *pwd;
if (argc
", argv[0]);
exit(1);
}
pwd = getpwnam(argv[1]);
if (pwd == NULL) {
printf("could not get %s record
", argv[1]);
exit(1);
} else {
printf("find [ %s ] record, the following is the info:
", argv[1]);
printf("Username: %s
", pwd->pw_name);
printf("Uid : %ld
", (long)pwd->pw_uid);
printf("Shell : %s
", pwd->pw_shell);
}
return 0;
}
コンパイルして実行し、
[root@python users_groups]# gcc -g -o my_getpwnam my_getpwnam.c
[root@python users_groups]# ./my_getpwnam
Usage: ./my_getpwnam
[root@python users_groups]# ./my_getpwnam root
find [ root ] record, the following is the info:
Username: root
Uid : 0
Shell : /bin/bash
[root@python users_groups]# ./my_getpwnam www
could not get www record
[root@python users_groups]# ./my_getpwnam lavenliu
find [ lavenliu ] record, the following is the info:
Username: lavenliu
Uid : 500
Shell : /bin/bash
[root@python users_groups]# ./my_getpwnam taoqi
find [ taoqi ] record, the following is the info:
Username: taoqi
Uid : 517
Shell : /bin/bash