运维常用命令:

用户类型:

创建root权限用户:

1
sudo useradd -o -u 0 -g 0 -m -s /bin/bash name

干净删除用户:

1
userdel -r -f username

切换用户:

1
su - username

修改用户密码:

1
passwd username

退出:

1
exit

修改用户为root权限:

1
usermod -u 0  -g 0 -o username

文件传输类型:

以下命令均在win上执行

win上传单文件给linux:

1
scp  test.txt  ranq@192.168.54.1:/tmp

win文件目录同步到linux:

1
scp -r test2 ranq@192.168.54.129:/tmp

linux传文件到win本地:

1
scp   ranq@192.168.54.129/tmp/1.txt   D:/desktop/test

linux文件目录同步到本地:

1
scp   -r ranq@192.168.54.129/tmp/test   D:/desktop/test

iptables类型:

查看iptables配置:

1
iptables -nL

语法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
iptables -t (表名,不写默认filter) -A (链名,INPUT,OUTPUT) -s (源地址src) -d (目的地址dst) --sport (源端口srcport) --dport (目的端口dstport) -p (协议tcp,udp,icmp) -j (处置动作ACCEPT,DENY,DROP)

例子:iptables -t filter -A INPUT -s 192.168.30.5 -d 192.168.30.1 -p tcp -j ACCEPT

介绍各个参数:
常用参数:
-t 选择表(不写的话默认filter)
-A 规则放在最后
-s 源地址 src
-d 目的地址 dst
--sport 源端口(注意双斜杠)
--dport 目的端口(注意双斜杠)
-p 协议(如tcp,udp){注意这是小写的p}
-P 默认策略(如ACCEPT,DENY){注意这是大写的p}
-j 处理动作
(这里常见的处理动作:
ACCEPT:允许
DROP:丢弃(直接丢弃)
REJECT:丢弃(回显ICMP不可达信息)
LOG:直接将数据包信息记录到syslog
)
-L list缩写(如列出链的策略。iptables -L INPUT)
-F flush缩写(如删除链的策略。Iptables -F INPUT)

例如:禁止ping

1
iptables -A INPUT -p icmp -j DROP

禁止某ip ssh:

1
iptables -A INPUT -s 192.168.1.1 --dport 22 -p tcp  -j DROP

SSH类型:

ssh配置位置:

1
/etc/ssh/sshd_config

免密登录ssh key位置:

1
~/.ssh/authorized_keys

1.先创建备份:

1
cp /etc/ssh/sshd_config  /etc/ssh/sshd_config_beifen

2.修改端口和协议

1
2
3
\#port 22

port 65500

建议选取大数值为端口,如 65500 等,端口扫描动态容易被安全软件获取到流量

写入和使用 ssh v2 协议

1
Protocol 2

3.禁止 root 登录

1
2
3
#PermitRootLogin no

PermitRootLogin no

4.禁止空密码登录

1
2
3
#PermitEmptyPasswords no

PermitEmptyPasswords no

5.限制用户和 ip 登录

白名单:

1
AllowUsers  admin@192.168.1.128  test@192.168.1.111

或者:

在/etc/hosts.allow 加入:(白名单)

1
2
sshd:192.168.1.128             //单 ip
sshd:192.168.1. //段 ip

黑名单:

1
DenyUsers   sync@192.168.1.128  test

在/etc/hosts.deny 加入:(黑名单)

1
2
all:192.168.1.128             //全部形式的 128 的都拒绝
sshd:192.168.1.128 //sshd,单 IP 的拒绝

hosts.allow 和 hosts.deny设置好后,要重新启动

1
2
/etc/rc.d/init.d/xinetd restart 
/etc/rc.d/init.d/network restart

6.限制身份验证最大尝试次数

1
MaxAuthTries 3       

即用户验证失败次数达到后即断开回话

空闲超时断开:

1
ClientAliveInterval 60    

60 秒探测一次

1
ClientAliveCountMax 1 

几次无反应就断开,这里就是 60*1=60s 后我反馈就断开

7.显示最后一次登录的日期和时间

1
PrintLastLog yes

最后操作:

1
sshd -t

如果无输出则证明无报错,则重启sshd

1
sudo systemctl restart sshd

个性化登录欢迎语句

登录前显示:

先添加提示词文件和内容
在/etc/ssh路径下执行下面命令

1
echo 登陆前  > /etc/ssh/banner              //banner可以随便写

再把读取欢迎语的文件在配置文件引用:

1
2
3
nano /etc/ssh/sshd_config
添加下面语句:
Banner /etc/ssh/banner //和上述文件一样名字

登录后显示:

1
echo 登录后  > /etc/motd

配置后需要保存并重启生效

重启:

1
2
3
service sshd restart
或者
systemctl restart sshd

日志类型:

日志位置:

1
/var/log

常用命令:

1
2
查看所有用户登录情况:lastlog
查看上一次失败用户情况:lastb