Linux 服务器配置


关键字:linux, 命令提示符, zsh, 禁用root, mysql, mongodb, redis, yum源, sed

  • 修改命令提示符
  • oh-my-zsh
  • 禁用root账户,创建普通用户
  • yum 安装 mysql
  • 将 CentOS 的 yum 源更换为国内的阿里云源
  • yum 安装 redis
  • 跨服务器 copy:scp
  • 配置 sftp 文件传输
  • 替换文本内容
  • 证书登录ssh(免密码)

1. 修改命令提示符


  • Linux登录过程中加载配置文件顺序:
/etc/profile → /etc/profile.d/*.sh → ~/.bash_profile → ~/.bashrc → [/etc/bashrc]
  • Linux修改环境变量PS1(命令行提示符),可以使用vi编辑/etc/profile文件,在最后加上:
export PS1='[/u@/h /W]/$ '

即可,其中/u显示当前用户账号,/h显示当前主机名,/W显示当前路径,/显示''符号
/W 代替 /w 就可以实现绝对路径到相对路径的转换
下面简单说说环境下默认的特殊符号所代表的意义:

\d :代表日期,格式为weekday month date,例如:"Mon Aug 1"
\H :完整的主机名称。例如:我的机器名称为:fc4.linux,则这个名称就是fc4.linux
\h :仅取主机的第一个名字,如上例,则为fc4,.linux则被省略
\t :显示时间为24小时格式,如:HH:MM:SS
\T :显示时间为12小时格式
\A :显示时间为24小时格式:HH:MM
\u :当前用户的账号名称
\v :BASH的版本信息
\w :完整的工作目录名称。家目录会以 ~代替
\W :利用basename取得工作目录名称,所以只会列出最后一个目录
\# :下达的第几个命令
\$ :提示字符,如果是root时,提示符为:# ,普通用户则为:$

eg:
比如想改为下边这种样式:

[user@test-server ~]$ 普通用户模式
[root@test-server ~]# root 模式

则要在 /etc/profile 中添加:

export PS1='[\u@test-server \W]\$ ' 

额~~~,其实末尾还有一个空格的......

2. oh-my-zsh


zsh 是 类Unix 系统的一种Shell,Linux/Unix提供了很多种Shell。像 bash 啦,zsh 也是一种。据说是最牛B的一个。
查看系统中的 Shell

cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
/bin/tcsh
/bin/csh

oh-my-zsh官网
终极 Shell 参考教程

3. 禁用 root 账户,创建普通用户


useradd 用户名  #创建新用户
passwd 用户名   #为该用户创建密码

编辑配置文件/etc/ssh/sshd_config 修改后面的yes 为 no 并且去掉前面的注释符.

 #PermitRootLogin yes

 PermitRootLogin no

重新启动 sshd 服务

service sshd restart

从此,root 账户再不能登录,世界和平......

4. yum 安装 MySQL (2016)


添加 mysql 仓库文件

vim /etc/yum.repos.d/mysql-community.repo

内容(2016)为:

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

创建文件/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql内容参考如下:

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.9 (SunOS)

mQGiBD4+owwRBAC14GIfUfCyEDSIePvEW3SAFUdJBtoQHH/nJKZyQT7h9bPlUWC3
RODjQReyCITRrdwyrKUGku2FmeVGwn2u2WmDMNABLnpprWPkBdCk96+OmSLN9brZ
fw2vOUgCmYv2hW0hyDHuvYlQA/BThQoADgj8AW6/0Lo7V1W9/8VuHP0gQwCgvzV3
BqOxRznNCRCRxAuAuVztHRcEAJooQK1+iSiunZMYD1WufeXfshc57S/+yeJkegNW
hxwR9pRWVArNYJdDRT+rf2RUe3vpquKNQU/hnEIUHJRQqYHo8gTxvxXNQc7fJYLV
K2HtkrPbP72vwsEKMYhhr0eKCbtLGfls9krjJ6sBgACyP/Vb7hiPwxh6rDZ7ITnE
kYpXBACmWpP8NJTkamEnPCia2ZoOHODANwpUkP43I7jsDmgtobZX9qnrAXw+uNDI
QJEXM6FSbi0LLtZciNlYsafwAPEOMDKpMqAK6IyisNtPvaLd8lH0bPAnWqcyefep
rv0sxxqUEMcM3o7wwgfN83POkDasDbs3pjwPhxvhz6//62zQJ7Q2TXlTUUwgUmVs
ZWFzZSBFbmdpbmVlcmluZyA8bXlzcWwtYnVpbG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AP7PXm3xmiMPriXf+el5fZZequQoIagf8rdRH
HhRJxQgI0HNknkaOqs8dtrkCDQQ+PqMdEAgA7+GJfxbMdY4wslPnjH9rF4N2qfWs
EN/lxaZoJYc3a6M02WCnHl6ahT2/tBK2w1QI4YFteR47gCvtgb6O1JHffOo2HfLm
RDRiRjd1DTCHqeyX7CHhcghj/dNRlW2Z0l5QFEcmV9U0Vhp3aFfWC4Ujfs3LU+hk
AWzE7zaD5cH9J7yv/6xuZVw411x0h4UqsTcWMu0iM1BzELqX1DY7LwoPEb/O9Rkb
f4fmLe11EzIaCa4PqARXQZc4dhSinMt6K3X4BrRsKTfozBu74F47D8Ilbf5vSYHb
uE5p/1oIDznkg/p8kW+3FxuWrycciqFTcNz215yyX39LXFnlLzKUb/F5GwADBQf+
Lwqqa8CGrRfsOAJxim63CHfty5mUc5rUSnTslGYEIOCR1BeQauyPZbPDsDD9MZ1Z
aSafanFvwFG6Llx9xkU7tzq+vKLoWkm4u5xf3vn55VjnSd1aQ9eQnUcXiL4cnBGo
TbOWI39EcyzgslzBdC++MPjcQTcA7p6JUVsP6oAB3FQWg54tuUo0Ec8bsM8b3Ev4
2LmuQT5NdKHGwHsXTPtl0klk4bQk4OajHsiy1BMahpT27jWjJlMiJc+IWJ0mghkK
Ht926s/ymfdf5HkdQ1cyvsz5tryVI3Fx78XeSYfQvuuwqp2H139pXGEkg0n6KdUO
etdZWhe70YGNPw1yjWJT1IhUBBgRAgAMBQJOdz3tBQkT+wG4ABIHZUdQRwABAQkQ
jHGNO1By4fUUmwCbBYr2+bBEn/L2BOcnw9Z/QFWuhRMAoKVgCFm5fadQ3Afi+UQl
AcOphrnJ
=443I
-----END PGP PUBLIC KEY BLOCK-----
yum list mysql*

会发现有很多

mysql-community-client.i686                                              5.7.16-1.el6                                       mysql57-community
mysql-community-client.x86_64                                            5.7.16-1.el6                                       mysql57-community
mysql-community-common.i686                                              5.7.16-1.el6                                       mysql57-community
mysql-community-common.x86_64                                            5.7.16-1.el6                                       mysql57-community
mysql-community-devel.i686                                               5.7.16-1.el6                                       mysql57-community
mysql-community-devel.x86_64                                             5.7.16-1.el6                                       mysql57-community
mysql-community-embedded.i686                                            5.7.16-1.el6                                       mysql57-community
mysql-community-embedded.x86_64                                          5.7.16-1.el6                                       mysql57-community
mysql-community-embedded-devel.i686                                      5.7.16-1.el6                                       mysql57-community
mysql-community-embedded-devel.x86_64                                    5.7.16-1.el6                                       mysql57-community
mysql-community-libs.i686                                                5.7.16-1.el6                                       mysql57-community
mysql-community-libs.x86_64                                              5.7.16-1.el6                                       mysql57-community
mysql-community-libs-compat.i686                                         5.7.16-1.el6                                       mysql57-community
mysql-community-libs-compat.x86_64                                       5.7.16-1.el6                                       mysql57-community
mysql-community-release.noarch                                           el6-7                                              mysql57-community
mysql-community-server.x86_64                                            5.7.16-1.el6                                       mysql57-community
mysql-community-test.x86_64                                              5.7.16-1.el6                                       mysql57-community

安装 mysql-community-server

yum install mysql-community-server -y

如果不创建文件/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql,可能会提示文件不存在。

如果安装成功了,下一步,启动服务:

service mysqld start

获取安装时的临时 root 登录密码:

sudo grep 'temporary password' /var/log/mysqld.log

输出最后面的就是临时密码:

2016-11-07T07:36:40.581196Z 1 [Note] A temporary password is generated for root@localhost: 32p+a?cuiN6e

修改密码

用临时密码登录进入mysql,修改 root 临时密码:

SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mypass');

授权外网访问

授权能够在任何 IP 使用用户(myuser)密码(mypassword)访问该mysql:

GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

如果正常飘过,那么,大功告成~~

5. 将CentOS的yum源更换为国内的阿里云源


参考阿里云官方教程,仅供参考,可能有细微差别,请对应自己的电脑情况

先检查下wget安装没,下边要用...

wget

没有,先用 yum 安装一个:

yum install wget

备份原来的 yum 源,注意Centos.repo可能会有细微差别,对应自己电脑上的:

mv /etc/yum.repos.d/Centos.repo /etc/yum.repos.d/Centos.repo.backup

下载新的Centos.repo 到/etc/yum.repos.d/,这里是centos 7:

现在是这个了:

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

原来是这个,但是好像不能用了:

  • 如果你用的 不是 阿里云服务器,运行下边这个:
wget -O /etc/yum.repos.d/Centos.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  • 如果你用的 阿里云服务器,运行下边这个:
wget -O /etc/yum.repos.d/Centos.repo http://mirrors.aliyuncs.com/repo/Centos-7.repo

阿里云服务器那个,用内网,不耗流量


之后运行 yum makecache 生成缓存,然后就没然后了......

6. yum 安装 redis


redis 比较好安装,就是有一点不好,yum 的版本比较低。
这里安装的是 v2.8 但是官网是 v3.2 (2016)

安装:

yum install -y redis

启动服务:

service redis start

配置文件在:

/etc/redis.conf

修改配置文件:

bind 0.0.0.0 #外网访问
requirepass mypassword #访问密码

Easy :)

7. 跨服务器 copy: scp


拷贝文件夹 (包括文件夹本身)
scp -r /data/abc [email protected]:/data/abc

8. 配置 sftp 文件传输


参考这里

9. linux 后台启动项目

nohup npm start >console.log 2>&1 &
其中 0、1、2分别代表如下含义:
 0 – stdin (standard input)
 1 – stdout (standard output)
 2 – stderr (standard error)
nohup ... & 

是让命令在后台执行

>console.log

是将信息输出到 console.log 日志中

2>&1

是将标准错误信息转变成标准输出,这样就可以将错误信息输出到 console.log 日志里面来。

10.替换文本内容

linux 下:
sed -i 's/oldstring/newstring/g' full-path-file
MacOS 下:
sed -i '' 's/oldstring/newstring/g' full-path-file

说明:
-i 为替换源文件参数,后面接备份文件后缀名,Linux可选,MacOS必选

11.证书登录ssh(免密码)

将本地 .pub 证书拷贝到服务器对应目录

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

你可能感兴趣的:(Linux 服务器配置)