Linux初始化配置及学用命令技巧
1、新系统初始化脚本(使用时可以根据自己实际情况做适当调整):
#!/bin/bash setenforce 0 getenforce sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config systemctl stop firewalld systemctl disable firewalld cat <<-ENDLIMIT >> /etc/security/limits.conf root soft nofile 102400 root hard nofile 102400 * soft nofile 102400 * hard nofile 102400 ENDLIMIT #ssh_config sed -i 's#GSSAPIAuthentication yes#GSSAPIAuthentication no#g' /etc/ssh/sshd_config echo "UseDNS no" >> /etc/ssh/sshd_config yum -y install epel-release yum -y install ntpdate #时间同步 echo "*/30 * * * * root /usr/sbin/ntpdate -s cn.pool.ntp.org" >> /etc/crontab #sudo_config sed -i 's#!visiblepw#visiblepw#g' /etc/sudoers yum -y update
2、根据服务端口或服务名查找服务进程ID
netstat -ntlp | grep 服务端口号 | awk '{print $7}' | awk -F/ '{print $1}' ps -ef | grep 服务名 |grep -v "grep" | awk '{print $2}' #示例(通过MySql端口号或服务名查看MySql服务进程ID): netstat -ntlp | grep 3306 | awk '{print $7}' | awk -F/ '{print $1}' ps -ef | grep mysql |grep -v "grep" | awk '{print $2}'
3、根据服务进程ID查找进程可执行文件位置
ls -l /proc/进程ID/exe 示例(查找MySql进程可执行文件目录): [root@web-test ~]# ps -ef | grep mysql |grep -v "grep" | awk '{print $2}' 17938 [root@web-test ~]# ls -l /proc/17938/exe lrwxrwxrwx 1 mysql mysql 0 May 9 10:58 /proc/17938/exe -> /usr/sbin/mysqld
4、ssh常用技巧
4.1 #ssh -p远程主机ssh端口号---用于远程主机修改默认ssh端口号的情况,即ssh端口不是:22
# 例如远程主机:172.16.16.2的ssh端口号为1822 ssh -p1822 root@172.16.16.2
#ssh反向代理/ssh隧道
4.2 #ssh -fCNR [ssh主机IP或省略]:[ssh主机端口]:[待映射服务IP]:[待映射服务端口] [ssh主机用户名@ssh主机IP]
[待映射服务IP]:[待映射服务端口]---可以是本机上服务或者本机可以访问到的主机上的服务。
注:该命令的功能是将本机上的服务或本机可以访问到的其他主机的服务映射到远程ssh主机的对应端口上;主要应用场景就是把本地网络服务映射到远程主机网络中,从而实 现把本地内网的服务映射到远程网络中。
#把本机80端口的服务映射到远程主机:1.1.1.1的8080端口 ssh -fCNR 8080:localhost:80 root@1.1.1.1 #假如本机所在局域网段为:172.16.16.0/24 把172.16.16.12:80端口映射到远程主机:1.1.1.1的8080端口 ssh -fCNR 8080:172.16.16.12:80 root@1.1.1.1
4.3 #ssh -fCNL [本机IP]:[本机端口]:[待映射服务IP]:[待映射服务端口] [ssh主机用户名@ssh主机IP]
[待映射服务IP]:[待映射服务端口]---可以是远程主机上的服务或者是远程主机可以访问到的服务
该命令的功能是将远程主机上的服务或远程主机可以访问到的其他主机的服务映射到本机的对应端口上;主要应用场景就是把远程主机上或远程主机所在网络防火墙后的服务映射到本机端口,从而实现绕过远程防火墙来访问远程服务。
#把远程主机:1.1.1.1的3306端口映射到本机33060端口 ssh -fCNL 33060:localhost:3306 root@1.1.1.1 #把远程主机:1.1.1.2的3306端口通过主机:1.1.1.1映射到本机33060端口 ssh -fCNL 33060:1.1.1.2:3306 root@1.1.1.1
5、批量查找相关文件中指定关键字:find /etc/ -type f | xargs grep "infra"
6、#读取文件URL列表批量下载文件
wget -i filename
Windows系统(不想开新页面了~):
tasklist 查看系统运行所有进程列表,参考链接:https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/tasklist
taskkill 终止相关进程,参考链接:https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill
Parameter | Description |
---|---|
/s <computer>
| 指定远程计算机的名称或 IP 地址(请勿使用反斜杠)。 默认为本地计算机。 |
/u <domain>\<username> | 使用 <username> 或 <domain>\<username> 指定的用户的帐户权限运行该命令。 仅当还指定了 /s 时,才能指定 /u 参数。 默认值是当前登录到发出该命令的计算机的用户的权限。 |
/p <password> | 指定 在 /u 参数中指定的用户帐户的密码。 |
/fi <filter> | 应用筛选器以选择一组任务。 可以使用多个筛选器或使用通配符 (* ) 指定所有任务或映像名称。 本文的“筛选器名称、运算符和值”部分列出了有效的筛选器。 |
/pid <processID> | 指定要终止的进程的进程 ID。 |
/im <imagename> | 指定要终止的进程的映像名称。 使用通配符 (* ) 指定所有映像名称。 |
/f | 指定强制结束进程。 对于远程进程,将忽略此参数;所有远程进程都会被强制结束。 |
/t | 结束指定的进程及其启动的任何子进程。 |
#示例(终止所有nginx相关进程):
taskkill /f /t /im nginx*