一、基本信息
Ubuntu是linux系統(tǒng),雖然他是支持界面化操作的,一般用來做服務(wù)器用,所以配置ssh可以比較安全的進(jìn)行訪問,也方便在其他地方訪問服務(wù)器,輕松省事。
Ubuntu系統(tǒng)版本:
使用uname -a指令獲取系統(tǒng)版本信息
zero@ubuntu:~$ uname -a
Linux ubuntu 5.15.0-79-generic #86-Ubuntu SMP Mon Jul 10 16:07:21 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
具體信息如下:
- 系統(tǒng)類型: Linux
- 主機(jī)名:ubuntu
- 內(nèi)核版本:5.15.0-79-generic
- 編譯時(shí)間:#86-Ubuntu SMP Mon Jul 10 16:07:21 UTC 2023
- 硬件架構(gòu):x86_64 x86_64 x86_64(處理器架構(gòu),操作系統(tǒng)類型,軟件環(huán)境)
- 操作系統(tǒng)名稱:GNU/Linux
二、ssh安裝
2.1 查看是否已經(jīng)安裝ssh
直接輸入ssh查看
zero@ubuntu:~$ ssh
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
[-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
[-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
[-i identity_file] [-J [user@]host[:port]] [-L address]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-Q query_option] [-R address] [-S ctl_path] [-W host:port]
[-w local_tun[:remote_tun]] destination [command [argument ...]]
有具體的信息就是已經(jīng)安裝了。
2.2 安裝ssh
可以使用如下指令安裝ssh
sudo apt-get install openssh-server
一般服務(wù)器是被訪問的,所以只要安裝openssh-server即可,如果要安裝客戶端,將安裝的內(nèi)容改成openssh-client即可。
2.3 查看ssh安裝狀態(tài)
安裝完成后重新查看ssh安裝狀態(tài)
使用netstat查看狀態(tài)
zero@ubuntu:~$ sudo netstat -tlnp | grep sshd
[sudo] password for zero:
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2714/sshd: /usr/sbi
tcp6 0 0 :::22 :::* LISTEN 2714/sshd: /usr/sbi
使用systemctl查看狀態(tài)
zero@ubuntu:~$ sudo systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2023-08-26 15:01:13 UTC; 13min ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 2714 (sshd)
Tasks: 1 (limit: 4514)
Memory: 4.0M
CPU: 33ms
CGroup: /system.slice/ssh.service
└─2714 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
Aug 26 15:01:13 ubuntu systemd[1]: Starting OpenBSD Secure Shell server...
Aug 26 15:01:13 ubuntu sshd[2714]: Server listening on 0.0.0.0 port 22.
Aug 26 15:01:13 ubuntu sshd[2714]: Server listening on :: port 22.
Aug 26 15:01:13 ubuntu systemd[1]: Started OpenBSD Secure Shell server.
Aug 26 15:11:26 ubuntu sshd[2899]: Accepted password for zero from 192.168.159.1 port 51604 ssh2
Aug 26 15:11:26 ubuntu sshd[2899]: pam_unix(sshd:session): session opened for user zero(uid=1000) by (uid=0)
三、啟動(dòng)、停止,及開機(jī)自啟動(dòng)
3.1 啟動(dòng)ssh
zero@ubuntu:~$ sudo systemctl start ssh #啟動(dòng)ssh服務(wù)
zero@ubuntu:~$ sudo systemctl status sshd #查詢狀態(tài)
Unit sshd.service could not be found.
zero@ubuntu:~$ sudo systemctl status ssh
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; disabled; vendor preset: enabled)
Active: active (running) since Sat 2023-08-26 15:01:13 UTC; 27min ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 2714 (sshd)
Tasks: 1 (limit: 4514)
Memory: 4.0M
CPU: 33ms
CGroup: /system.slice/ssh.service
└─2714 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"
Aug 26 15:01:13 ubuntu systemd[1]: Starting OpenBSD Secure Shell server...
Aug 26 15:01:13 ubuntu sshd[2714]: Server listening on 0.0.0.0 port 22.
Aug 26 15:01:13 ubuntu sshd[2714]: Server listening on :: port 22.
Aug 26 15:01:13 ubuntu systemd[1]: Started OpenBSD Secure Shell server.
Aug 26 15:11:26 ubuntu sshd[2899]: Accepted password for zero from 192.168.159.1 port 51604 ssh2
Aug 26 15:11:26 ubuntu sshd[2899]: pam_unix(sshd:session): session opened for user zero(uid=1000) by (uid=0)
zero@ubuntu:~$ sudo netstat -tlnp | grep sshd #查詢狀態(tài)
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2714/sshd: /usr/sbi
tcp6 0 0 :::22 :::* LISTEN 2714/sshd: /usr/sbi
3.2 關(guān)閉ssh
zero@ubuntu:~$ sudo systemctl stop ssh #關(guān)閉ssh服務(wù)
zero@ubuntu:~$ sudo netstat -tlnp | grep sshd #關(guān)閉后查詢不到網(wǎng)絡(luò)狀態(tài)
zero@ubuntu:~$ sudo systemctl status ssh #ssh狀態(tài)已關(guān)閉
○ ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; disabled; vendor preset: enabled)
Active: inactive (dead) since Sat 2023-08-26 15:34:05 UTC; 11s ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 2714 ExecStart=/usr/sbin/sshd -D $SSHD_OPTS (code=exited, status=0/SUCCESS)
Main PID: 2714 (code=exited, status=0/SUCCESS)
CPU: 35ms
Aug 26 15:01:13 ubuntu systemd[1]: Starting OpenBSD Secure Shell server...
Aug 26 15:01:13 ubuntu sshd[2714]: Server listening on 0.0.0.0 port 22.
Aug 26 15:01:13 ubuntu sshd[2714]: Server listening on :: port 22.
Aug 26 15:01:13 ubuntu systemd[1]: Started OpenBSD Secure Shell server.
Aug 26 15:11:26 ubuntu sshd[2899]: Accepted password for zero from 192.168.159.1 port 51604 ssh2
Aug 26 15:11:26 ubuntu sshd[2899]: pam_unix(sshd:session): session opened for user zero(uid=1000) by (uid=0)
Aug 26 15:34:05 ubuntu systemd[1]: Stopping OpenBSD Secure Shell server...
Aug 26 15:34:05 ubuntu systemd[1]: ssh.service: Deactivated successfully.
Aug 26 15:34:05 ubuntu systemd[1]: Stopped OpenBSD Secure Shell server.
3.3 使用systemctl設(shè)置ssh服務(wù)自啟動(dòng)
zero@ubuntu:~$ sudo systemctl enable ssh
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable ssh
設(shè)置完成之后重啟即可
3.4 使用systemctl關(guān)閉ssh開機(jī)啟動(dòng)
zero@ubuntu:~$ sudo systemctl disable ssh
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable ssh
Removed /etc/systemd/system/multi-user.target.wants/ssh.service.
Removed /etc/systemd/system/sshd.service.
四、配置通過密鑰進(jìn)行免密訪問
部分操作說明可參考Windows下配置SSH實(shí)現(xiàn)免密訪問和遠(yuǎn)程端口轉(zhuǎn)發(fā),本文直接進(jìn)行操作。
4.1 生成密鑰
zero@ubuntu:~$ sudo ssh-keygen #此處直接使用默認(rèn)設(shè)置生成rsa的密鑰
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //不修改路徑
Enter passphrase (empty for no passphrase): #不設(shè)置密碼,實(shí)際使用根據(jù)自己需要設(shè)置密碼會(huì)比較安全
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:elLfv9r5e4RLOSKnee6oJPBGmS6YGm3WcAuvXVAxBtg root@ubuntu
The key's randomart image is:
+---[RSA 3072]----+
| o..+ |
| . E. o |
| . |
| . o |
| o + + S o |
| . O B o ...o = .|
|. * = O o .=.o + |
| = o + = o..o...|
|. . . ...++.=++|
+----[SHA256]-----+
注: 若要配置git訪問服務(wù)器,此處密鑰對可使用rsa格式的,git for windows默認(rèn)識(shí)別rsa密鑰。
4.2 通過ssh-agent管理私鑰
zero@ubuntu:~$ ssh-agent #啟動(dòng)服務(wù)
SSH_AUTH_SOCK=/tmp/ssh-XXXXXXjkoDta/agent.3455; export SSH_AUTH_SOCK;
SSH_AGENT_PID=3456; export SSH_AGENT_PID;
echo Agent pid 3456;
zero@ubuntu:~$ ssh-add /root/.ssh/id_rsa # 沒有使用agent啟動(dòng)bash直接調(diào)用ssh-add添加私鑰會(huì)報(bào)錯(cuò)
Could not open a connection to your authentication agent.
zero@ubuntu:~$ sudo ssh-agent bash --login -i # 使用ssh-agent啟動(dòng)bash,注意需要添加sudo,添加root的密鑰需要使用root權(quán)限。
root@ubuntu:/home/zero# ssh-add /root/.ssh/id_rsa #將密鑰添加到agent中
Identity added: /root/.ssh/id_rsa (root@ubuntu)
root@ubuntu:/home/zero# exit # 退出當(dāng)前bash
logout
zero@ubuntu:~$
ssh-add 參數(shù):
- -D:刪除ssh-agent中的所有密鑰.
- -d:從ssh-agent中刪除密鑰
- -e pkcs11:刪除PKCS#11共享庫pkcs1提供的鑰匙。
- -s pkcs11:添加PKCS#11共享庫pkcs1提供的鑰匙。
- -L:顯示ssh-agent中的公鑰
- -l:顯示ssh-agent中的密鑰
- -t life:對加載的密鑰設(shè)置超時(shí)時(shí)間,超時(shí)ssh-agent將自動(dòng)卸載密鑰
- -X:對ssh-agent進(jìn)行解鎖
- -x:對ssh-agent進(jìn)行加鎖
4.3 管理公鑰
服務(wù)器管理公鑰,可以直接添加公鑰文件中
zero@ubuntu:~$ cat /root/.ssh/id_ras.pub >> authorized_keys # 直接拷貝匯報(bào)錯(cuò),當(dāng)我們要操作的公鑰在root文件夾下時(shí)就需要root權(quán)限
cat: /root/.ssh/id_ras.pub: Permission denied
zero@ubuntu:~$ sudo cat /root/.ssh/id_rsa.pub >> authorized_keys # 添加sudo使用root權(quán)限操作
zero@ubuntu:~$ # 操作成功沒有錯(cuò)誤提示
4.4 通過scp將公鑰拷貝到服務(wù)器
需要將公鑰拷貝到要訪問的服務(wù)器中。
PS C:\WINDOWS\system32> scp C:\Users\LJM\.ssh\id_rsa.pub zero@192.168.159.129:.ssh/id_rsa_git.pub
zero@192.168.159.129's password:
id_rsa.pub 100% 567 558.8KB/s 00:00
PS C:\WINDOWS\system32>
4.5 將公鑰添加到公鑰管理文件中
切換到.ssh文件夾中,然后將公鑰添加到公鑰管理文件中。
zero@ubuntu:~/.ssh$ ll
total 24
drwx------ 2 zero zero 4096 Aug 26 17:25 ./
drwxr-x--- 4 zero zero 4096 Aug 26 16:13 ../
-rw------- 1 zero zero 567 Aug 26 17:28 authorized_keys
-rw-rw-r-- 1 zero zero 2602 Aug 26 16:57 id_rsa_git
-rw-rw-r-- 1 zero zero 567 Aug 26 17:25 id_rsa_git.pub
-rw-r--r-- 1 root root 565 Aug 26 16:43 id_rsa.pub
zero@ubuntu:~/.ssh$ cat id_rsa_git.pub >> authorized_keys
4.6 享受ssh免密鏈接服務(wù)器
公鑰和私鑰添加管理后,對應(yīng)的文件即可刪除了。然后客戶端就可以通過ssh免密訪問服務(wù)器。
通常一般要訪問的客戶端保存私鑰,服務(wù)端保存公鑰。
五、創(chuàng)建多用戶免密訪問
5.1 使用useradd創(chuàng)建新用戶
zero@ubuntu:~$ sudo useradd -g git git_user # 創(chuàng)建用戶,并指定初始區(qū)組為git
zero@ubuntu:~$ id git_user # 查詢用戶基本信息
uid=1001(git_user) gid=1001(git) groups=1001(git)
zero@ubuntu:/etc/ssh$ sudo passwd git_user # 修改用戶密碼,不修改密碼,可能后續(xù)ssh無法登錄
New password:
Retype new password:
passwd: password updated successfully
5.2 將ssh的密鑰文件拷貝到新用戶的家目錄中
zero@ubuntu:~$ sudo cp .ssh/authorized_keys /home/git_user/.ssh/authorized_keys
- 第一個(gè)目錄是已經(jīng)可以免密訪問的賬戶,家目錄下的密碼存儲(chǔ)文件,當(dāng)前就在zero賬戶的家目錄,故直接用相對路徑。
- 第二個(gè)參數(shù)是新建用戶的家目錄下的.ssh文件,文件名保持一致,若修改文件名需要修改“/etc/ssh/sshd_config”文件的密碼保存文件,將新的文件名添加到后面。
5.3 將配置文件的擁有者修改為新建用戶和新建用戶的初始群組
zero@ubuntu:~$ cd / # 返回更目錄
# 修改git_user的家目錄擁有著為git_user
zero@ubuntu:/$ sudo chown -R git_user /home/git_user/.ssh/authorized_keys
[sudo] password for zero:
zero@ubuntu:/$ getfacl /home/git_user/.ssh/authorized_keys
getfacl: Removing leading '/' from absolute path names
# file: /home/git_user/.ssh/authorized_keys
# owner: git_user
# group: root
user::rw-
group::---
other::---
# 修改/home/git_user的擁有群組為git
zero@ubuntu:/$ sudo chgrp git /home/git_user/.ssh/authorized_keys
zero@ubuntu:/$ getfacl /home/git_user/.ssh/authorized_keys
getfacl: Removing leading '/' from absolute path names
# file: /home/git_user/.ssh/authorized_keys
# owner: git_user
# group: git
user::rw-
group::---
other::---
由于原來拷貝的密鑰文件已經(jīng)存在密鑰,至此,有密鑰的電腦即可實(shí)現(xiàn)使用新賬號(hào)git_user進(jìn)行免密訪問了。
5.4 踩坑歷程:
在測試配置其他用戶也免密訪問的過程中通過多次實(shí)驗(yàn),首先在網(wǎng)上找解決方案,不過沒有找到,網(wǎng)上基本只是講了一個(gè)全新配置,沒有此方面的案例,迫不得已自行摸索。文章來源:http://www.zghlxwxcb.cn/news/detail-754765.html
- 最開始,我將創(chuàng)建萬用戶后,直接訪問,結(jié)果發(fā)現(xiàn)無法免密訪問。就想著新建用戶可能賬號(hào)是權(quán)限問題,所以就想著將新建戶和群組添加到ssh密鑰管理文件的權(quán)限里。
- 首先將用戶組添加到密鑰管理文件,結(jié)果不止新建用戶無法訪問,原來的zero賬戶也無法訪問了;
- 覺得可能是權(quán)限問題,所以又將該文件用戶的和其他用戶的權(quán)限改為“rw-”,也是一樣;
- 在常務(wù)無法使用之后,嘗試去除添加的新用戶git_user,仍然無法使用;
- 最后將該文件的group權(quán)限改為“—”才能正常訪問,即家目錄下ssh要正常訪問,authorized_keys的權(quán)限只能是擁有者,而且擁有群組的權(quán)限需要為“—”;
- 我又開始嘗試,新建一個(gè)用戶,然后將新建用戶家目錄和初始群組設(shè)置為可以免密訪問的用戶同目錄和群組,結(jié)果原來的用戶可以正常免密訪問,但是新用戶依然無法實(shí)現(xiàn)免密訪問;
- 嘗試將密鑰文件authorized_keys的權(quán)限添加上新用戶的,不過新用戶依然無法實(shí)現(xiàn)免密訪問。
- 最終,經(jīng)過多番折騰,發(fā)現(xiàn)只能通過新建用戶的家目錄中添加配置文件,然后再行添加密鑰即可實(shí)現(xiàn)免密訪問,才是應(yīng)該是家目錄的特殊性導(dǎo)致的,畢竟家目錄是針對單一用戶設(shè)置的,理論上每一個(gè)用戶都有一個(gè)家目錄。
踩踩坑,記錄以下。文章來源地址http://www.zghlxwxcb.cn/news/detail-754765.html
到了這里,關(guān)于Linux學(xué)習(xí)筆記-Ubuntu系統(tǒng)下配置ssh免密訪問,創(chuàng)建多用戶免密訪問的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!