组、用户操作
查看用户组
1
cat /etc/group
查看系统中有哪些用户
1
cut -d : -f 1 /etc/passwd
查看所有用户详细信息
1
cat /etc/passwd
查看可以登录系统的用户
1
cat /etc/passwd | grep -v /sbin/nologin | cut -d : -f 1
查看用户操作:w命令(需要root权限)
- 查看某一用户:w 用户名
- 查看登录用户:who
- 查看用户登录历史记录:last
查找用户
1
cat /etc/passwd|grep tomcat
查找组
1
cat /etc/group|grep tomcat
删除用户
1
2
3
4
5
6#强制删除该用户的主目录和主目录下的所有文件和子目录
[root@centos6 /]# userdel -rf tomcatuser
userdel: user tomcatuser is currently used by process 3125
# 退出客户端然后再删除
[root@centos6 /]# userdel -rf tomcatuser
userdel: user 'tomcatuser' does not exist
从组中删除用户
编辑/etc/group 找到GROUP1那一行,删除 A
或者用命令
gpasswd -d A GROUP
删除用户组
1
groupdel
用户和组常用命令
- groups 查看当前登录用户的组内成员
- groups tomcat 查看 tomcat 用户所在的组,以及组内成员
- whoami 查看当前登录用户名
创建用户组
1
groupadd group-java
创建用户
1
useradd -s /bin/bash -g group-java utomcat
创建用户的主要参数
- -c:加上备注文字,备注文字保存在passwd的备注栏中。
- -d:指定用户登入时的启始目录。
- -D:变更预设值。
- -e:指定账号的有效期限,缺省表示永久有效。
- -f:指定在密码过期后多少天即关闭该账号。
- -g:指定用户所属的起始群组。
- -G:指定用户所属的附加群组。
- -m:自动建立用户的登入目录。
- -M:不要自动建立用户的登入目录。
- -n:取消建立以用户名称为名的群组。
- -r:建立系统账号。
- -s:指定用户登入后所使用的shell。
- -u:指定用户ID号。
- 修改用户密码
1
2
3
4
5
6
7
8#usertomcatfor130
usertomcatfor198
[root@centos6 /]# passwd utomcat
Changing password for user utomcat.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
关于删除用户的进一步说明:彻底删除用户
以下以用户 gitadmin 为例。
彻底删除用户,要带参数 -r,否则,只是 userdel gitadmin
的话,只删除了以下四个文件里的该账户和组的信息1
2
3
4/etc/passwd
/etc/shadow
/etc/group/
/etc/gshadow
默认情况下创建一个用户账号,还会创建一个 home 目录和一个用户邮箱(在/var/spool/mail目录以用户名命名)
查看用户信息:1
2
3
4
5cat /etc/passwd | grep gitadmin
cat /etc/shadow | grep gitadmin
cat /etc/group | grep gitadmin
cat /etc/gshadow | grep gitadmin
find / -name "*gitadmin*"
验证->真实执行情况:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20[root@localhost ~]# find / -name "*gitadmin*"
/home/gitadmin
/var/spool/mail/gitadmin
[root@localhost ~]# cat /etc/passwd | grep gitadmin
gitadmin:x:1001:1003::/home/gitadmin:/bin/bash
[root@localhost ~]# cat /etc/shadow | grep gitadmin
gitadmin:$6$9v2xSWUA$wy5ZgD5HIj5FYlBkxX43a7zhMlRz2XinKAI1sQd9z7kjJthvArbZ7W7y9twKzrTI4Ug/JsqyvnsJ3t6O7Z6q60:18141:0:99999:7:::
[root@localhost ~]# cat /etc/group | grep gitadmin
gitadmin:x:1003:
[root@localhost ~]# cat /etc/gshadow | grep gitadmin
gitadmin:!::
[root@localhost ~]# userdel gitadmin
[root@localhost ~]# cat /etc/passwd | grep gitadmin
[root@localhost ~]# cat /etc/shadow | grep gitadmin
[root@localhost ~]# cat /etc/group | grep gitadmin
[root@localhost ~]# cat /etc/gshadow | grep gitadmin
[root@localhost ~]# find / -name "*gitadmin*"
/home/gitadmin
/var/spool/mail/gitadmin
重新添加该用户再执行带参数 -r 的删除:1
2
3
4
5
6
7
8
9
10
11
12[root@localhost ~]# useradd gitadmin
useradd: warning: the home directory already exists.
Not copying any file from skel directory into it.
Creating mailbox file: File exists
[root@localhost ~]# userdel -r gitadmin
userdel: /var/spool/mail/gitadmin not owned by gitadmin, not removing
userdel: /home/gitadmin not owned by gitadmin, not removing
[root@localhost ~]# rm -rf /var/spool/mail/gitadmin
[root@localhost ~]# rm -rf /home/gitadmin
[root@localhost ~]# useradd gitadmin
[root@localhost ~]# userdel -r gitadmin
[root@localhost ~]# find / -name "*gitadmin*" # 已经查不到相关目录了
再次useradd gitadmin
也不会有任何提示了。
Centos 查看硬盘空间
df -h
du -ah –max-depth=1 目录深度为1
-h:以人类可读的方式显示
-a:显示目录占用的磁盘空间大小,还要显示其下目录和文件占用磁盘空间的大小
-s:显示目录占用的磁盘空间大小,不要显示其下子目录和文件占用的磁盘空间大小
-c:显示几个目录或文件占用的磁盘空间大小,还要统计它们的总和
–apparent-size:显示目录或文件自身的大小
-l :统计硬链接占用磁盘空间的大小
-L:统计符号链接所指向的文件占用的磁盘空间大小
linux 清除文本文件内容
快速清空文件内容的几种方法:1
2
3
4
5$ : > filename
$ > filename
$ echo "" > filename
$ echo > filename
$ cat /dev/null > filename
防火墙
关闭
1 | systemctl stop firewalld |
开启
1 | systemctl start firewalld |
关闭开机启动
1 | systemctl disable firewalld |
开启开机启动
1 | systemctl enable firewalld |
Creating mailbox file: File exists
linux下添加用户后,会在系统里自动加一个邮箱(系统邮箱),路径是:/var/spool/mail/用户名.
可以直接用命令#rm -rf /var/spool/mail/用户名
这样再次添加同一名字的用户时,就不会提示这个错误。
the home directory already exists.
1 | useradd: warning: the home directory already exists. |
在执行 userdel 时,用户目录没有被删除,所以,需要手工删除。删除之后再次创建,则不会出现这个错误提示。
cp: omitting directory
目录下面还有目录,不能直接拷贝
解决办法:递归拷贝命令:cp -r bbs ../backup/bbs
解释:-r 这个 options 是递归的意思
删除的时候也可能出现这种下面有文件不能删除的问题,也用 -r 级联删除
如果确定不再需要目录或文件,直接用 rm -rf a # a 可是文件,也可以是一个目录
shell 脚本调用参数
采用 $0,$1,$2..等方式获取脚本命令行传入的参数,
值得注意的是,$0 获取到的是脚本路径以及脚本名,后面按顺序获取参数,当参数超过10个时(包括10个),需要使用${10},${11}….才能获取到参数,但是一般很少会超过 10 个参数的情况。
函数调用时,传递参数也是一样的用法。
shell 脚本里用 curl 会显示统计信息
1 | % Total % Received % Xferd Average Speed Time Time Time Current |
在 curl 后添加参数 -s 可以不显示统计信息
查看 CentOS 版本
cat /etc/redhat-release
修改 CentOS7 IP 地址
1 | cd /etc/sysconfig/network-scripts/ |
找到要修改的文件。如果不是多网卡的主机,一般会有两个文件,如下,其中一个是 127.0.0.1 的,我们需要修改的是另外一个。1
2
3[root@AndyCentOS7Basic network-scripts]# ll *ifcfg*
-rw-r--r-- 1 root root 405 Apr 25 16:06 ifcfg-enp0s3 # 普通网卡
-rw-r--r--. 1 root root 254 May 3 2017 ifcfg-lo # 127.0.0.1
执行 vim ifcfg-enp0s3
以编辑各参数,具体参数如何修改,这里不讨论。
- 重启网络服务以使更改生效:
systemctl restart network
查看文件更新的详细时间
ls --full-time