Linux MISC.
Linux CPU,内存,磁盘,防火墙…
查看所有进程
[test@localhost ~]$ ps -ef
显示Linux进程
[test@localhost ~]$ top
查看指定目录的大小
[test@localhost ~]$ du -sh
查看活动用户
[test@localhost ~]$ w
查看用户登录历史
[test@localhost ~]$ last
查看用户id,组id等信息
[test@localhost ~]$ id
运行时间、用户数、负载
[test@localhost ~]$ uptime
主机名
[test@localhost ~]$ hostname
查看内核/操作系统/CPU架构/Linux系统信息命令
[test@localhost ~]$ uname -a
查看环境变量资源
[test@localhost ~]$ env
查看所有安装的软件包
[test@localhost ~]$ rpm -qa
查看系统服务
[test@localhost ~]$ chkconfig --list
当前用户计划任务
[test@localhost ~]$ crontab -l
列出所有PCI设备
[test@localhost ~]$ lspci -tv
列出所有USB设备
[test@localhost ~]$ lsusb -tv
列出加载的内核模块
[test@localhost ~]$ lsmod
查看IO端口相关信息
[test@localhost ~]$ cat /proc/ioports
查看挂载的文件系统信息
[test@localhost ~]$ mount | column -t
Linux 用户
sudo
配置<user>
用户 sudo 免密码,将 <user> ALL=(ALL) NOPASSWD: ALL
添加到文件末尾即可。
例如: test 用户
1 | [root@localhost ~]# visudo |
免密登录
1.生成密钥对
1 | [test@localhost ~]$ ssh-keygen -t rsa |
2.添加已认证
1 | #默认 cd $HOME/.ssh |
注意,文件authorized_keys
的权限是600
。
3.客户端配置使用私钥(id_rsa
)登录即可
FAQ:
ERROR: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
(root权限操作)
修改/etc/ssh/sshd_config
文件,确保有:
1 | PubkeyAuthentication yes |
重启sshd服务:
1 | [root@localhost ~]# systemctl status sshd |
修改用户Home目录
/etc/passwd
root❌0:0:root:/root:/bin/bash
test❌1000:1000:test:/home/test:/bin/bash
用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录Shell
1 | 用法:usermod [选项] 登录 |
1 | [root@localhost ~]# usermod -l newLoginUserName oldLoginUserName |
/etc/shadow是在安装了影子(shadow)口令软件的系统上的影子口令文件。
/etc/group 存储有关本地用户组的信息。
添加/冻结用户
添加用户
[root@localhost ~]# useradd <user> -d <user_home>
修改密码为pwd
[root@localhost ~]# passwd <user>
冻结一个用户 (即密码冻结,让用户无法登陆)
1 | [root@localhost ~]# passwd -l username |
密码冻结解开:
[root@localhost ~]# passwd -u username
Linux CPU
查看CPU相关参数
[test@localhost ~]$ cat /proc/cpuinfo
判断物理CPU的个数可以查看physical id 的值,相同则为同一个。flags 如果有 ht 说明支持超线程技术
[test@localhost ~]$ cat /proc/cpuinfo |grep "model name" && cat /proc/cpuinfo |grep "physical id"
linux MEM
查看内存相关信息
[test@localhost ~]$ free -h
[test@localhost ~]$ /proc/meminfo
[test@localhost ~]$ cat /proc/meminfo |grep MemTotal
linux buff/cache过大,清理方法
执行:
1 | sync |
注意:sync 命令将所有未写的系统缓冲区写到磁盘中,确保文件系统的完整性。
1 表示清除pagecache。
2 表示清除回收slab分配器中的对象(包括目录项缓存和inode缓存)。slab分配器是内核中管理内存的一种机制,其中很多缓存数据实现都是用的pagecache。
3 表示清除pagecache和slab分配器中的缓存对象。
drop_caches
Writing to this will cause the kernel to drop clean caches, as well as
reclaimable slab objects like dentries and inodes. Once dropped, their
memory becomes free.To free pagecache:
echo 1 > /proc/sys/vm/drop_caches
To free reclaimable slab objects (includes dentries and inodes):
echo 2 > /proc/sys/vm/drop_caches
To free slab objects and pagecache:
echo 3 > /proc/sys/vm/drop_cachesThis is a non-destructive operation and will not free any dirty objects.
To increase the number of objects freed by this operation, the user may run
sync
prior to writing to /proc/sys/vm/drop_caches. This will minimize the
number of dirty objects on the system and create more candidates to be dropped.
Linux Disk
获取分区的UUID
[root@localhost ~]# blkid /dev/sda1
或者 [test@localhost ~]$ ls -l /dev/disk/by-uuid/
查看文件格式
1 | [root@localhost ~]# parted /dev/sda1 |
永久挂载
[root@localhost ~]# vi /etc/fstab
查看硬盘分区信息
[test@localhost ~]$ cat /proc/partitions
查看已挂载的磁盘信息
[test@localhost ~]$ df -h
查看所有磁盘(包括已挂载的和未挂载的) root
[root@localhost ~]# fdisk -l
Linux Network
网口信息
[test@localhost ~]$ ifconfig
路由表
[test@localhost ~]$ route -n
查看端口连接情况
[test@localhost ~]$ netstat -nlap
Linux firewall
查看防火墙状态
[root@localhost ~]# firewall-cmd --state
查看防火墙规则
[root@localhost ~]# firewall-cmd --list-all
添加规则
[root@localhost ~]# firewall-cmd --permanent --add-port=9201/tcp
或
[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
参数含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效
重新加载防火墙配置
[root@localhost ~]# firewall-cmd --reload
停止防火墙
[root@localhost ~]# systemctl stop firewalld.service
启动防火墙
[root@localhost ~]# systemctl start firewalld.service
查开机启动状态
[root@localhost ~]# systemctl list-unit-files |grep fire
开机启动
[root@localhost ~]# systemctl enable firewalld.service
停止开机启动
[root@localhost ~]# systemctl disable firewalld.service
参考
- Linux Manual.