优化Linux的SSH远程连接:
[root@wind ssh]# vim /etc/ssh/sshd_config
打开sshd配置文件,sshd_config是服务配置文件,# This is the sshd server system-wide configuration file.
ssh_config则是客户端配置文件。# This is the ssh client system-wide configuration file.
更改文件前一定要先备份,原因有两个,一是出错了可以还原,而是修改完可以对比;
[root@wind ssh]# cp sshd_config sshd_config.ori
然后用vim打开文件进入命令模式,输入:set nu,作用是显示行号,
- 修改Port 0-65536之间的一个
- 修改Protocol 2最安全
- 修改UseDNS no
- 修改PermitRootLogin no禁止管理员远程连接
- 修改ListenAddress 为服务器内网的IP
使用【diff】或者【vimdiff】查看两个文件的修改处;
- 解决两个机器连接慢的问题,修改GSSAPIAuthentication no
出错如何还原曾经备份的文件
[root@wind ~]# cp /etc/ssh/sshd_config.ori /etc/ssh/sshd_config cp: overwrite `/etc/ssh/sshd_config'? y
上述修改也可以集中修改,因为上述参数在原配置中都是注释掉的,直接集中添加在一起;
####by windzd##2020/1/5 Port 5465 PermitRootLogin no PermitEmptyPasswords no UseDNS no GSSAPIAutnentication no ListenAddress ip ####by windzd##2020/1/5
上述修改的仅仅是硬盘上的数据,并未生效,应该重启服务,将上述配置文件加载到内存;
[root@wind ~]# /etc/init.d/sshd restart
Stopping sshd: [ OK ]
Starting sshd: [ OK ]
推荐reload重加载模式,不影响访问用户;
[root@wind ~]# /etc/init.d/sshd reload
Reloading sshd: [ OK ]
查看端口和监听地址;
[root@wind ~]# netstat -lntup|grep sshd
利用sudo控制用户对系统命令的使用权限:
普通用户基本上只能在自己的卧室里或者公共卫生间里干些事了,一些命令也只能允许管理员才可以执行;
sudo的管理命令【visudo】相当于【vi /etc/sudoers】
vi在命令模式下输入行号gg可以直接跳到某一行,:set nu显示行号,
在某一行的命令模式下yyp可以复制并产生一行,改为普通用户,使用命令前加sudo;
形象比喻sudo来说就是皇帝给将军的虎符,带兵打仗必须用虎符sudo,不加sudo执行命令相当于没带虎符就要带兵打仗;
## Allow root to run any commands anywhere root ALL=(ALL) ALL zd ALL=(ALL) ALL ## Allows members of the 'sys' group to run networking, software,
[zd@wind ~]$ sudo useradd mm We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things: #1) Respect the privacy of others. #2) Think before you type. #3) With great power comes great responsibility. [sudo] password for zd: [zd@wind ~]$ tail -1 /etc/passwd mm:x:501:501::/home/mm:/bin/bash
[root@wind ~]# id mm uid=501(mm) gid=501(mm) groups=501(mm)
【which commond】查找命令所在的路径,
搜索范围来自全局变量PATH变量对应的路径
[root@wind ~]# which adduser /usr/sbin/adduser
which搜索范围来自全局变量PATH变量对应的路径,以:分割
[root@wind ~]# echo $PATH /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PATH变量位置,执行非内置命令时,系统会先查找PATH对应的路径是否有,没有就找不到指定命令,一般全局变量都位于/etc/profile下
[root@wind ~]# grep "PATH" /etc/profile case ":${PATH}:" in PATH=$PATH:$1 PATH=$1:$PATH export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
临时修改【export PATH="/tmp:$PATH"】,永久修改前面临时修改的命令放到/etc/profile下,使用source /etc/profile使立即生效;
[root@wind ~]# export PATH="/tmp:$PATH" [root@wind ~]# echo $PATH /tmp:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [root@wind ~]# touch /tmp/wind [root@wind ~]# chmod +x /tmp/wind [root@wind ~]# which wind /tmp/wind
export PATH="/tmp:$PATH"
【chmod +x file】将文件的权限改为可执行
【find】也可以查到
[root@wind ~]# find / -type f -name "useradd" /usr/sbin/useradd /etc/default/useradd
【whereis -b commond】也可以查到,-b查找二进制文件
[root@wind ~]# whereis -b adduser adduser: /usr/sbin/adduser [root@wind ~]# whereis -b useradd useradd: /usr/sbin/useradd
【locate commond】也可以查。
sudo管理不了内置命令;
Linux中文显示设置:
GBK:定长,双字节,不是国标
UTF-8:非定长,一个汉字占3个字节
字符集对应的配置文件:/etc/sysconfig/i18n
[root@wind ~]# cat /etc/sysconfig/i18n LANG="en_US.UTF-8" SYSFONT="latarcyrheb-sun16"
遵循先备份后修改!
改完配置文件用.或者source来生效配置文件;
CRT也要改UTF-8;
设置Linux服务器时间同步:
【date】显示时间
【date -s ""】
[root@wind ~]# date -s "2020/01/05 22:16" 2020年 01月 05日 星期日 22:16:00 CST [root@wind ~]# date 2020年 01月 05日 星期日 22:16:02 CST
定时和互联网同步的配置:
- 先找一个时间服务器
- 执行【ntpdate 服务器地址】同步互联网时间
[root@wind ~]# ntpdate ntp7.aliyun.com 5 Jan 22:22:04 ntpdate[2260]: step time server 203.107.6.88 offset -6015.115216 sec [root@wind ~]# date 2020年 01月 05日 星期日 22:22:10 CST [root@wind ~]# /usr/sbin/ntpdate ntp6.aliyun.com 5 Jan 22:22:44 ntpdate[2262]: adjust time server 203.107.6.88 offset -0.001497 sec [root@wind ~]# date 2020年 01月 05日 星期日 22:22:53 CST
- 设置定时任务【echo "*/5****/usr/sbin/ntpdate ntp6.aliyun.com >/dev/null 2>&1 " >>/var/spool/cron/root】
[root@wind ~]# echo "*5/ * * * * /usr/sbin/ntpdate ntp6.aliyun.com >/dev/null 2>&1" >>/var/spool/cron/root
-
>/dev/null 2>&1正确错误都扔黑洞null,等价于1>/dev/null 2>/dev/null等价于&>/dev/null
-
【crontab -l】查看定时任务
[root@wind ~]# crontab -l *5/ * * * * /usr/sbin/ntpdate ntp6.aliyun.com >/dev/null 2>&1
其实系统自己就有时间同步服务器ntpserver;【/etc/init.d/ntpd start】
【export TMOUT=10】设置中端10秒退出,临时生效
永久生效【echo "export TMOUT=10" >>/etc/profile】,然后用.或者source /etc/profile生效
[root@wind ~]# echo "export TMOUT=300" >>/etc/profile [root@wind ~]# tail -1 /etc/profile export TMOUT=300 [root@wind ~]# . /etc/profile
【history】历史记录,太多不好,可以设置历史记录:
[root@wind ~]# export HISTSIZE=5 [root@wind ~]# history 814 tail -1 /etc/profile 815 . /etc/profile 816 history 817 export HISTSIZE=5 818 history [root@wind ~]# echo "export HISTSIZE=20" >>/etc/profile [root@wind ~]# tail -1 /etc/profile export HISTSIZE=20 [root@wind ~]# . /etc/profile [root@wind ~]# history 817 export HISTSIZE=5 818 history 819 echo "export HISTSIZE=20" >>/etc/profile 820 tail -1 /etc/profile 821 . /etc/profile 822 history
【cat ~/.bash_history】历史记录文件,HISTFILESIZE=20临时更改和永久生效和上述一样;
【history -c】清空
【echo ***|passwd --stdin mm】更改用户mm的密码
[root@wind ~]# echo 123456|passwd --stdin mm 更改用户 mm 的密码 。 passwd: 所有的身份验证令牌已经成功更新。
删除指定历史命令记录:【history -d】
[root@wind ~]# echo 666666|passwd --stdin mm 更改用户 mm 的密码 。 passwd: 所有的身份验证令牌已经成功更新。 [root@wind ~]# history 817 ~/.bash_history 818 cat ~/.bash_history 819 echo 666666|passwd --stdin mm 820 history [root@wind ~]# history -d 819 [root@wind ~]# history 817 ~/.bash_history 818 cat ~/.bash_history 819 history 820 history -d 819 821 history