linux的ssh相关指令

1.安装ssh

apt-get install openssh-server

2.备份ssh的配置文件

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

3.新装的ssh需要修改配置文件

vi /etc/ssh/sshd_config

  配置文件修改这几处地方  

Port = 22 # 默认是22端口,如果和windows端口冲突或你想换成其他的否则不用动
#ListenAddress 0.0.0.0 # 如果需要指定监听的IP则去除最左侧的井号,并配置对应IP,默认即监听PC所有IP
PermitRootLogin no # 如果你需要用 root 直接登录系统则此处改为 yes
PasswordAuthentication no # 将 no 改为 yes 表示使用帐号密码方式登录

4.启动ssh

service ssh start            # * Starting OpenBSD Secure Shell server sshd
# 或者
/etc/init.d/ssh start # * Starting OpenBSD Secure Shell server sshd

  如果提示错误信息中包含could not load host key 则需要重新生成 key

sudo rm /etc/ssh/ssh*key # 先移除旧的key
dpkg-reconfigure openssh-server

  生成之后需要重启SSH服务使新的密钥生效:     

service ssh restart          # * Restarting OpenBSD Secure Shell server sshd
# 或者
/etc/init.d/ssh restart       # * Restarting OpenBSD Secure Shell server sshd

  启动、停止和重启ssh的命令如下

/etc/init.d/ssh start         # * Starting OpenBSD Secure Shell server sshd
/etc/init.d/ssh stop          # * Stopping OpenBSD Secure Shell server sshd
/etc/init.d/ssh restart       # * Restarting OpenBSD Secure Shell server sshd

5.查看服务状态

service ssh status
# * sshd is running  显示此内容则表示启动正常

6.查看ssh是否启动

ps -e | grep ssh

  如果ssh已经启动则会提示

  469 ?        00:00:00 sshd

7.设置ssh开机自启动

sudo systemctl enable ssh    #说明:sudo是提升权限,systemctl是服务管理器,enable是systemctl的参数,表示启用开机自动运行,ssh是要设置的服务名称。

  注:如果要设置开机禁止启动则

sudo systemctl disable ssh   #说明:sudo是提升权限,systemctl是服务管理器,disable是systemctl的参数,表示禁止开机运行,ssh是要设置的服务名称。

  

  

原文地址:https://www.cnblogs.com/caixiaohua/p/9683591.html