第16章 Linux的常用服务搭建

文档配套视频讲解链接地址

  1. 腾讯课堂视频链接地址 : 63_常用服务_TFTP服务1
  2. 腾讯课堂视频链接地址 : 64_常用服务_Samba服务2
  3. 腾讯课堂视频链接地址 : 65_常用服务_SSH服务3
  4. 腾讯课堂视频链接地址 : 66_常用服务_VSCode+SSH4
  5. 腾讯课堂视频链接地址 : 67_常用服务_SSH免密登录5
  6. 腾讯课堂视频链接地址 : 68_常用服务_VSCode问题处理6
  7. 腾讯课堂视频链接地址 : 69_常用服务_github与gitee7

第16章 常用服务搭建

16.1 TFTP服务器搭建

  1. TFTP(Trial File Transfer Protocol)是一种网络协议,主要用于文件的传输。在嵌入式交叉开发环境中被广泛使用。
  2. 查看服务(软件)是否安装
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ sudo apt policy tftpd xinetd
tftpd:
  已安装:()
  候选: 0.17-18ubuntu3
  版本列表:
     0.17-18ubuntu3 500
        500 http://mirrors.yun-idc.com/ubuntu bionic/universe amd64 Packages
xinetd:
  已安装:()
  候选: 1:2.3.15.3-1
  版本列表:
     1:2.3.15.3-1 500
        500 http://mirrors.yun-idc.com/ubuntu bionic/universe amd64 Packages
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ 
  1. 安装tftp 服务器和客户端
# tftp :下载文件的客户端 
# tftpd :下载文件的服务器 
# xinetd :系统后台服务 
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ sudo apt install tftpd tftp xinetd
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列【新】软件包将被安装:
  tftp tftpd xinetd
升级了 0 个软件包,新安装了 3 个软件包,要卸载 0 个软件包,有 15 个软件包未被升级。
需要下载 138 kB 的归档。
解压缩后会消耗 404 kB 的额外空间。
获取:1 http://mirrors.yun-idc.com/ubuntu bionic/universe amd64 tftp amd64 0.17-18ubuntu3 [16.5 kB]
获取:2 http://mirrors.yun-idc.com/ubuntu bionic/universe amd64 xinetd amd64 1:2.3.15.3-1 [108 kB]
获取:3 http://mirrors.yun-idc.com/ubuntu bionic/universe amd64 tftpd amd64 0.17-18ubuntu3 [14.3 kB]
已下载 138 kB,耗时 0(487 kB/s)
正在选中未选择的软件包 tftp。
(正在读取数据库 ... 系统当前共安装有 169453 个文件和目录。)
正准备解包 .../tftp_0.17-18ubuntu3_amd64.deb  ...
正在解包 tftp (0.17-18ubuntu3) ...
正在选中未选择的软件包 xinetd。
正准备解包 .../xinetd_1%3a2.3.15.3-1_amd64.deb  ...
正在解包 xinetd (1:2.3.15.3-1) ...
正在选中未选择的软件包 tftpd。
正准备解包 .../tftpd_0.17-18ubuntu3_amd64.deb  ...
正在解包 tftpd (0.17-18ubuntu3) ...
正在设置 xinetd (1:2.3.15.3-1) ...
正在设置 tftpd (0.17-18ubuntu3) ...
Note: xinetd currently is not fully supported by update-inetd.
      Please consult /usr/share/doc/xinetd/README.Debian and itox(8).
正在设置 tftp (0.17-18ubuntu3) ...
正在处理用于 systemd (237-3ubuntu10.53) 的触发器 ...
正在处理用于 man-db (2.8.3-2ubuntu0.1) 的触发器 ...
正在处理用于 ureadahead (0.100.0-21) 的触发器 ...
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ 
  1. 启动服务是否启动
sudo systemctl status  xinetd #查看后台服务器是否启动
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ sudo systemctl status  tftpd
Unit tftpd.service could not be found.
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ sudo systemctl status  xinetd
● xinetd.service - LSB: Starts or stops the xinetd daemon.
   Loaded: loaded (/etc/init.d/xinetd; generated)
   Active: active (running) since Mon 2022-09-05 08:45:43 CST; 2min 37s ago
     Docs: man:systemd-sysv-generator(8)
    Tasks: 1 (limit: 2283)
   CGroup: /system.slice/xinetd.service
           └─27702 /usr/sbin/xinetd -pidfile /run/xinetd.pid -stayalive -inetd_compat -inetd_ipv6

