System data files and information

Password file

linux in passwd The structure contains 7 of the 10 fields, and each field uses:separate

user name:encrypted password:user ID:group ID:comment field :Initial working directory:initial shell
yzq:x:1000:1000:,,,:/home/yzq:/bin/bash

In the order of the fields above, the passwd structural morphology
struct passwd {
	char   *pw_name;       /* username */
    char   *pw_passwd;     /* user password */
    uid_t   pw_uid;        /* user ID */
    gid_t   pw_gid;        /* group ID */
    char   *pw_gecos;      /* user information */
    char   *pw_dir;        /* home directory */
    char   *pw_shell;      /* shell program */
};

getpwuid,getpwname

//Pass in a user id and return the details of the user
struct passwd *getpwuid(uid_t uid);
//Pass in a user name and return the details of the user
struct passwd *getpwnam(const char *name);

Group file

/ etc/group in the root directory is the group file, which is used to save all the information of the user group.

linux in group Structure has four fields

Group name:encrypted password:group ID:An array of pointers to each user name
yzq:x:1000:

There is also a structure
struct group {
	char   *gr_name;        /* group name */
    char   *gr_passwd;      /* group password */
    gid_t   gr_gid;         /* group ID */
    char  **gr_mem;         /* NULL-terminated array of pointers
                               to names of group members */
};

getgrgid,getgrgrnam

//Query all information of the group through gid
struct group *getgrgid(gid_t gid);
//Query all information of the group by name
struct group *getgrnam(const char *name);

shadow password

/etc/shadow file is used to store the password information of users in Linux system, also known as "shadow file". The / etc/passwd file was introduced earlier. Since the file allows all users to read, it is easy to cause user password disclosure. Therefore, the Linux system separates the user's password information from the / etc/passwd file and puts it separately into the shadow file.
Like the / etc/passwd file, each line in the file represents a user, and the ":" is also used as the separator. The difference is that each line of user information is divided into 9 fields.

User name: encryption password: last modification time: minimum modification interval: password validity: warning days before password change: Grace time after password Expiration: account expiration time: reserved

yzq:$6$N6IhIX8foeKuzK.6$sgX.nRp5kAjzv2tbldw.4HQt9wSMACx9JCiNmwS6xoysJIfbIGFhLRlujhiu0MrSk1OMOpGxXOR/r2367lzto.:18924:0:99999:7:::

struct spwd {
	char *sp_namp;     /* Login name */
    char *sp_pwdp;     /* Encrypted password */
    long  sp_lstchg;   /* Date of last change
                                     (measured in days since
                                     1970-01-01 00:00:00 +0000 (UTC)) */
    long  sp_min;      /* Min # of days between changes */
    long  sp_max;      /* Max # of days between changes */
    long  sp_warn;     /* # of days before password expires
                                     to warn user to change it */
    long  sp_inact;    /* # of days after password expires
                                     until account is disabled */
    long  sp_expire;   /* Date when account expires
                                     (measured in days since
                                     1970-01-01 00:00:00 +0000 (UTC)) */
     unsigned long sp_flag;  /* Reserved */
};

getspnam

Get a line of content in the shadow file through name

struct spwd *getspnam(const char *name);

This function can be used in conjunction with the crypt function (for encryption)

time

time

time_t time(time_t *tloc);

This function returns the number of seconds elapsed since UTC time on January 1, 1970 from 0:0:0 to now
Two uses:

time_t stamp;
time(&stamp);
//Or write it like this
stamp = time(NULL);

If successful, the number of seconds is returned. If failed, the ((time_t)-1) value is returned. The error reason is stored in errno.

gmtime,localtime

Both functions are used to set time_ The content of type T is converted to a tm structure

//The time and date returned by gmtime is UTC time instead of time zone conversion.
struct tm *gmtime(const time_t *timep);
//The time and date returned by localtime has been converted to the local time zone.
struct tm *localtime(const time_t *timep);
struct tm {
	int tm_sec;    /* Seconds (0-60) */
    int tm_min;    /* Minutes (0-59) */
    int tm_hour;   /* Hours (0-23) */
    int tm_mday;   /* Day of the month (1-31) */
    int tm_mon;    /* Month (0-11) */
    int tm_year;   /* Year - 1900 */
    int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
    int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
    int tm_isdst;  /* Daylight saving time */
};

mktime

mktime is used to convert parameters of type tm to time_t type

time_t mktime(struct tm *tm);

Note that the formal parameters of gmtime and localtime are modified with const, indicating that the time will not be modified when the function body will not be converted_ T type. However, there is no const modification here, which means that mktime may modify the content of tm during conversion. Because mktime will first judge whether the time is legal, for example, if the month overflows, it will be adjusted, etc

strftime

Format the date and time, that is, convert the structure type to a format string

size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);

The first two parameters jointly specify a buffer to put up to max contents into s. The content is obtained from tm and is in the format of format.

time_t stamp;
time(&stamp);

struct tm *ptm = localtime(&stamp);
strftime(buf, BUFSIZE, "%Y-%m-%d", ptm);
puts(buf);

Tags: Linux Operation & Maintenance server

Posted on Wed, 03 Nov 2021 14:13:45 -0400 by anon_amos