查看当前用户的登录名
[root@VM-4-2-centos ~]# whoami
root
查看当前用户登录信息
[root@VM-4-2-centos ~]# who
root pts/0 2022-04-17 16:03 (211.14.69.17)
查看当前活跃用户
[root@VM-4-2-centos ~]# w
16:09:48 up 2:39, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 211.14.69.17 16:03 4.00s 0.04s 0.00s w
# TTY: pts表示远程终端,多个终端:pts/0,pts/1,pts/2...
# TTY: tty表示本地登录,即在真实机器上登录
查看系统用户列表
# 查看所有用户的列表 cat /etc/passwd
[root@VM-4-2-centos ~]# cat /etc/passwd | grep 'root'
root:x:0:0:root:/root:/bin/bash
# 以冒号为分隔,索引从1开始,依次为:1:2:3:4:5:6:7
1 用户名:root
2 密码:x,表示加密密码保存在
/etc/shadow
文件中,只有root权限才能查看/etc/shadow
3 用户 ID:0
4 群组 ID:0,保存在
/etc/group
文件中5 用户信息:root,描述用户的信息
6 用户家目录:
/home/root
7 Shell:/bin/bash,代表用户使用的shell类型
print (username, UID, GID) with awk command
[root@VM-4-2-centos home]# awk -F':' '{print $1, $3, $4}' /etc/passwd | grep 'postgres'
postgres 1001 888
[root@VM-4-2-centos home]# awk -F':' '{print $1 "|" $3 "|" $4}' /etc/passwd | grep 'postgres'
postgres|1001|888
添加用户
第一步:新建用户所属的组
groupadd用于创建一个新的工作组,新工作组的信息将被添加到系统文件中。
# 语法:groupadd(选项)(参数)
groupadd -g 888 postgres # 新建工作组“postgres”,设置组ID为888,将ID加入系统
-g 指定新建工作组的id
第二步:为新组新建用户
useradd 选项 用户名
-g 指定用户所属的用户组
-s 指定用户的登录Shell
useradd -g postgres -s /bin/bash -m postgres
# 创建用户名为“postgres”的用户,所属组为“postgres”,使用/bin/bash登录
第三步:为用户设置UID
# usermod -u 888 postgres
第四步:查看用户信息(方式一)
[root@VM-4-2-centos ~]# cat /etc/passwd | grep postgres
postgres:x:888:888::/home/postgres:/bin/bash
第四步:查看用户信息(方式二)
[root@VM-4-2-centos ~]# su - postgres
[postgres@VM-4-2-centos ~]$ id
uid=888(postgres) gid=888(postgres) groups=888(postgres)
删除用户
[root@VM-4-2-centos ~]# ls /home
lighthouse postgres
[root@VM-4-2-centos home]# userdel -r postgres
[root@VM-4-2-centos home]# ls /home/
lighthouse
删除组
# 删除之前的查询
[root@VM-4-2-centos ~]# getent group | grep postgres
postgres:x:888:
# 删除组"postgres"
[root@VM-4-2-centos ~]# groupdel postgres
# 验证是否成功删除组“postgres”
[root@VM-4-2-centos ~]# getent group | grep postgres
删除用户密码
# passwd -d user1
passwd: password expiry information changed
修改用户访问文件/文件夹权限
[root@VM-4-2-centos postgres]# ls -la
total 24
drwx------ 19 postgres 1003 4096 Apr 17 15:30 db12tde1
-rw------- 1 1001 1003 1882 Apr 17 12:56 logfile
-rw-r--r-- 1 postgres 1003 49 Mar 18 16:28 provide_key.sh
# 文件夹db12tde1的可访问用户为postgres,用户postgres属于组1003
# 文件logfile的可访问用户为1001,用户1001属于组1003
# 将目录db12tde1中所有文件的所有者和组更改为用户postgresuser,组postgresgroup:
chown -R postgresuser:postgresgroup db12tde1
[root@VM-4-2-centos postgres]# ls -la
total 24
drwx------ 19 postgresuser postgresgroup 4096 Apr 17 15:30 db12tde1
-rw------- 1 1001 1003 1882 Apr 17 12:56 logfile
-rw-r--r-- 1 postgres 1003 49 Mar 18 16:28 provide_key.sh
切换用户登录
# 变更帐号为 clsung 并改变工作目录至 clsung 的家目录(home dir)
su - clsung
[root@moapp-0003 ~]# whoami
root
[root@moapp-0003 ~]# pwd
/root
[root@moapp-0003 ~]# su - postgres // 切换到 postgres 用户
Last login: Mon Aug 16 22:08:09 CST 2021 on pts/0
[postgres@moapp-0003 ~]$ whoami // 显示当前用户
postgres
[postgres@moapp-0003 ~]$ pwd // 显示当前目录,目录已变为 postgres 用户的家目录
/home/postgres
su - postgres -c "/usr/local/pg12tde/bin/pg_ctl -D /home/postgres/db12tde1 start"
# su - postgress // 切换到 postgres 用户,目录切换至 /home/postgress
-c command 变更账号后执行命令