9月 05 08:45:43 ubuntu xinetd[27702]: Reading included configuration file: /etc/xinetd.d/discard [file=/etc/xinetd.d/discard] [line=14]
9月 05 08:45:43 ubuntu xinetd[27702]: Reading included configuration file: /etc/xinetd.d/discard-udp [file=/etc/xinetd.d/discard-udp] [line=25]
9月 05 08:45:43 ubuntu xinetd[27702]: Reading included configuration file: /etc/xinetd.d/echo [file=/etc/xinetd.d/echo] [line=14]
9月 05 08:45:43 ubuntu xinetd[27702]: Reading included configuration file: /etc/xinetd.d/echo-udp [file=/etc/xinetd.d/echo-udp] [line=26]
9月 05 08:45:43 ubuntu xinetd[27702]: Reading included configuration file: /etc/xinetd.d/servers [file=/etc/xinetd.d/servers] [line=14]
9月 05 08:45:43 ubuntu xinetd[27702]: Reading included configuration file: /etc/xinetd.d/services [file=/etc/xinetd.d/services] [line=13]
9月 05 08:45:43 ubuntu xinetd[27702]: Reading included configuration file: /etc/xinetd.d/time [file=/etc/xinetd.d/time] [line=13]
9月 05 08:45:43 ubuntu xinetd[27702]: Reading included configuration file: /etc/xinetd.d/time-udp [file=/etc/xinetd.d/time-udp] [line=28]
9月 05 08:45:43 ubuntu xinetd[27702]: 2.3.15.3 started with libwrap loadavg labeled-networking options compiled in.
9月 05 08:45:43 ubuntu xinetd[27702]: Started working: 0 available services
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ 

  1. 设置下载和目录
sudo vi /etc/xinetd.d/tftp # 这个文件是, 配置tftp下载的目录信息 

service tftp
{
    socket_type        = dgram 
    protocol           = udp 
    wait               = yes
    user               = linux
    server             = /usr/sbin/in.tftpd  # 要启动的程序
    server_args        = -s /var/tftpboot   # 这个就是服务器的目录 
    disable            = no 
    per_source         = 11
    cps                = 100 2
    flags              = IPv4
}

#server_args设置的/var/tftpboot目录是tftp服务器的目录,TFTP客户端就是从这个目录里获取文件的。
#使用命令"mkdir /var/tftpboot"建立TFTP服务器的目录。然后设置/var/tftpboot的访问权限为775.
sudo mkdir /var/tftpboot
sudo chmod 0777 /var/tftpboot


# 执行过程:
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ sudo vi /etc/xinetd.d/tftp
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ ls /var/
backups  cache  crash  lib  local  lock  log  mail  metrics  opt  run  snap  spool  tmp
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ sudo mkdir /var/tftpboot
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ ls /var/
backups  cache  crash  lib  local  lock  log  mail  metrics  opt  run  snap  spool  tftpboot  tmp
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ sudo chmod 0777 /var/tftpboot
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ ls /var/
backups  cache  crash  lib  local  lock  log  mail  metrics  opt  run  snap  spool  tftpboot  tmp
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ ls -l /var/
总用量 52
drwxrwxrwx  2 root root     4096 95 08:54 tftpboot
drwxrwxrwt  9 root root     4096 95 08:45 tmp
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$
  1. 重启xinetd服务
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ sudo systemctl restart xinetd.service 
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ sudo systemctl status xinetd
● xinetd.service - LSB: Starts or stops the xinetd daemon.
   Loaded: loaded (/etc/init.d/xinetd; generated)
   Active: active (running) since Mon 2022-09-05 08:56:42 CST; 12s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 29097 ExecStop=/etc/init.d/xinetd stop (code=exited, status=0/SUCCESS)
  Process: 29103 ExecStart=/etc/init.d/xinetd start (code=exited, status=0/SUCCESS)
    Tasks: 1 (limit: 2283)
   CGroup: /system.slice/xinetd.service
           └─29113 /usr/sbin/xinetd -pidfile /run/xinetd.pid -stayalive -inetd_compat -inetd_ipv6

9月 05 08:56:42 ubuntu xinetd[29113]: Reading included configuration file: /etc/xinetd.d/discard-udp [file=/etc/xinetd.d/discard-udp] [line=25]
9月 05 08:56:42 ubuntu xinetd[29113]: Reading included configuration file: /etc/xinetd.d/echo [file=/etc/xinetd.d/echo] [line=14]
9月 05 08:56:42 ubuntu xinetd[29113]: Reading included configuration file: /etc/xinetd.d/echo-udp [file=/etc/xinetd.d/echo-udp] [line=26]
9月 05 08:56:42 ubuntu xinetd[29113]: Reading included configuration file: /etc/xinetd.d/servers [file=/etc/xinetd.d/servers] [line=14]
9月 05 08:56:42 ubuntu xinetd[29113]: Reading included configuration file: /etc/xinetd.d/services [file=/etc/xinetd.d/services] [line=13]
9月 05 08:56:42 ubuntu xinetd[29113]: Reading included configuration file: /etc/xinetd.d/tftp [file=/etc/xinetd.d/tftp] [line=13]
9月 05 08:56:42 ubuntu xinetd[29113]: Reading included configuration file: /etc/xinetd.d/time [file=/etc/xinetd.d/time] [line=13]
9月 05 08:56:42 ubuntu xinetd[29113]: Reading included configuration file: /etc/xinetd.d/time-udp [file=/etc/xinetd.d/time-udp] [line=28]
9月 05 08:56:42 ubuntu xinetd[29113]: 2.3.15.3 started with libwrap loadavg labeled-networking options compiled in.
9月 05 08:56:42 ubuntu xinetd[29113]: Started working: 1 available service
linux@ubuntu:~/work/02-linuxbase/03-cmake/06-exam$ 

  1. 测试文件的下载
