本文內容來源於Unix高級編程,歡迎評論。
口令文件 /etc/passwd
passwd
結構包含用戶名、密碼、用戶ID、組ID等信息,Linux在該結構中定義了下列結構:
char *pw_name;
char *pw_passwd;
uid_t pw_uid;
gid_t pw_gid;
char *pw_gecos; //註釋字段
char *pw_dir;
char *pw_shell;
如下函數通過uid或用戶名來獲取用戶的passwd
信息:
#include <pwd.h>
struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);
// 成功,返回指針;出錯,返回NULL
下列函數可以用來查看整個口令文件:
#include <pwd.h>
struct passwd *getpwent(void);
// 成功,返回口令文件下一個記錄項指針;若出錯或到達文件尾,返回NULL
void setpwen(); // 將getpwent指向口令文件開頭
void endpwent(); // 關閉相關的文件
陰影口令 /etc/shadow
陰影口令包含了加密的用戶登陸信息,相關結構體爲struct spwd
,包含如下成員:
struct spwd {
char *sp_namp; // Login name.
char *sp_pwdp; // Encrypted password.
long int sp_lstchg; // Date of last change.
long int sp_min; // Minimum number of days between changes.
long int sp_max; // Maximum number of days between changes.
long int sp_warn; // Number of days to warn user to change the password.
long int sp_inact; // Number of days the account may be inactive.
long int sp_expire; // Number of days since 1970-01-01 until account expires.
unsigned long int sp_flag; // Reserved.
};
與口令文件類似,下列函數可用於訪問陰影口令文件:
#include <shadow.h>
struct spwd *getspent (void);
struct spwd *getspnam (const char *__name);
void setspent (void);
void endspent (void);
組文件 /etc/group
組文件結構包含如下字段:
struct group{
char *gr_name; // Group name.
char *gr_passwd; // Password.
__gid_t gr_gid; // Group ID.
char **gr_mem; // Member list.
};
其中gr_mem
是一個指針數組,每一項都指向一個屬於該組的用戶名。與口令文件類似,下列函數可以用來獲取組相關信息:
struct group *getgrgid (__gid_t __gid);
struct group *getgrnam (const char *__name);
void setgrent (void);
void endgrent (void);
struct group *getgrent (void);
附屬組 ID
附屬組,即每個進程不僅可以屬於口令文件中規定的組,也可以屬於最多16個其它組。下列函數用來獲取附屬組ID:
#include <unistd.h>
#include <grp.h>
int getgroups(int gidsetsize, git_t grouplist[]);
// 返回附屬組ID數量,出錯返回-1
int setgroups(int ngroups, const gid_t grouplist[]);
int initgroups(const char *username, gid_t basegid);
// 成功,返回0,出錯,返回-1
其它數據文件
說明 | 數據文件 | 頭文件 | 結構 |
---|---|---|---|
口令 | passwd | pwd.h | passwd |
組 | group | grp.h | group |
陰影 | shadow | shadow.h | spwd |
主機 | hosts | netdb.h | hostent |
網絡 | networks | netdb.h | netent |
協議 | protocols | netdb.h | protoent |
服務 | services | netdb.h | servent |
登陸賬戶記錄
參考utmp(5),相關頭文件爲utmp.h
,相關記錄存放在/var/run/utmp
與/var/log/wtmp
中。
系統標識
utsname
結構中包含相關信息。
struct utsname{
char sysname[_UTSNAME_SYSNAME_LENGTH]; // Name of the implementation of the operating system.
char nodename[_UTSNAME_NODENAME_LENGTH]; // Name of this node on the network.
char release[_UTSNAME_RELEASE_LENGTH]; // Current release level of this implementation.
char version[_UTSNAME_VERSION_LENGTH]; // Current version level of this release.
char machine[_UTSNAME_MACHINE_LENGTH]; // Name of the hardware type the system is running on.
char domainname[_UTSNAME_DOMAIN_LENGTH];
};
下列函數提供了獲取系統標識的相關數據:
#include <sys/utsname.h>
int uname(struct utsname *name);
// 成功,返回非負值,出錯,返回-1
#include <unistd.h>
int gethostname(char *name, int namelen);
// 成功,返回0,出錯,返回-1
時間和日期
time
函數返回當前時間
#include <time.h>
time_t time(time_t *calptr);
// 成功,返回時間,出錯,返回-1
int clock_gettime(clockid_t clock_id, struct timespec *tsp); // 獲取指定時鐘的時間
// 成功,返回0,出錯,返回-1
下列函數用於轉換時間,並將結果存放到tm
結構中:
struct tm{
int tm_sec; // Seconds. [0-60] (1 leap second)
int tm_min; // Minutes. [0-59]
int tm_hour; // Hours. [0-23]
int tm_mday; // Day. [1-31]
int tm_mon; // Month. [0-11]
int tm_year; // Year - 1900.
int tm_wday; // Day of week. [0-6]
int tm_yday; // Days in year.[0-365]
int tm_isdst; // DST. [-1/0/1]
long int tm_gmtoff; // Seconds east of UTC.
const char *tm_zone; // Timezone abbreviation.
};
#include <time.h>
struct tm *gmtime (const time_t *__timer); // UTC(Universal Time of Coordinate)協調統一時間
struct tm *localtime (const time_t *__timer); // 本地時間
// 成功,返回指針,出錯,返回NULL
time_t mktime(struct tm *tmptr); // 將年月日等參數轉換爲time_t
// 成功,返回time_t,出錯,返回-1
函數strftime
類似於printf
,通過參數來控制產生的字符串,參考strftime(3),而strptime
將字符串轉換爲時間:
#include <time.h>
size_t strftime (char *__restrict __s, size_t __maxsize, const char *__restrict __format, const struct tm *__restrict __tp);
size_t strftime_l (char *__restrict __s, size_t __maxsize, const char *__restrict __format, const struct tm *__restrict __tp, locale_t __loc)
// 成功,返回存入數組的字符數,否則,返回0
// Parse S according to FORMAT and store binary time information in TP.
// The return value is a pointer to the first unparsed character in S.
// 根據 fmt 解析 s,並將結果存入 tp中
char *strptime (const char *__restrict __s, const char *__restrict __fmt, struct tm *__tp);
// 成功,返回指向第一個未被解析字符的指針,否則,返回NULL
Linux設置時區
通過如下命令可以設置時區,其中GMT+6
可以替換爲其它的,另外也可以設置爲/usr/share/zoneinfo/
中的其它文件
sudo unlink /etc/localtime
sudo ln -s /usr/share/zoneinfo/Etc/GMT+6 /etc/localtime