Linux-ssh
参考文献
SSH
1 | ssh [options] [user@]host [command] |
- -l 指定登入用户
- -p 设置端口号
- -f 后台运行,并推荐加上 -n 参数
- -n 将标准输入重定向到 /dev/null,防止读取标准输入.如果在后台运行ssh的话(-f选项),就需要这个选项.
- -N 不执行远程命令,只做端口转发
- -q 安静模式,忽略一切对话和错误提示
- -T 禁用伪终端配置
- -t (tty)为远程系统上的ssh进程分配一个伪tty(终端).如果没有使用这个选项,当你在远程系统上运行某条命令的时候,ssh不会为该进程分配tty(终端).相反,ssh将会把远端进程的标准输入和标准输出附加到ssh会话上去,这通常就是你所希望的(但并非总是如此).这个选项将强制ssh在远端系统上分配tty,这样那些需要tty的程序就能够正常运行.
- -v verbose)显示与连接和传送有关的调试信息.如果命令运行不太正常的话,这个选项就会非常有用.
SSH
生成访问公钥
1 | $ ssh-keygen -o -t rsa -b 2048 -C "example@163.com" |
1 | cat $HOME/.ssh/id_rsa.pub |
SSH
隧道
本地端口转发
1 | ssh -L [bind_address:]port:host:hostport |
Linux Shell
远程执行命令(命令行与脚本方式)
前提条件
- 配置ssh免密码登陆
执行简单命令
1 | ssh user@remoteNode "cd /home ; ls" |
- 基本能完成常用的对于远程节点的管理了,几个注意的点
- 双引号,必须有.如果不加双引号,第二个ls命令在本地执行
- 分号,两个命令之间用分号隔开
执行复杂命令
1 | #!/bin/bash |
- 远程执行的内容在“<< eeooff ” 至“ eeooff ”之间,在远程机器上的操作就位于其中,注意的点:
- << eeooff,ssh后直到遇到eeooff这样的内容结束,eeooff可以随便修改成其他形式.
- 重定向目的在于不显示远程的输出了
- 在结束前,加exit退出远程节点
Ubuntu 18.04
离线安装openssh
-
下载
deb
包1
2
3
4
5
6
7-rwx------ 1 holelin staff 1.5M 1 23 10:18 libssl-dev_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
-rwx------ 1 holelin staff 1.2M 1 23 10:18 libssl1.1_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
-rwx------ 1 holelin staff 596K 1 23 10:16 openssh-client_7.6p1-4ubuntu0.7_amd64.deb
-rwx------ 1 holelin staff 324K 1 23 10:16 openssh-server_7.6p1-4ubuntu0.7_amd64.deb
-rwx------ 1 holelin staff 44K 1 23 10:16 openssh-sftp-server_7.6p1-4ubuntu0.7_amd64.deb
-rwx------ 1 holelin staff 599K 1 23 10:19 openssl_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
-rwx------ 1 holelin staff 6.8K 1 23 10:19 perl-openssl-defaults_3build1_amd64.deb -
先安装依赖
1
2
3dpkg -i perl-openssl-defaults_3build1_amd64.deb
dpkg -i libssl-dev_1.1.1-1ubuntu2.1~18.04.23_amd64.deb
dpkg -i libssl1.1_1.1.1-1ubuntu2.1~18.04.23_amd64.deb -
再安装应用
1
dpkg -i openssh*
SSH
别名
-
打开本地
SSH
配置文件1
vim ~/.ssh/config
-
添加别名及相关设置
1
2
3
4
5
6
7
8
9
10Host alias_name
Hostname target_host
User username
Port port_number
IdentityFile path_to_private_key
# 示例
Host dev
HostName 192.168.11.110
User holelin
Port 22alias_name
: 替代复杂主机名的简洁别名.target_host
: 目标主机的主机名或IP地址.username
: 连接远程主机时使用的用户名.port_number
(可选): 目标主机的SSH端口号,默认为22.path_to_private_key
(可选): 指定用于连接的私钥文件路径.
-
测试SSH别名
1
2
3ssh alias_name
# 示例
ssh dev
修改服务器SSH
配置
修改默认端口
-
备份原始
sshd
配置文件1
cp /etc/ssh/sshd_config /etc/ssh/sshd_config_bak
-
修改
sshd
服务的端口号1
vim /etc/ssh/sshd_config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22# $OpenBSD: sshd_config,v 1.101 2017/03/14 07:19:07 djm Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
#Port 22
# 修改对应需要开放的端口
Port 1022
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key -
重启
sshd
服务1
systemctl restart sshd
禁止root
登录
-
修改
/etc/ssh/sshd_config
1
2
3
4
5
6
7
8# Authentication:
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
PermitRootLogin no
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10 -
重启
sshd
服务1
systemctl restart sshd
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 HoleLin's Blog!