LinuxC——3.系统属性

LinuxC——3.系统属性

1.❤️passwd口令文件

1.1.什么是口令文件

存放账户信息的文件,就是口令文件

vim /etc/passwd
1.2.文件内容
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
用户名:密码:用户id:用户组id:注释:主目录路径:shell程序路径

真实密码放在 /etc/shadow中

1.3.getpwuid & getpwnam

这两个函数获取账户数据,同样可以读取/etc/passwd也可以

struct passed * getpwuid(const char *name);
struct passed * getpwnam(const char *name);

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 */
};

案例

#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
int main() {
        struct passwd *pw, *pw2;
        pw = getpwnam("root");
  			pw2 = getpwuid(1);

        printf("%s:%s:%d:%d:%s:%s:%s", pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_gecos, pw->pw_dir, pw->pw_shell);
        putchar('\n');
        return 0;
}

🧡2.shadow文件

2.1.shadow文件介绍

用户密码被加密,存放在shadow文件,使用不可逆加密方法

peo:$6$PCcVI2xz$5KEfoyoWuRlwx1LwhWlZjKlVeLDx/rHcA1vBjwS9hhK/HV/6OjhzuKyJxMY7BjKdItfe2jeH/oyis8eqS9JhX/:18361:0:99999:7:::
用户名:密码:日期:多久后可再次修改:账户有效期:...其他字段
2.2.getspnam
struct spwd *getspnam(const char *name)

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 */
};

💛3.组文件

3.1./etc/group

用户权限放在/etc/group下

dockerroot:x:993:
peo:x:1001:
组名:组密码:组id:组员
3.2.getgrnam & getgrgid
struct group *getgrnam(const char *name);
struct group *getgrgid(gid_t gid);


struct group {
  char   *gr_name;       /* group name */
  char   *gr_passwd;     /* group password */
  gid_t   gr_gid;        /* group ID */
  char  **gr_mem;        /* group members */
};

💚4.time函数

1.linux计时方式,从1970.1.1开始,到现在的时间
#include <time.h>
time_t time(time_t *t);

#include <stdio.h>
#include <time.h>
int main() {
        time_t tm = 0;
        time(&tm);
        printf("%ld", tm);
        return 0;
}
2.c库时间

char *ctime(const time_t *timep); //获取时间

struct tm *gmtime(const time_t *timep); //返回国际时间

char *asctime(const struct tm *tm); // 从tm中获取,转换成ctime一样的时间

size_t strftime(char *s, size_t max, const char *format,
                       const struct tm *tm);
strftime(strtim_buf, sizeof(strtim_buf), "%Y %m %d %H %M %S\n", &tm);

time_t mktime(struct tm *tm);

struct tm {
  int tm_sec;         /* seconds */
  int tm_min;         /* minutes */
  int tm_hour;        /* hours */
  int tm_mday;        /* day of the month */
  int tm_mon;         /* month */
  int tm_year;        /* year */
  int tm_wday;        /* day of the week */
  int tm_yday;        /* day in the year */
  int tm_isdst;       /* daylight saving time */
};

测试

#include <stdio.h>
#include <time.h>
int main() {
        time_t tm = 0;
        time(&tm);
        printf("%ld", tm);
        putchar('\n');
        char* ch = ctime(&tm);
        puts(ch);
        return 0;
}
posted @ 2020-04-09 11:18  SteveYu  阅读(215)  评论(0编辑  收藏  举报