[SSH] Intro to SSH command
Create an ssh key:
ssh-keygen
Copy an SSH key to a remoate server:
ssh-copy-id root@104.197.227.8 // username@ip address / hostname
Then enter your password.
To make sure you can SSH into remote server, you can do:
ssh root@104.197.227.8
Since ssh-copy-id
is just a helper script, let's find it what it's actually doing in the event we want to manually add keys for authentication in the future. After SSHing into the remote host, go into the .ssh
folder within your home directory. Within that folder will be a file named authorized_keys
. This file contains a list of public SSH keys which have been granted access for authentication. We can see that our public SSH key has been added to this file. Public key added to authorized_keys.
SSH into a remote server:
Normal way to do it:
1 | ssh root@104.197.227.8 |
If you need to do more configuration:
ssh -p 2022 -i ~/.ssh/another_key root@104.197.227.8 // Let's assume the SSH server is bound to port 2022 instead of port 22. We add a -p flag followed by our port number 2022. Another command flag that is important is the -i flag. The i stands for identity and allows us to specify a different SSH key to authenticate with. This makes it possible to set up any number of different SSH keys to connect to any number of different hosts.
SSHing into a remote host drops you right into a bash shell, but you may just want to run one command and immediately exit. Instead of dropping a newer bash shell you may specify a command to run after typing in your connection string. This allows you to run one-off commands, a quick bash script, or anything else you wish without needing to create and stay within an SSH session.
ssh root@104.197.227.8 hostname // get hostname and exit
Simplify connections with SSH config files:
Using SSH config files to greatly simplify SSH connections to remote hosts, use hostname aliases, and set advanced directives such as Port and IdentityFile settings per host
Create a config file:
vi ~/.ssh/config
Host foo // alias for the host HostName 104.197.227.8 // define the actul hostname or ip address IdentityFile ~/.ssh/id_rsa // if needed, tell using a different public key Port 22 // if needed, change the port
SSH into a host:
ssh foo
Use scp to securely copy files remotely over SSH:
To use the secure copy command to copy the file to the remote host, type scp
followed by the file you wish to copy, in this case bar.txt
. Then specify your username @remotehost connection string, and suffix it with a colon. After the colon is where to tell scp where to copy the file to on the remote host. In this case, copy it to the home directory.
scp bar.txt mark@myhost.com:~/bar.txt
By default, scp
will use your default SSH key to connect to the remote host. To specify a different SSH key or identity, use the -i
flag followed by the location of your SSH private key.
scp -i ~/.ssh/another bar.txt mark@myhost.com:~/
You can also copy the whole folder by using `-r` command:
scp -r foo mark@myhost.com:~/bar.txt // copy the whole foo folder
Copy the fild from remote host to local host:
scp mark@myhost.com:~/bar.txt ./new.txt
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2018-04-11 [Tailwind] Extending Tailwind with Responsive Custom Utility Classes
2018-04-11 [Tailwind] Control What Variations are Generated for Each Utility Class Module in Tailwind
2016-04-11 [RxJS] Creation operator: of()