linux@ubuntu:~/work/02-linuxbase/01-shell$ cp 01-hello.sh /var/tftpboot/
cp: 无法创建普通文件'/var/tftpboot/01-hello.sh': 权限不够
linux@ubuntu:~/work/02-linuxbase/01-shell$ sudo chmod 0777 /var/tftpboot/
linux@ubuntu:~/work/02-linuxbase/01-shell$ cp 01-hello.sh /var/tftpboot/
linux@ubuntu:~/work/02-linuxbase/01-shell$ cp 02-weizhi.sh  /var/tftpboot/
linux@ubuntu:~/work/02-linuxbase/01-shell$ cp 03-huanjing.sh /var/tftpboot/
linux@ubuntu:~/work/02-linuxbase/01-shell$ ls /var/tftpboot/
01-hello.sh  02-weizhi.sh  03-huanjing.sh


linux@ubuntu:~/Desktop$ tftp 192.168.1.20   # 自己计算机的ip 
tftp> get 01-hello.sh    # 下载/var/tftpboot目录中的文件 , 把这些文件下载到Destktop目录下 
Received 110 bytes in 0.0 seconds
tftp> get 02-weizhi.sh
Received 416 bytes in 0.0 seconds
tftp> get 03-huanjing.sh
Received 109 bytes in 0.0 seconds
tftp> q    # 退出程序 
linux@ubuntu:~/Desktop$ 

16.2 Samba服务搭建

  1. Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。
  2. 检查samba服务是否安装
linux@ubuntu:~$ sudo apt policy samba
[sudo] linux 的密码: 
samba:
  已安装:()
  候选: 2:4.7.6+dfsg~ubuntu-0ubuntu2.28
  版本列表:
     2:4.7.6+dfsg~ubuntu-0ubuntu2.28 500
        500 http://mirrors.yun-idc.com/ubuntu bionic-security/main amd64 Packages
        500 http://mirrors.yun-idc.com/ubuntu bionic-updates/main amd64 Packages
     2:4.7.6+dfsg~ubuntu-0ubuntu2 500
        500 http://mirrors.yun-idc.com/ubuntu bionic/main amd64 Packages
linux@ubuntu:~$ 
  1. 安装samba软件
linux@ubuntu:~$ sudo apt install samba
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
将会同时安装下列软件:
  attr ibverbs-providers libcephfs2 libibverbs1 libnl-route-3-200 libpython-stdlib librados2 python python-crypto python-dnspython python-ldb
  python-minimal python-samba python-tdb python2.7 python2.7-minimal samba-common samba-common-bin samba-dsdb-modules samba-vfs-modules tdb-tools
建议安装:
  python-doc python-tk python-crypto-doc python-gpgme python2.7-doc binfmt-support bind9 bind9utils ctdb ldb-tools ntp | chrony smbldap-tools winbind
  heimdal-clients
下列【新】软件包将被安装:
  attr ibverbs-providers libcephfs2 libibverbs1 libnl-route-3-200 libpython-stdlib librados2 python python-crypto python-dnspython python-ldb
  python-minimal python-samba python-tdb python2.7 python2.7-minimal samba samba-common samba-common-bin samba-dsdb-modules samba-vfs-modules tdb-tools
升级了 0 个软件包,新安装了 22 个软件包,要卸载 0 个软件包,有 15 个软件包未被升级。
需要下载 9,533 kB 的归档。
解压缩后会消耗 52.8 MB 的额外空间。
您希望继续执行吗? [Y/n] y
获取:1 http://mirrors.yun-idc.com/ubuntu bionic-security/main amd64 python2.7-minimal amd64 2.7.17-1~18.04ubuntu1.8 [1,289 kB]
获取:2 http://mirrors.yun-idc.com/ubuntu bionic/main amd64 python-minimal amd64 2.7.15~rc1-1 [28.1 kB]
获取:3 http://mirrors.yun-idc.com/ubuntu bionic-security/main amd64 python2.7 amd64 2.7.17-1~18.04ubuntu1.8 [248 kB]
...
获取:22 http://mirrors.yun-idc.com/ubuntu bionic-security/main amd64 samba-vfs-modules amd64 2:4.7.6+dfsg~ubuntu-0ubuntu2.28 [298 kB]
已下载 9,533 kB,耗时 1(8,214 kB/s)      
正在预设定软件包 ...
正在选中未选择的软件包 python2.7-minimal。
(正在读取数据库 ... 系统当前共安装有 169505 个文件和目录。)
正准备解包 .../python2.7-minimal_2.7.17-1~18.04ubuntu1.8_amd64.deb  ...
正在解包 python2.7-minimal (2.7.17-1~18.04ubuntu1.8) ...
正在选中未选择的软件包 python-minimal。
正准备解包 .../python-minimal_2.7.15~rc1-1_amd64.deb  ...
...
Samba is not being run as an AD Domain Controller, masking samba-ad-dc.service.
Please ignore the following error about deb-systemd-helper not finding samba-ad-dc.service.
Created symlink /etc/systemd/system/multi-user.target.wants/nmbd.service → /lib/systemd/system/nmbd.service.
Failed to preset unit: Unit file /etc/systemd/system/samba-ad-dc.service is masked.
/usr/bin/deb-systemd-helper: error: systemctl preset failed on samba-ad-dc.service: No such file or directory
Created symlink /etc/systemd/system/multi-user.target.wants/smbd.service → /lib/systemd/system/smbd.service.
正在处理用于 mime-support (3.60ubuntu1) 的触发器 ...
正在处理用于 ureadahead (0.100.0-21) 的触发器 ...
正在处理用于 desktop-file-utils (0.23-1ubuntu3.18.04.2) 的触发器 ...
正在处理用于 libc-bin (2.27-3ubuntu1.6) 的触发器 ...
正在处理用于 systemd (237-3ubuntu10.53) 的触发器 ...
正在处理用于 man-db (2.8.3-2ubuntu0.1) 的触发器 ...
正在处理用于 gnome-menus (3.13.3-11ubuntu1.1) 的触发器 ...
正在处理用于 ufw (0.36-0ubuntu0.18.04.2) 的触发器 ...
linux@ubuntu:~$ 
  1. 查看samba服务是否启动
