ssh 实践

ssh

安装

ssh 服务需要安装 ssh 服务端

1
apt install openssh-server

查看当前 ssh 开启情况

1
ps -e | grep ssh

手动开启 ssh 服务

1
2
/etc/init.d/ssh start
/etc/init.d/ssh restart

qa

  • sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper

需要配置非 root 用户登录密码提示.

1
2
echo $USER ALL=\(root\) NOPASSWD:ALL | tee /etc/sudoers.d/$USER \
&& chmod 0440 /etc/sudoers.d/$USER

ssh-keygen

ssh-keygen 可用于生产 ssh 验证密钥,供 scp 及 ssh 等工具

生产 ssh 密钥

1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

使用示例

1
2
3
4
# 传输文件到远程
scp -P 22 local_file_path username@remote_host:remote_file_path
# 执行远程命令
ssh -p 22 username@remote_host 'command_to_execute'

将公钥添加到服务器。将 id_rsa.pub 文件的内容复制并粘贴到服务器的~/.ssh/authorized_keys 文件中。

ssh-copy-id

使用该工具上传公钥到服务器,可实现免密登录

安装 ssh-copy-id

1
2
# Windows
choco install ssh-copy-id
1
2
# 将本地上传到远程
ssh-copy-id root@192.168.145.128