Linux基本命令(三)
用户和用户组的常用命令:
用户:
eg:
[root@Hadoop ~]# ll /usr/sbin/user*
-rwxr-x---. 1 root root 103096 Dec 8 2011 /usr/sbin/useradd(添加)
-rwxr-x---. 1 root root 69560 Dec 8 2011 /usr/sbin/userdel(删除)
-rws--x--x. 1 root root 42384 Aug 23 2010 /usr/sbin/userhelper
-rwxr-x---. 1 root root 98680 Dec 8 2011 /usr/sbin/usermod(修改)
-rwsr-xr-x. 1 root root 9000 Nov 23 2013 /usr/sbin/usernetctl
用户组:
eg:
[root@Hadoop ~]# ll /usr/sbin/group*
-rwxr-x---. 1 root root 54968 Dec 8 2011 /usr/sbin/groupadd(添加)
-rwxr-x---. 1 root root 46512 Dec 8 2011 /usr/sbin/groupdel(删除)
-rwxr-x---. 1 root root 50800 Dec 8 2011 /usr/sbin/groupmems
-rwxr-x---. 1 root root 61360 Dec 8 2011 /usr/sbin/groupmod(修改)
查看用户:
eg:
[root@Hadoop ~]# id xk
uid=500(xk) gid=500(xk) groups=500(xk)
vim /etc/passwd
hadoop:x:1001:1001::/home/hadoop:/bin/bash
[root@xkhadoop ~]# cat /etc/group |grep hadoop
hadoop:x:1001:
gid:主组
groups:所有组
给用户改密码:
eg:
passwd xk :给xk 这个用户改密码
切换用户:
1.su xx(用户)
2.su - xx(用户)
-:执行切换用户的环境变量且 进入到家目录
临时获取root最大权限(****):
sudo +xxx(想要执行的代码,不支持环境变量)
先进入到root用户
修改/etc/sudoers,为一个用户加sudo权限,添加一行
xk ALL=(ALL) NOPASSWD:ALL
eg:
[xk@Hadoop ~]$ sudo ls -l /root
total 112
-rw-------. 1 root root 1354 Sep 9 00:03 anaconda-ks.cfg
drwxr-x---. 2 root root 4096 Sep 9 00:09 anaconda-screenshots
drwxr-xr-x. 2 root root 4096 Sep 9 01:05 Desktop
drwxr-xr-x. 2 root root 4096 Sep 9 01:05 Documents
drwxr-xr-x. 2 root root 4096 Sep 9 01:05 Downloads
-rw-r--r--. 1 root root 47358 Sep 9 00:03 install.log
-rw-r--r--. 1 root root 10033 Sep 9 00:01 install.log.syslog
drwxr-xr-x. 2 root root 4096 Sep 9 01:05 Music
drwxr-xr-x. 2 root root 4096 Sep 9 01:05 Pictures
drwxr-xr-x. 2 root root 4096 Sep 9 01:05 Public
drwxr-xr-x. 2 root root 4096 Sep 9 01:05 Templates
drwxr-xr-x. 3 root root 4096 Sep 12 07:34 text
drwxr-xr-x. 2 root root 4096 Sep 9 01:05 Videos
-rw-r--r--. 1 root root 4 Oct 21 14:17 xk.log
这样查看进程?
ps-ef
eg:
[root@Hadoop ~]# ps -ef | grep ntp
ntp 1870 1 0 19:33 ? 00:00:00 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
root 3286 2628 0 21:08 pts/0 00:00:00 grep ntp
root:是运行的user
3286:PID(类似于唯一主键)
1:父ID
杀死一个进程:
kill -9 +进程号
eg:
[root@Hadoop ~]# kill -9 1870
[root@Hadoop ~]# ps -ef | grep ntp
root 3440 2628 0 21:57 pts/0 00:00:00 grep ntp
查看一个服务是否开启:
service 服务名 status
eg:
[root@Hadoop ~]# service ntpd status
ntpd dead but pid file exists
开启一个服务:
service 服务名 start
eg:
[root@Hadoop ~]# service ntpd start
Starting ntpd: [ OK ]
[root@Hadoop ~]# ps -ef | grep ntp
ntp 3466 1 0 21:59 ? 00:00:00 ntpd -u ntp:ntp -p /var/run/ntpd.pid -g
root 3470 2628 0 21:59 pts/0 00:00:00 grep ntp
service 是centos6.x的功能
systemctl是centos7.x新特性,后面可以跟多个服务
eg:
systemctl start/status... xxx(服务器名称)
[root@xkhadoop ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: man:httpd(8)
man:apachectl(8)
怎样查看端口号?
1.
netstat -nlp | grep +进程的PID
[root@Hadoop ~]# netstat -nlp | grep 3466
udp 0 0 192.168.40.129:123 0.0.0.0:* 3466/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 3466/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 3466/ntpd
udp 0 0 fe80::250:56ff:fe2f:b8dc:123 :::* 3466/ntpd
udp 0 0 ::1:123 :::* 3466/ntpd
udp 0 0 :::123 :::* 3466/ntpd
2.netstat -nlp | grep +服务名
eg:
[root@Hadoop ~]# netstat -nlp | grep ntp
udp 0 0 192.168.40.129:123 0.0.0.0:* 3466/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 3466/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 3466/ntpd
udp 0 0 fe80::250:56ff:fe2f:b8dc:123 :::* 3466/ntpd
udp 0 0 ::1:123 :::* 3466/ntpd
udp 0 0 :::123 :::* 3466/ntpd
3.查看端口是否开启:telnet IP port
安装下temnet
[root@xkhadoop ~]# telnet
bash: telnet: 未找到命令...
[root@xkhadoop ~]# yum install -y telnet
[root@xkhadoop ~]# telnet localhost 22
Trying ::1...
Connected to localhost.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4
案例:一台服务器的一个服务无法访问
以下是查询正常的服务
[root@xkhadoop ~]# netstat -nlp|grep ssh
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1254/sshd
tcp6 0 0 :::22 :::* LISTEN 1254/sshd
unix 2 [ ACC ] STREAM LISTENING 34984 2203/gnome-keyring- /run/user/1001/keyring/ssh
unix 2 [ ACC ] STREAM LISTENING 34634 2357/ssh-agent /tmp/ssh-sB22KPDcChdF/agent.2216
有可能是因为0 0.0.0.0:22给的是127.0.0.1:22导致的服务无法访问,则需要修改下这个服务的配置文件:
[root@xkhadoop ~]# cd /etc/httpd/
[root@xkhadoop httpd]# ll
总用量 0
drwxr-xr-x. 2 root root 37 11月 19 23:34 conf
drwxr-xr-x. 2 root root 82 11月 19 23:34 conf.d
drwxr-xr-x. 2 root root 146 11月 19 23:34 conf.modules.d
lrwxrwxrwx. 1 root root 19 11月 19 23:34 logs -> ../../var/log/httpd
lrwxrwxrwx. 1 root root 29 11月 19 23:34 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx. 1 root root 10 11月 19 23:34 run -> /run/httpd
[root@xkhadoop httpd]# cd conf
[root@xkhadoop conf]# ll
总用量 28
-rw-r--r--. 1 root root 11753 8月 6 21:44 httpd.conf
-rw-r--r--. 1 root root 13077 8月 8 19:42 magic
[root@xkhadoop conf]# vim httpd.conf
#Listen 12.34.56.78:80
Listen 80
权限(*****):
读: r 4
写: w 2
执行:x 1
-:无权限,占位
eg:
先执行ll命令查看用户根目录下的文件:
[root@xkhadoop ~]# ll
total 104
-rw-------. 1 root root 1354 Oct 27 03:12 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Desktop
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Documents
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Downloads
-rw-r--r--. 1 root root 47358 Oct 27 03:12 install.log
-rw-r--r--. 1 root root 10033 Oct 27 03:09 install.log.syslog
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Music
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Pictures
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Public
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Templates
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Videos
drwxr-xr-x. 2 root root 4096 Oct 27 16:23 xkhadoop
-rw-r--r--. 1 root root 0 Oct 27 16:24 xk.log
分析具体内容:
-rw-r--r--. 1 root root 0 Oct 27 16:24 xk.log
第一位“-”代表是文件还是其他类型
d:代表是一个文件夹
-:代表是一个文件
l:代表是一个链接
第一组:rw-:代表root这个用户对这个文件(或者其他类型)有读和写的权限
第二组:r--:代表root这个用户组里面的用户对这个文件只有读的权限
第三组:r--:代表其他用户组的用户对这个文件只有读的权限
chown:修改文件或者文件夹的额用户及用户组
chown -R (用户):(用户组) +文件夹
chown (用户):(用户组) +文件
eg:
文件夹:
[root@xkhadoop ~]# chown -R xk:xk /root/xkhadoop/
[root@xkhadoop ~]# ll
total 104
-rw-------. 1 root root 1354 Oct 27 03:12 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Desktop
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Documents
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Downloads
-rw-r--r--. 1 root root 47358 Oct 27 03:12 install.log
-rw-r--r--. 1 root root 10033 Oct 27 03:09 install.log.syslog
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Music
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Pictures
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Public
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Templates
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Videos
drwxr-xr-x. 2 xk xk 4096 Oct 27 16:23 xkhadoop
-rw-r--r--. 1 root root 0 Oct 27 16:24 xk.log
文件:
[root@xkhadoop ~]# chown xk:xk /root/xk.log
[root@xkhadoop ~]# ll
total 104
-rw-------. 1 root root 1354 Oct 27 03:12 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Desktop
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Documents
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Downloads
-rw-r--r--. 1 root root 47358 Oct 27 03:12 install.log
-rw-r--r--. 1 root root 10033 Oct 27 03:09 install.log.syslog
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Music
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Pictures
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Public
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Templates
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Videos
drwxr-xr-x. 2 xk xk 4096 Oct 27 16:23 xkhadoop
-rw-r--r--. 1 xk xk 0 Oct 27 16:24 xk.log
chmod:修改文件或者文件夹的权限
rw-r--r-- ==》644
chmod -R +(改后权限数字) +文件夹地址
chmod +x +文件地址:相当于三种权限都加上这个x(数字)
chmod +(改后权限数字) +文件地址
文件夹:
eg:
[root@xkhadoop ~]# chmod -R 777 /root/xkhadoop
[root@xkhadoop ~]# ll
total 104
-rw-------. 1 root root 1354 Oct 27 03:12 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Desktop
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Documents
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Downloads
-rw-r--r--. 1 root root 47358 Oct 27 03:12 install.log
-rw-r--r--. 1 root root 10033 Oct 27 03:09 install.log.syslog
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Music
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Pictures
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Public
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Templates
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Videos
drwxrwxrwx. 2 xk xk 4096 Oct 27 16:23 xkhadoop
-rw-r--r--. 1 xk xk 0 Oct 27 16:24 xk.log
文件:
eg:
[root@xkhadoop ~]# chmod 777 /root/xk.log
[root@xkhadoop ~]# ll
total 104
-rw-------. 1 root root 1354 Oct 27 03:12 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Desktop
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Documents
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Downloads
-rw-r--r--. 1 root root 47358 Oct 27 03:12 install.log
-rw-r--r--. 1 root root 10033 Oct 27 03:09 install.log.syslog
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Music
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Pictures
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Public
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Templates
drwxr-xr-x. 2 root root 4096 Oct 27 03:54 Videos
drwxrwxrwx. 2 xk xk 4096 Oct 27 16:23 xkhadoop
-rwxrwxrwx. 1 xk xk 0 Oct 27 16:24 xk.log
安装rpm包:
eg:
yum安装:
yum install httpd
service +(服务名) status|start|stop|restart
卸载rpm包:
rpm -qa | grep +服务名:搜索需要卸载的rpm包
eg:
[root@xkhadoop ~]# rpm -qa | grep http
httpd-tools-2.2.15-69.el6.centos.x86_64
httpd-2.2.15-69.el6.centos.x86_64
rpm --nodeps -e +(名称):不查看其依赖关系卸载
--nodeps 不验证包的依赖关系 强行卸载
eg:
[root@xkhadoop ~]# rpm --nodeps -e httpd-tools-2.2.15-69.el6.centos.x86_64
[root@xkhadoop ~]# rpm -qa | grep http
httpd-2.2.15-69.el6.centos.x86_64
命令或者某个文件在哪?
which xxx(搜索命令)
locate xxx(搜索命令)
find xxx(搜索文件)
eg:
which:
[root@xkhadoop ~]# which useradd
/usr/sbin/useradd
locate:
[root@xkhadoop ~]# locate useradd
/etc/default/useradd
/usr/sbin/luseradd
/usr/sbin/useradd
/usr/share/man/fr/man8/useradd.8.gz
/usr/share/man/id/man8/useradd.8.gz
/usr/share/man/it/man8/useradd.8.gz
/usr/share/man/ja/man8/useradd.8.gz
/usr/share/man/man1/luseradd.1.gz
/usr/share/man/man8/useradd.8.gz
/usr/share/man/overrides/man8/useradd.8.gz
/usr/share/man/pl/man8/useradd.8.gz
/usr/share/man/ru/man8/useradd.8.gz
/usr/share/man/sv/man8/useradd.8.gz
/usr/share/man/tr/man8/useradd.8.gz
/usr/share/man/zh_CN/man8/useradd.8.gz
/usr/share/man/zh_TW/man8/useradd.8.gz
find:(不加/root(地址)默认是全局搜索)
[root@xkhadoop ~]# find /root -name "*log"
/root/.imsettings.log
/root/install.log.syslog
/root/install.log
/root/xk.log
查看硬盘:
df -h
eg:
[root@xkhadoop ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 38G 3.1G 33G 9% / (从根目录开始说明这个磁盘是系统盘)
tmpfs 936M 80K 936M 1% /dev/shm
/dev/sda1 124M 34M 84M 29% /boot
/dev/sr0 4.2G 4.2G 0 100% /media/CentOS_6.5_Final
查看内存:
free -m
eg:
[root@xkhadoop ~]# free -m
total used free shared buffers cached
Mem: 1870 765 1105 0 40 447
-/+ buffers/cache: 276 1593
Swap: 2047 0 2047
memory详细情况:
http://blog.itpub.net/30089851/viewspace-2131678/
实时查看系统情况:
top
top - 23:28:44 up 1:11, 2 users, load average: 0.00, 0.01, 0.05
Tasks: 210 total, 1 running, 207 sleeping, 2 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.3 sy, 0.0 ni, 99.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1863076 total, 117036 free, 777092 used, 968948 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 903652 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4394 root 20 0 162144 2400 1584 R 0.3 0.1 0:00.04 top
1 root 20 0 128380 6972 4204 S 0.0 0.4 0:01.75 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
6 root 20 0 0 0 0 S 0.0 0.0 0:00.13 ksoftirqd/0
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
主要查看:
load average: 0.00, 0.01, 0.05:可能会影响你的机器是否会丢数据,正常物理机10以上就会出现丢数据,具体情况要根据自己机器性能来测试
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
4394 root 20 0 162144 2400 1584 R 0.3 0.1 0:00.04 top
1 root 20 0 128380 6972 4204 S 0.0 0.4 0:01.75 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.01 kthreadd
看下是否有CPU和MEMORY的异常使用率:可能是代码级别的问题也可能是硬件方面的问题,如果代码级别没什么问题,那就重启下机器,看是否可以启动,如果无法正常启动则说明是硬件方面的问题
怎样下载软件包:
wget +(地址)
eg:
[root@xkhadoop ~]# wget https://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-2.9.0/hadoop-2.9.0.tar.gz
文件压缩:
tar -czf +压缩包名(eg:xxx.tar.gz)+需要压缩的文件
eg:
[root@xkhadoop ~]# tar -czf 1.tar.gz *.log
zip +压缩包名(eg:xxx.zip)+需要压缩的文件
eg:
[root@xkhadoop ~]# zip 2.zip *.log
adding: install.log (deflated 77%)
adding: xk.log (stored 0%)
压缩包解压(***):
tar -xzvf +压缩包名称
eg:
[root@xkhadoop ~]# tar -xzvf 1.tar.gz
install.log
xk.log
(参数v:实时打印出解压的文件)
unzip +压缩包名称
eg:
[root@xkhadoop ~]# unzip 2.zip
Archive: 2.zip
replace install.log? [y]es, [n]o, [A]ll, [N]one, [r]ename:
(输出A)
replace install.log? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
inflating: install.log
extracting: xk.log