linux@ubuntu:~$ sudo systemctl status smbd.service 
● smbd.service - Samba SMB Daemon
   Loaded: loaded (/lib/systemd/system/smbd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2022-09-05 10:24:58 CST; 2min 19s ago
     Docs: man:smbd(8)
           man:samba(7)
           man:smb.conf(5)
 Main PID: 31250 (smbd)
   Status: "smbd: ready to serve connections..."
    Tasks: 4 (limit: 2283)
   CGroup: /system.slice/smbd.service
           ├─31250 /usr/sbin/smbd --foreground --no-process-group
           ├─31252 /usr/sbin/smbd --foreground --no-process-group
           ├─31253 /usr/sbin/smbd --foreground --no-process-group
           └─31254 /usr/sbin/smbd --foreground --no-process-group

9月 05 10:24:58 ubuntu systemd[1]: Starting Samba SMB Daemon...
9月 05 10:24:58 ubuntu systemd[1]: Started Samba SMB Daemon.
linux@ubuntu:~$ 
  1. 配置共享目录 /etc/samba/smb.conf
sudo vi /etc/samba/smb.conf 
#在文件末尾追加: 
[work]   # 表示共享目录的名称 , 只是一个标号 
   comment = work folder  # 表述信息 
   browseable = yes    # 可浏览 
   path = /home/linux/work  #共享的路径 
   create mask = 0777  # 可读 , 可写 , 可执行 
   directory mask = 0777  # 不屏蔽权限 
   valid users = linux   #有效的用户 linux 
   force user = linux   # 指定linux
   force group = linux  # 指定linux 
   public = yes          # 不是私有的
   writeable = yes      # 可写 
   available = yes     # 有效 
  1. 设置访问密码
sudo smbpasswd -a linux  // 密码 ,设置用户(liunux)的密码 
#根据提示深入用户密码(登陆Samba共享目录的时候需要)。


linux@ubuntu:~$ sudo vi /etc/samba/smb.conf 
linux@ubuntu:~$ sudo smbpasswd -a linux
New SMB password:     # 1 
Retype new SMB password:  # 1 
Added user linux.
linux@ubuntu:~$ 
  1. 重启samba服务
sudo systemctl  restart smbd.service

linux@ubuntu:~$ sudo systemctl restart smbd.service 
linux@ubuntu:~$ sudo systemctl status smbd.service 
● smbd.service - Samba SMB Daemon
   Loaded: loaded (/lib/systemd/system/smbd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2022-09-05 10:35:40 CST; 8s ago
     Docs: man:smbd(8)
           man:samba(7)
           man:smb.conf(5)
 Main PID: 33097 (smbd)
   Status: "smbd: ready to serve connections..."
    Tasks: 4 (limit: 2283)
   CGroup: /system.slice/smbd.service
           ├─33097 /usr/sbin/smbd --foreground --no-process-group
           ├─33099 /usr/sbin/smbd --foreground --no-process-group
           ├─33100 /usr/sbin/smbd --foreground --no-process-group
           └─33101 /usr/sbin/smbd --foreground --no-process-group

9月 05 10:35:40 ubuntu systemd[1]: Starting Samba SMB Daemon...
9月 05 10:35:40 ubuntu systemd[1]: Started Samba SMB Daemon.
linux@ubuntu:~$ 
  1. 测试samba服务
#Ubuntu测试: 
->在Ubuntu中点击文件图标 -> 其他位置 -> 底部的连接到服务器中输入服务器地址:
smb://192.168.1.20/work       # ip 是自己电脑的ip地址 
然后点击右下角的Connect按钮. 
此时会提示输入密码(在第6步中创建),输入密码后即可进入共享目录。
  1. window下测试samba服务
打开window的资源管理器(文件夹) -> 在地址栏输入:  \\192.168.1.20\work 
-> 用户名: linux 
     密码: 1 

image-20220905105237411

image-20220905105542393

点击此电脑 , 添加一个网络驱动器

image-20220905105805411

image-20220905105854467

image-20220905140603813

  1. 知识点的补充

    使用以上配置 , 发现不能写文件夹和创建文件, 因为没有设置force user 和 force group, 才会导致权限的问题 , 重新配置如下:

[work]  
   comment = work folder   
   browseable = yes    
   path = /home/linux/work 
   create mask = 0777  
   directory mask = 0777 
   valid users = linux  
   force user = linux   #  用户
   force group = linux  #  组 都这是都linux 
   public = yes      
   writeable = yes    
   available = yes   
   
  # 重启服务
  linux@ubuntu:~$ sudo systemctl status smbd.service 
● smbd.service - Samba SMB Daemon
   Loaded: loaded (/lib/systemd/system/smbd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2022-09-05 11:13:41 CST; 6min ago
     Docs: man:smbd(8)
           man:samba(7)
           man:smb.conf(5)
 Main PID: 33423 (smbd)
   Status: "smbd: ready to serve connections..."
    Tasks: 5 (limit: 2283)
   CGroup: /system.slice/smbd.service
           ├─33423 /usr/sbin/smbd --foreground --no-process-group
           ├─33425 /usr/sbin/smbd --foreground --no-process-group
           ├─33426 /usr/sbin/smbd --foreground --no-process-group
           ├─33427 /usr/sbin/smbd --foreground --no-process-group
           └─33434 /usr/sbin/smbd --foreground --no-process-group

9月 05 11:13:41 ubuntu systemd[1]: Starting Samba SMB Daemon...
9月 05 11:13:41 ubuntu systemd[1]: Started Samba SMB Daemon.
9月 05 11:14:47 ubuntu smbd[33434]: pam_unix(samba:session): session opened for user linux by (uid=0)
linux@ubuntu:~$ 

16.3 SSH服务搭建

  1. 什么是SSH

​ SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定;SSH 为建立在应用层基础上的安全协议。SSH 是较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。SSH最初是UNIX系统上的一个程序,后来又迅速扩展到其他操作平台。SSH在正确使用时可弥补网络中的漏洞。SSH客户端适用于多种平台。几乎所有UNIX平台—包括HP-UX、Linux、AIX、Solaris、Digital UNIX、Irix,以及其他平台,都可运行SSH。

image-20220905142117852

  1. 查看系统是否安装SSH服务
linux@ubuntu:~$ sudo apt policy openssh-client openssh-server 
openssh-client:
  已安装:1:7.6p1-4ubuntu0.7
  候选: 1:7.6p1-4ubuntu0.7
  版本列表:
 *** 1:7.6p1-4ubuntu0.7 500
        500 http://mirrors.yun-idc.com/ubuntu bionic-updates/main amd64 Packages
        100 /var/lib/dpkg/status
     1:7.6p1-4ubuntu0.5 500
        500 http://mirrors.yun-idc.com/ubuntu bionic-security/main amd64 Packages
     1:7.6p1-4 500
        500 http://mirrors.yun-idc.com/ubuntu bionic/main amd64 Packages
openssh-server:
  已安装:1:7.6p1-4ubuntu0.7
  候选: 1:7.6p1-4ubuntu0.7
  版本列表:
 *** 1:7.6p1-4ubuntu0.7 500
        500 http://mirrors.yun-idc.com/ubuntu bionic-updates/main amd64 Packages
        100 /var/lib/dpkg/status
     1:7.6p1-4ubuntu0.5 500
        500 http://mirrors.yun-idc.com/ubuntu bionic-security/main amd64 Packages
     1:7.6p1-4 500
        500 http://mirrors.yun-idc.com/ubuntu bionic/main amd64 Packages
linux@ubuntu:~$ 
  1. 安装ssh服务
sudo apt install openssh-client openssh-server 
linux@ubuntu:~$ sudo apt install openssh-client openssh-server 
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
openssh-client 已经是最新版 (1:7.6p1-4ubuntu0.7)。
openssh-server 已经是最新版 (1:7.6p1-4ubuntu0.7)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 15 个软件包未被升级。
linux@ubuntu:~$ 
  1. 检查ssh服务是否启动
linux@ubuntu:~$ sudo systemctl status sshd.service 
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2022-08-29 09:36:30 CST; 1 weeks 0 days ago
 Main PID: 780 (sshd)
    Tasks: 1 (limit: 2283)
   CGroup: /system.slice/ssh.service
           └─780 /usr/sbin/sshd -D

9月 05 10:09:17 ubuntu systemd[1]: Reloading OpenBSD Secure Shell server.
9月 05 10:09:17 ubuntu systemd[1]: Reloaded OpenBSD Secure Shell server.
9月 05 10:09:17 ubuntu sshd[780]: Received SIGHUP; restarting.
9月 05 10:09:17 ubuntu sshd[780]: Server listening on 0.0.0.0 port 22.
9月 05 10:09:17 ubuntu sshd[780]: Server listening on :: port 22.
9月 05 10:09:18 ubuntu systemd[1]: Reloading OpenBSD Secure Shell server.
9月 05 10:09:18 ubuntu sshd[780]: Received SIGHUP; restarting.
9月 05 10:09:18 ubuntu systemd[1]: Reloaded OpenBSD Secure Shell server.
9月 05 10:09:18 ubuntu sshd[780]: Server listening on 0.0.0.0 port 22.
9月 05 10:09:18 ubuntu sshd[780]: Server listening on :: port 22.
linux@ubuntu:~$ sudo systemctl status ssh
sshd.service  ssh.service   ssh.socket    
linux@ubuntu:~$ sudo systemctl status ssh.service   # sshd.server 和 ssh.server 是一样的服务
● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2022-08-29 09:36:30 CST; 1 weeks 0 days ago
 Main PID: 780 (sshd)
    Tasks: 1 (limit: 2283)
   CGroup: /system.slice/ssh.service
           └─780 /usr/sbin/sshd -D

9月 05 10:09:17 ubuntu systemd[1]: Reloading OpenBSD Secure Shell server.
9月 05 10:09:17 ubuntu systemd[1]: Reloaded OpenBSD Secure Shell server.
9月 05 10:09:17 ubuntu sshd[780]: Received SIGHUP; restarting.
9月 05 10:09:17 ubuntu sshd[780]: Server listening on 0.0.0.0 port 22.
9月 05 10:09:17 ubuntu sshd[780]: Server listening on :: port 22.
9月 05 10:09:18 ubuntu systemd[1]: Reloading OpenBSD Secure Shell server.
9月 05 10:09:18 ubuntu sshd[780]: Received SIGHUP; restarting.
9月 05 10:09:18 ubuntu systemd[1]: Reloaded OpenBSD Secure Shell server.
9月 05 10:09:18 ubuntu sshd[780]: Server listening on 0.0.0.0 port 22.
9月 05 10:09:18 ubuntu sshd[780]: Server listening on :: port 22.
linux@ubuntu:~$ 
  1. 在Window下进行测试 ,运行 cmd 或者 PowerShell ,输入链接命令:
ssh linux@192.168.1.20     # ssh   用户名@ip地址    , 是自己ubuntu的ip地址


PS C:\Users\sheng> ssh linux@192.168.1.20  # 在window下登录ubuntu 
The authenticity of host '192.168.1.20 (192.168.1.20)' can't be established.
ECDSA key fingerprint is SHA256:g+rxrkFtBCIwTyzY4xIPvtftDh8f04+ydYM6SFGdmBI.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.1.20' (ECDSA) to the list of known hosts.
linux@192.168.1.20's password:     # 输入密码 1 
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-124-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

15 updates can be applied immediately.
14 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable

Your Hardware Enablement Stack (HWE) is supported until April 2023.

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

linux@ubuntu:~$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  snap  Templates  Videos  work

16.4 xshell登录ubuntu

安装软件并选择免费为家庭/学校

image-20220905150243085

安装完成后, 运行软件是会弹出新建连接对话框

image-20220905150444287

->在弹出的对话框中点击新建
-> 名称: ubuntu18.04
   协议: ssh 
   主机: 192.168.1.20   # ubuntu主机的ip地址 
   说明信息: 用户名:linux 
            密码  :1 
-> 点击确定 

image-20220905151019781

选中ubuntu18.04 之后点击连接

image-20220905151051629

会提示, 是否保存秘钥 ,可以保存用户名和密码 , 下一次连接时可以不输入密码, 选择接收并保存

image-20220905151130745

输入用户名:linux , 选择记住用户名 , 点击确定

image-20220905151336807

正产输入密码后,即可连接, 如果没有连接上 , 可以点击工具栏中的重新连接

image-20220905151536743

点击重新连接后, 输入密码, 选择记住密码, 下次可以不在输入密码 , 之后点击确定。

image-20220905151559021

连接成功如下:

image-20220905151735559

16.5 VSCode通过SSH登录ubuntu

  1. 安装vscode的ssh插件

打开vscode软件 , 点击“扩展” , 在搜索框中输入: ssh , 之后点击安装即可。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5Jr8nuyU-1667655879391)(C:/Users/sheng/AppData/Roaming/Typora/typora-user-images/image-20220905155605455.png)]

安装成功后 , 在侧边栏会有一个 远程资源管理器, 单击如下图:

image-20220905155742710

  1. 使用ssh 连接ubuntu18.04

如下图, 点击 “+” , 新建一个连接, 在弹出的输入框中输入:

ssh linux@192.168.1.20 

image-20220905155948736

image-20220905160135162

image-20220905160206464

-> 选择 C:\user\sheng\.ssh\config 文件 
-> 在弹出的对话中选择 , OpenConfig 选项 
-> 添加如下信息:

Host ubuntu18.04-2207
  HostName 192.168.1.20
  User linux
  ForwardAgent yes
 
# 使用ctrl + s 保存文件

在远程资源管理窗口中点击 刷新后, 会出现我们配置的连接信息,之后右击“ubuntu18.04-2207”

image-20220905160635573

image-20220905160814045

选择 “Connect to Host in Current Window” , 开始连接

平台选择: linux

密码: 1

之后按下回车键即可完成连接 , 出现如下图, 表示成功连接。

image-20220905161037520

打开“资源管理器” , 选择打开文件夹 , 选择 /home/linux/work 目录, 点击确定。

image-20220905161210802

image-20220905161245420

之后再次输入密码: 1

image-20220905161403953

选择 “是, 我信任此作者 信任文件夹并启动所有功能”

image-20220905161433814

成功打开后, 如下图

image-20220905161713612

开启终端: ctrl + ~ 开启终端, 再按一次关闭终端

  1. vscode 安装runner code 插件

单击扩展 , 在搜索框中输入: code , 选择Code Runner , 打开目录有, 右击代码选择 “Run Code” 即可完成运行代码

image-20220905162455999

16.6 “Bad owner or permissions on C:\Users\##/.ssh”

image-20220906083741025

  1. 第一种可能的解决办法
vscode 中 "Bad owner or permissions on C:\\Users\\\##/.ssh" 
解决办法:
-> 文件 
-> 首选项 
-> 设置 
-> 扩展 
-> Remote-SSH 
-> Remote.SSH: Config File
   The absolute file path to a custom SSH config file.
   "C:\Users\YShengli\.ssh\config"
-> 再次连接, 以上问题即可解决 
  1. 第二种可能的解决办法

在ip地址改变以后, 同样也会发生这样的问题 , 解决办法: 直接删除windonw下的known_hosts

PS C:\Users\sheng> cd .\.ssh\
PS C:\Users\sheng\.ssh> ls


    目录: C:\Users\sheng\.ssh


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----            22/9/5     16:05            301 config
-a----            22/9/6     08:57           2602 id_rsa
-a----            22/9/6     08:57            569 id_rsa.pub
-a----            22/9/6     09:43            877 known_hosts


PS C:\Users\sheng\.ssh> rm .\known_hosts  # 也可以打开这个文件,删除指定的秘钥  

再次连接一次即可。

16.7 SSH免密登录

xshell 可以自动保存秘钥, 因此可以免密登录

window下的powershell没有免密登录的功能,vscode的ssh插件也没有这个功能, 我们需要手动的生成秘钥, 来进行免密登录

  1. 在window 下 进入cmd 或则 powershell
进入目录:C:\Users\sheng\.ssh> 
输入命令:ssh-keygen -t rsa
目录里面会生成两个文件: id_rsa  id_rsa.pub

# 执行过程: 
PS C:\Users\sheng> cd .\.ssh\
PS C:\Users\sheng\.ssh> ls


    目录: C:\Users\sheng\.ssh


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----            22/9/5     16:05            301 config
-a----            22/9/5     14:17            701 known_hosts


PS C:\Users\sheng\.ssh> pwd

Path
----
C:\Users\sheng\.ssh

PS C:\Users\sheng\.ssh> ssh-keygen -t rsa   # 生成密码文件 
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\sheng/.ssh/id_rsa):    # 回车
Enter passphrase (empty for no passphrase):   # 回车
Enter same passphrase again:  # 回车
Your identification has been saved in C:\Users\sheng/.ssh/id_rsa.
Your public key has been saved in C:\Users\sheng/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:S/fj/sL5AKxi4bvu08kQhDnkrv03UGR+4Pmn3KN51CM sheng@YShengli
The key's randomart image is:
+---[RSA 3072]----+
|   ..o           |
|   .+ . +        |
|    .o = o       |
|   .  . =..      |
|    . .oSo+  .   |
|   o .oo +.oE o  |
|  . . +=oo *+o . |
|     o.o* ooBo   |
|     o*+ .o+o=o  |
+----[SHA256]-----+
PS C:\Users\sheng\.ssh>
PS C:\Users\sheng\.ssh> ls


    目录: C:\Users\sheng\.ssh


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----            22/9/5     16:05            301 config
-a----            22/9/6     08:57           2602 id_rsa
-a----            22/9/6     08:57            569 id_rsa.pub
-a----            22/9/5     14:17            701 known_hosts


PS C:\Users\sheng\.ssh>
  1. 在ubuntu中也生成一个密钥
# 执行过程
linux@ubuntu:~$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  snap  Templates  Videos  work
linux@ubuntu:~$ ssh-keygen -t rsa    # 生成秘钥 
Generating public/private rsa key pair.
Enter file in which to save the key (/home/linux/.ssh/id_rsa):   # 回车 
Enter passphrase (empty for no passphrase):   # 回车 
Enter same passphrase again:    # 回车 
Your identification has been saved in /home/linux/.ssh/id_rsa.
Your public key has been saved in /home/linux/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:GDj+F21ZALB1NGuWaAm8+sNXKTfNBN1CWARa0EqOHvM linux@ubuntu
The key's randomart image is:
+---[RSA 2048]----+
|     .o.+=BB+.   |
|     ..+.*+=o .  |
|    o o==.=...   |
|   . .=+o+ o.    |
|    .o.+S +=     |
|    ... Eo= o    |
|     o. .+ .     |
|      +..        |
|       o         |
+----[SHA256]-----+
linux@ubuntu:~$ 

linux@ubuntu:~$ ls .ssh/     # 生成了2个文件   id_rsa  id_rsa.pub
id_rsa  id_rsa.pub
linux@ubuntu:~$ 
  1. 把window下生成的秘钥放到ubuntu中去, 即可实现免密登录

把window下的 C:\Users\YShengli\.ssh>id_rsa.pub 复制到ubuntu的/home/linux 目录下

linux@ubuntu:~$ ls
Desktop  Documents  Downloads  id_rsa.pub  Music  Pictures  Public  snap  Templates  Videos  work
linux@ubuntu:~$ cat id_rsa.pub >>.ssh/authorized_keys  #把id_rsa.pub 加入到信任密钥列表中去
linux@ubuntu:~$ rm id_rsa.pub   #删除id_rsa.pub
linux@ubuntu:~$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  snap  Templates  Videos  work
linux@ubuntu:~$ 
  1. powershell测试免密连接
PS C:\Users\sheng> ssh linux@192.168.1.20  # 测试连接命令 
Welcome to Ubuntu 18.04.6 LTS (GNU/Linux 5.4.0-124-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

15 updates can be applied immediately.
14 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable

Your Hardware Enablement Stack (HWE) is supported until April 2023.
Last login: Tue Sep  6 08:54:16 2022 from 192.168.1.103
linux@ubuntu:~$
linux@ubuntu:~$ exit  # 断开连接 
注销
Connection to 192.168.1.20 closed.
PS C:\Users\sheng>
  1. vscode 测试免密连接

直接使用vscode的ssh连接ubuntu即可, 中途不需要输入密码即可ssh连接ubuntu18.04

image-20220906091102158

16.8 github和gitee的使用

  1. GitHub是一个面向开源及私有软件项目的托管平台,因为只支持Git作为唯一的版本库格式进行托管,故名GitHub。

由于github国内访问速度过慢,使用很不方便, 使用国内的替代网站, 就是gitee 。

  1. Gitee是开源中国(OSChina)推出的基于Git的代码托管服务
  2. 使用gitee 来创建一个组织和仓库
-> 注册账号并登陆账号
-> 登陆成功后,点击右上角的"+" 
-> 创建组织
-> 输入组织的名称 :iotemb
-> 再次点击右上角的"+" 
-> 新建仓库
-> 输入: 创库名称 : emb2207
-> 根据提示 完成仓库的创建 

image-20220920085421202

16.9 https模式下使用vscode+ubuntu克隆仓库并上传代码

  1. ubuntu18.04 中安装git
linux@ubuntu:~$ sudo apt install git
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
git 已经是最新版 (1:2.17.1-1ubuntu0.12)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 15 个软件包未被升级。
linux@ubuntu:~$ 
  1. 使用git克隆代码
-> gitee中找到自己的创库 
-> 选择 "克隆/下载" 选项
-> 选择 Https 
-> 选择 复制
-> 即可得到仓库的地址  https://gitee.com/iotemb/emb2207.git
-> vscode 使用ssh 登录ubuntu18.04 
-> vscode 中的代码管理器 
-> 克隆仓库 
-> 在弹出的输入框中输入仓库地址: https://gitee.com/iotemb/emb2207.git
-> 选择存放的路径, emb2207.git将来会创建一个目录,这个目录是emb2207 , 这个注意一下 
-> 提示输入账号和密码
	账号: 18612251094
	密码: *********  # 自己账户的密码 
-> 在提示是否打开创库时
-> 选择打开仓库 
-> 把本地的代码复制到 这个目录内 
-> 点击vscode中的"代码管理器"
-> 在消息输入框中输入:一般是说明信息, 哪一个用户
-> 提交
-> 即可完成本地数据上传代码托管平台

image-20220920090841909

https的模式下每次都需要输入账号和密码 , 这种方式使用起来很麻烦 , 可以使用ssh的方式来实现免密登录

16.10 SSH模式来实现Gitee的密码提交代码

  1. 在ubuntu18.04 中生成密码

    请参考16.7 章节去实现

  2. 把密码保存到gitee服务器中

-> 登录gitee账号 
-> 设置 
-> 安全设置 
-> SSH 公钥 
-> 使用命令 获取ubuntu18.04中秘钥的内容 
-> 把秘钥中的内容复制到下图的文本框中
-> 确定
-> 输gitee 的密码 
-> 添加秘钥成功
linux@ubuntu:~$ cd .ssh/
linux@ubuntu:~/.ssh$ cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDCn8gt9bl9rbxB7BMfTuTrVCnA5LfQLC0O9l5geFooLaPKrU3G0OIIPYy/lhxoPbryONuHlmJZr+Ur901iKLtncdwsWhQt7bhVZJsV//vsSAF4yXIy97r6nuQSie8pGWVNGQqIOs0LvNVMwlF4XBtY/hNHN4mCLf8BCvvVdHbm7fOOAqBwnnwT42lOmWg7N4QvOaflqaoGFE+c1f6K5w78cimW0uTWL9a+DD85cGCwLT+vmQP7qhJjCxyjaNgbaDDkU6/xRDH6YrG6t8Kb95ZmhRRQWZfuHUWwaoY3FMVT7jYQrISqk22J2Ghj3ZoM3yw86TEoySe8l+Sb/1zmXGeZ linux@ubuntu
linux@ubuntu:~/.ssh$ 

image-20220915144735894

image-20220915145027915

  1. 重新获取git的ssh地址

    需要把emb2207 这个目录改一个名称, 因为会发生重名的问题, 所以需要改名。



-> 找到自己的创库 
-> 选择 "克隆/下载" 选项
-> 选择 ssh
-> 选择 复制
-> 即可得到仓库的地址  
git@gitee.com:iotemb/emb2207.git
-> 在家目录下克隆代码 
linux@ubuntu:~$ git clone git@gitee.com:iotemb/emb2207.git
正克隆到 'emb2207'...
The authenticity of host 'gitee.com (212.64.63.190)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitee.com,212.64.63.190' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 184, done.
remote: Counting objects: 100% (184/184), done.
remote: Compressing objects: 100% (169/169), done.
remote: Total 184 (delta 55), reused 113 (delta 14), pack-reused 0
接收对象中: 100% (184/184), 120.76 MiB | 336.00 KiB/s, 完成.
处理 delta 中: 100% (55/55), 完成.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值