阿里云服务器初始设置

本文详细介绍了如何在Linux服务器上进行远程连接配置,包括创建用户、生成SSH密钥对,并通过ssh-copy-id命令或阿里云控制台导入公钥。此外,还涉及服务器初始化步骤,如安装vim、修改主机名、更新yum源、设置命令提示符样式、创建并授权普通用户,以及设置用户vim样式。最后提到了安装开发工具包的选项。
摘要由CSDN通过智能技术生成

一、远程连接

1.在服务机上操作

创建要远程登录的用户和密码

[root@izwz97s23bov6qmem6poj8z ~]# useradd jundong
[root@izwz97s23bov6qmem6poj8z ~]# passwd jundong
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

2.在客户机上操作(其他机器也行,主要是用来创建密钥对)

2.1.创建ssh-key

[root@node1 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
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:W05lCKLqZ8FWTyAwTsUrBzg7HRzZfZ8ZbacHg/Wnf6c root@node1
The key's randomart image is:
+---[RSA 2048]----+
| o=Bo.o .  +.    |
|oo=.oo.o..o.=..  |
| +.o.......=o=. .|
|o ooo. o  +o. .o |
| ..o+   S o  ..  |
| . . .   =     . |
|  . o   . .     +|
|   o           .o|
|              E  |
+----[SHA256]-----+

2.2.把生成的的秘钥拷贝到服务器,

两种可选方式

方式一:可以手动拷贝

手动拷贝刚刚生成的公钥id_rsa.pub(在当前用户家目录的.ssh/id_rsa.pub),将里面的内容追加到要登录的服务器的目标用户家目录下的.ssh/ authorized_keys中,这里是/home/jundong.ssh/ authorized_keys

方式二:直接用命令拷贝

ssh-copy-id可以方便快捷地把公钥追加到到服务端的authorized_keys文件中,注意用户名,输入密码

ssh-copy-id jundong@120.78.150.47

我的操作

[root@node1 ~]# ssh-copy-id jundong@120.78.150.47
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '120.78.150.47 (120.78.150.47)' can't be established.
ECDSA key fingerprint is SHA256:KwfKwzN4xqw7/VxoVw5IoTi0NhUPZ+h2fSQXi3zgXXY.
ECDSA key fingerprint is MD5:22:b0:8e:c0:21:e6:96:b1:de:bd:bd:c4:08:d9:bf:4d.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jundong@120.78.150.47's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'jundong@120.78.150.47'"
and check to make sure that only the key(s) you wanted were added.

方式三:在阿里云控制台导入

在控制台导入证书,阿里云做的事是:将公钥追加到/root/.ssh/authorized_keys中;修改/etc/ssh/sshd_config,把PasswordAuthentication改成no

1.这种方式会默认把密码登录禁用,只能使用秘钥登录,可以自己手动打开(修改/etc/ssh/sshd_config,把PasswordAuthentication改成yes)

2.这种方式只能使用root用户登录,其他用户不能使用这种方式导入,因为控制台导入只会导入到/root/.ssh/authorized_keys,不会导入到其他用户目录

复制刚刚生成的公钥id_rsa.pub(在当前用户家目录的.ssh/id_rsa.pub),这里是/home/jundong.ssh/ authorized_keys,粘贴到控制台的文本框确定即可,再将密钥对绑定到指定服务器 
这里写图片描述

参考链接:将第三方工具生成的SSH密钥对导入ECS实例_云服务器 ECS(ECS)-阿里云帮助中心

3.测试

3.1在生成秘钥那台客户机上

[root@node1 ~]# ssh jundong@120.78.150.47

Welcome to Alibaba Cloud Elastic Compute Service !

[jundong@izwz97s23bov6qmem6poj8z ~]$
  • 1
  • 2
  • 3
  • 4
  • 5

3.2在其他工具上

拷贝生成的秘钥对的私钥/home/jundong/.ssh/id_rsa(在当前用户家目录的.ssh/id_rsa)到你想要用来登录的客户机,指定其为登录的私钥和对应的用户名即可

这里写图片描述

提示使用密钥文件登录了 
这里写图片描述

windows下其他工具应该也是类似的

【推荐使用Linux下自带的ssh-keygen工具生成公钥和私钥,这样的证书是通用的。在putty或者secureCRT生成的证书会是其他格式或者是带有工具特有的信息,这些证书可能在其他工具无法使用,例如:在SecureCRT生成的证书,putty可能就不能用了】

二、初始化

1、安装基本的编辑软件 vim

yum install vim

2、修改主机名

 
  1. # 查看主机名

  2. uname -n 或者 hostname

  3. # 临时修改

  4. hostname s001

  5. # 永久修改, 方式一, 退出登录终端重新登录生效;

  6. hostnamectl set-hostname s001

  7. # 永久修改, 方式二, 重启生效;

  8. vim /etc/hostname s001

3、更新yum源

 
  1. # 备份源yum源

  2. cd /etc/yum.repos.d

  3. mkdir back

  4. mv *.repo back

  5. # 更新为阿里云的源

  6. wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

  7. # 或

  8. curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

  9. # 清除yum缓存

  10. yum clean all

  11. # 缓存yum源的安装包

  12. yum makecache

  13. #升级所有包

  14. yum -y update

4、设置命令提示符样式(个人偏好)

 
  1. #创建设置命令提示符样式的文件

  2. cd /etc/profile.d/

  3. vi setPS1.sh

   写入内容:

PS1='\[\033[01;32m\][\u@\h \w]\$'
 
  1. #执行, 使生效

  2. . setPS1.sh

 5、创建普通用户并授权(允许其添加sudo后以root身份操作)

 
  1. #创建名为nratel的用户,(adduser -h 查看帮助) (adduser同useradd)

  2. adduser nratel

  3. #设置该用户的登录密码

  4. passwd nratel

  5. #查看sudoers文件(sudo命令的授权管理文件)的位置

  6. whereis sudoers

  7. #查看sudoers文件的读写权限(默认为只读)

  8. ls -l /etc/sudoers

  9. #增加对sudoers文件的写权限

  10. chmod -v u+w /etc/sudoers

  11. #编辑sudoers

  12. vim /etc/sudoers

  在 Allow root to run any commands anywhere 下方增加一行:

 
  1. ## Allow root to run any commands anywhere

  2. root ALL=(ALL) ALL

  3. nratel ALL=(ALL) ALL

  另外,设置在使用sudo时,不重置环境变量(即:保持当前登录用户的环境变量):

  可解决在使用 sudo 时,提示 "commond not found" 的问题。

  另外,设置在使用sudo时,需要输入管理密码的时间间隔。(默认太短,个人觉得30分钟差不多)

 
  1. #注释掉此行

  2. #Defaults    env_reset

  3. #新增一行为

  4. Defaults    !env_reset,timestamp_timeout=30   

  5. #注释掉此行

  6. #Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bi

  保存退出

 
  1. #收回对sudoer文件的写权限

  2. chmod -v u-w /etc/sudoers

 6、用普通用户登录

  直接切换用户,  或在xshell 中新建会话登录。

 
  1. #切换用户

  2. su nratel

7、设置用户的vim样式。

 
  1. #编辑 vim设置

  2. vim ~/.vimrc

  写入内容:

 
  1. set nu

  2. syntax on

  3. set foldenable

  4. set tabstop=4

  5. set softtabstop=4

  6. set shiftwidth=4

  7. set expandtab

  8. colorscheme desert

8、安装开发工具包。(可选)

 
  1. #centos 7.x

  2. sudo yum group info 'Development Tools'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值