梦想博客

Linux备忘录

calendar 2023/07/06
refresh-cw 2023/08/10
1864字,4分钟
tag Linux; SSH;

前言 🔗

Linux下使用Let’s Encrypt配置Nginx SSL证书 这篇文章中,粗略的讲解了一下在linux中如何使用acme.sh来实现ssl证书的签发及配置

尽管上述文章只是很简单的进行了介绍我认为以后会看的懂,但是随着时间的流逝和恰逢大变,明显感觉到记忆力的减弱和身心俱疲以及对技术的痴迷与日俱减,所以我选择了以本文来重构一下我学习linux以及需要记忆的地方

本文的编写以及本人所使用的linux系统为 Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-76-generic x86_64) 不能保证所有的命令能被其他版本所用,也可能会有错误


SSH 🔗

先引用一下来自维基百科的说法


至于如何通过SSH来链接,据我所知目前有三种工具可以实现,分别为:

如果是以前的话,我会推荐使用Xshell 7,但是现在,我发现Win32-OpenSSH更胜一筹(其实vs code也挺好用)

那么来尝试使用一下Win32-OpenSSH吧,首先打开cmd,输入

1ssh root@127.0.0.1

其中root为账户名,127.0.0.1为ip地址,当然您也可以使用在.ssh中设置的别名

.ssh文件夹位于 %USERPROFILE% 目录下 C:\Users\Administrator.DESKTOP-1A2DGNK.ssh

1Host dream
2	HostName 127.0.0.1
3	User root
4	ServerAliveInterval 60
5	IdentityFile C:\Users\Administrator.DESKTOP-1A2DGNK\.ssh\dream
  • dream 别名
  • User 账户
  • ServerAliveInterval 心跳间隔
  • IdentityFile 密钥文件

经过上述配置后,您可以使用以下的命令来进行链接

1ssh dream


如果要尝试使用以上命令,则必须在环境变量中配置ssh否则无法直接使用!
至于如何使用sftp来进行文件的上传和下载这里就不过多赘述了,请使用**Xftp-7**来完成

SSH安全性 🔗

如果仅设置了密码登录,存在被爆破的风险,而且root用户无法远程登录 之前开了个虚拟机采用账号密码都是root,结果一晚上过去我就再也连不上我的服务器了...

这里采用Win32-OpenSSH为例,介绍如何使用密钥登录的方式来防范风险

  • 首先打开Win32-OpenSSH的文件


  • 然后打开ssh-keygen.exe


一共需要输入3次,分别为

  • 密钥的文件名
  • 密钥的密码
  • 密码确认密码

输入完成后会在根目录下生成文件


其中后缀名为.pub需要放入在服务器上,打开ssh远程链接

1#进入.ssh目录
2cd .ssh
3
4#创建authorized_keys文件,如果存在则直接编辑即可
5touch authorized_keys >> 您的pub文件内的内容

pub文件的内容是这种形式 ssh-rsa xxxxxx

没有后缀名的文件为登录私钥,一定要妥善保存,否则后续可能会无法链接到服务器!!!!

  • 然后打开ssh配置项
1cd /etc/ssh
2 
3vi sshd_config

修改以下配置

允许密钥登录 🔗

PubkeyAuthentication yes

允许root登录(此项可能存在风险) 🔗

PermitRootLogin yes

存放pub密钥的文件路径 🔗

AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2

关闭密码登录(即仅允许密钥方式的登录) 🔗

PasswordAuthentication no

以上配置保存完毕后,输入 systemctl restart sshd.service 重启ssh服务,即可完成上述配置!


包管理器 🔗

linux中的包管理器真的是非常的好用,仅需要几个字母便可以直接安装/卸载/更新软件

 1#安装
 2apt install xxx
 3
 4#重新安装
 5apt reinstall xxx
 6
 7#移除
 8apt remove xxx
 9
10#包源更新
11apt update
12
13#更新
14apt upgrade

由于国内特殊的网络原因,导致访问这些软件时下载更新很慢,鉴于此可以参考使用清华大学镜像源来解决此问题

使用vi编辑/etc/apt/sources.list,修改如下

 1# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
 2deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
 3# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
 4deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
 5# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
 6deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
 7# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
 8
 9# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
10# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
11
12deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
13# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
14
15# 预发布软件源,不建议启用
16# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
17# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse

使用包管理器安装的部分软件可能版本过老,如果遇到此情况的话请参考下列官方文档教程处理(其他软件大同小异不做过多赘述)


系统代理 🔗

打开/etc/environment文件

在文件中增加如下内容,然后:wq保存后reboot机器即可正常使用

1http_proxy="http://ip:port/"
2https_proxy="http://ip:port/"
3no_proxy="localhost, 127.0.0.1"

开机自启 🔗

打开 /etc/systemd/system/ 文件夹

新建一个文件,文件名称以xxx.service格式

然后键入如下的内容

 1[Unit]
 2Description=subconverter
 3
 4[Service]
 5WorkingDirectory=/root/subconverter
 6ExecStart=/root/subconverter/subconverter
 7Restart=always
 8RestartSec=3 
 9SyslogIdentifier=dotnet-example
10User=root
11
12[Install]
13WantedBy=multi-user.target
  • Description 说明
  • WorkingDirectory 工作目录
  • ExecStart 执行命令
  • RestartSec 多少秒后重启
  • User 运行的角色

编辑完成后输入如下的命令

  • systemctl daemon-reload 重载
  • systemctl enable xxx.service 启用服务
  • systemctl restart xxx.service 启动服务
  • systemctl status xxx.service 获取服务状态
  • systemctl enable xxx.service –now 可以直接启动

如果status显示为 active (running) 则代表正常启动,否则可能是配置出了一些问题,具体内容请查看日志后决定如何处理


结语 🔗

这些仅是linux系统中非常少的一些常规用法,无奈由于时间和精力不足,无法对其中的内容做更多的处理

感谢micoya,fox等大佬的帮助和教导让我踏上了新世界的大门,行则将至道阻且长,本文会持续更新下去

echo "Hello Linux"

参考资料 🔗

分类