Logging in to a remote server with SSH
To connect to a server remotely, use:
ssh user@hostname
user: the usernamehostname: the server's IP address or domain name
On the first connection, SSH will usually display a host verification prompt like this:
The authenticity of host '123.57.47.211 (123.57.47.211)' can't be established.
ECDSA key fingerprint is SHA256:iy237yysfCe013/l+kpDGfEG9xxHxm0dnxnAbJTPpG8.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Type yes and press Enter. After that, the server's identity will be recorded in ~/.ssh/known_hosts.
Then enter the password to complete the login.
SSH uses port 22 by default. If the server listens on a different port, specify it explicitly:
ssh user@hostname -p 22
Simplifying connections with ~/.ssh/config
If you connect to the same machines regularly, it is more convenient to define aliases.
Create the file ~/.ssh/config and edit it, for example with Vim:
Host myserver1
HostName IP地址或域名
User 用户名
Host myserver2
HostName IP地址或域名
User 用户名
Once this is set up, you can connect by using the aliases directly, such as myserver1 or myserver2, instead of retyping the full username and host each time.
Setting up key-based login
To generate an SSH key pair, run:
ssh-keygen
You can simply keep pressing Enter to accept the defaults.
When the command finishes, two new files will appear under ~/.ssh/:
id_rsa: the private keyid_rsa.pub: the public key
If you want password-free access to a server, copy the public key to that server.
For example, to enable passwordless login to myserver, place the contents of the current machine's public key into ~/.ssh/authorized_keys on myserver.
You can also do this in one step with:
ssh-copy-id myserver #mysever是配置免密登录的服务器名称
A practical sequence for setting up passwordless login from server A to server B looks like this:
- On server
A, configure the target in the.ssh/directory using theconfigfile. - Try logging in once with
ssh serverBto make sure the connection works normally. - Back on server
A, runssh-keygento create the private/public key pair if it does not already exist. - Then run
ssh-copy-id serverB, or manually copy the contents ofid_rsa.pubfrom serverAinto~/.ssh/authorized_keyson serverB.
Copying files with scp
The basic form of scp is:
scp source destination
This copies the file at source to destination.
To copy multiple files at once:
scp source1 source2 destination
Copying directories
Use -r when transferring folders recursively:
scp -r ~/tmp myserver:/home/acs/ #将本地家目录中的tmp文件夹复制到myserver服务器中的/home/acs/目录下。
scp -r ~/tmp myserver:homework/ #将本地家目录中的tmp文件夹复制到myserver服务器中的~/homework/目录下。
scp -r myserver:homework . #将myserver服务器中的~/homework/文件夹复制到本地的当前路径下。
Specifying a server port
If needed, provide the port number like this:
scp -P 22 source1 source2 destination
One detail worth remembering: options such as -r and -P should ideally be placed before source and destination.
A simple example is using scp to copy your vim and tmux configuration files to another server:
scp ~/.vimrc ~/.tmux.conf myserver: