Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Friday, May 10, 2024

SSH log with key

Reference: 

https://www.digitalocean.com/community/tutorials/how-to-configure-ssh-key-based-authentication-on-a-linux-server 


Step 1 — Creating SSH Keys in Client

On your local computer, generate a SSH key pair by typing:

  1. ssh-keygen
Output
Generating public/private rsa key pair. Enter file in which to save the key (/home/username/.ssh/id_rsa):

The utility will prompt you to select a location for the keys that will be generated. By default, the keys will be stored in the ~/.ssh directory within your user’s home directory. The private key will be called id_rsa and the associated public key will be called id_rsa.pub.

Step 2 — Copying an SSH Public Key to Your Server

The full command will look like this:

  1. cat ~/.ssh/id_rsa.pub | ssh username@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

You may see a message like this:

Output
The authenticity of host '203.0.113.1 (203.0.113.1)' can't be established. ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe. Are you sure you want to continue connecting (yes/no)? yes

This means that your local computer does not recognize the remote host. This will happen the first time you connect to a new host. Type yes and press ENTER to continue.

Tuesday, December 1, 2020

Mount linux partition to windows

 

Local Windows access remote Linux with samba


在linux服务器上配置samba,有两种方式:share方式,user方式。

1. share方式:也就是访问共享文件夹时不需要输入共享文件夹的密码,所有guest都能访问。这种不需要密码的访问当然不安全,但是很方便,也是比较常用的方式。配置步骤如下:

(1) Install samba
sudo apt-get install samba
(2)配置samba
sudo vi /etc/samba/smb.conf
打开这个文件后,按如下添加信息:
[share]
comment = yangxxxx samba share dir
path = /home/wxxhxx/share
browseable = yes
guest ok = yes
create mask = 0777
writable = yes
在这里插入图片描述
注意:这里的写权限单词不要拼写错了writable。
注意:samba版本,我是samba4.7,其中已经没有security = share这种写法了,有的网上资料里都会配置这个参数,这是以前的写法,samba4里面用的是map to guest = Bad User。
保存配置。
(3)重启samba服务:sudo /etc/init.d/smbd restart
(4) windows下访问:
win+r打开这面运行框,输入linux的ip地址后点确定。
在这里插入图片描述
可以看到linux下创建的share共享文件夹,因为我开通了所有权限,所以在这里啥都可以干,新建、修改、删除文件都可以。
在这里插入图片描述


2.user方式:也就是指定的user才有权访问共享文件夹。配置步骤如下:

(1)首先要创建samba user
sudo useradd yangxxsmb (这个名字自定义)
sudo smbpasswd -a yangxxsmb (给这个用户设置访问密码,自定义一个密码,系统会要求输入两边密码,密码自己要记好不然访问不了共享文件夹了。)
(2)samba配置
sudo vi /etc/samba/smb.conf
打开文件,按如下配置,没有的就手动添加。
[share]
comment = yangxxxx samba share dir
path = /home/wxxhxx/share
browseable = yes
writable = yes
valid users = yangxxsmb

(3)重启samba服务
sudo /etc/init.d/smbd restart
(4) windows访问共享文件夹
win+r打开这面运行框,输入linux的ip地址后点确定。
在这里插入图片描述
可以看到share文件夹,但是要访问时需要输入用户名和密码,就是步骤(2)中设置的用户名和密码,输入之后就可以访问了,也可以在里面新建、修改、删除文件。
在这里插入图片描述

Wednesday, November 25, 2020

Install virtualenv

# Step 1: Update your repositories
sudo apt-get update
# Step 2: Install pip for Python 3
sudo apt-get install build-essential libssl-dev libffi-dev python-dev
sudo apt install python3-pip
# Step 3: Use pip to install virtualenv
sudo pip3 install virtualenv
# Step 4: Launch your Python 3 virtual environment, here the name of my virtual environment will be env3
virtualenv -p python3 env3
# Step 5: Activate your new Python 3 environment. There are two ways to do this
. env3/bin/activate # or source env3/bin/activate which does exactly the same thing
# you can make sure you are now working with Python 3
python -- version
# this command will show you what is going on: the python executable you are using is now located inside your virtualenv repository
which python
# Step 6: code your stuff
# Step 7: done? leave the virtual environment
deactivate

 

Friday, September 11, 2020

rsync with a non-standard port

 rsync -avz -e "ssh -p $portNumber" user@remoteip:/path/to/files/ /local/path/

Tuesday, August 4, 2020

Move an Apache Web Root Directory to a New Location on Ubuntu

How To Move an Apache Web Root Directory to a New Location on Ubuntu 

From https://www.digitalocean.com/community/tutorials/how-to-move-an-apache-web-root-to-a-new-location-on-ubuntu-16-04

Prerequisites

To complete this guide, you will need:

  • An Ubuntu 16.04 server with a non-root user with sudo privileges. You can learn more about how to set up a user with these privileges in our Initial Server Setup with Ubuntu 16.04 guide.

  • An Apache2 web server: If you haven’t already set one up, the Apache section of the in-depth article, How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04, can guide you.

  • A new location for your document root: The new document root location is completely configurable based on your needs. If you are moving your document root to a different storage device, you will want to select a location under the device’s mount point.

In this example, we will use the /mnt/volume-nyc1-01 directory. If you are using Block Storage on DigitalOcean, this guide will show you how to mount your drive before continuing with this tutorial.

Step 1 — Copying files to the new location

On a fresh installation of Apache, the document root is located at /var/www/html. If you’re working with an existing server, however, you may have a significantly different setup including multiple document roots in corresponding VirtualHost directives.

You can search for the location of additional document roots using grep. We’ll search in the /etc/apache2/sites-enabled directory to limit our focus to active sites. The -R flag ensures that grep will print both the DocumentRoot and the filename in its output:

  • grep -R "DocumentRoot" /etc/apache2/sites-enabled

The result will look something like the output below, although the names and number of results are likely to be different on an existing installation:

Output
sites-enabled/000-default.conf DocumentRoot /var/www/html

Use the feedback from grep to make sure you’re moving the files that you want to move and updating their appropriate configuration files.

Now that we’ve confirmed the location of our document root, we’ll copy the files to their new location with rsync. Using the -a flag preserves the permissions and other directory properties, while-v provides verbose output so you can follow the progress.

Note: Be sure there is no trailing slash on the directory, which may be added if you use tab completion. When there’s a trailing slash, rsync will dump the contents of the directory into the mount point instead of transferring it into a containing html directory:

  • sudo rsync -av /var/www/html /mnt/volume-nyc1-01

Now we’re ready to update the configuration.

Step 2 — Updating the configuration files

Apache2 makes use of both global and site specific configuration files. For background about the hierarchy of configuration files, take a look at How To Configure the Apache Web Server on an Ubuntu or Debian VPS.

If you’re working with an existing installation, you should modify the virtual host files you found earlier with the grep command. For our example, we’re going to look at the two Virtual Host files that ship with Apache by default, 000-default.conf and default-ssl.conf.

We’ll start by editing the 000-default.conffile:

  • sudo nano /etc/apache2/sites-enabled/000-default.conf

Next we’ll find the line that begins with DocumentRoot and update it with the new location.

Note: You should look for other places the original path showed up, and change those to the new location as well. With a default installation, there’s the DocumentRoot and a Directory block you’ll need to change. On an existing installation, you may find things like aliases and rewrites that need updating, too. Wherever you see the original document root’s path in the output of grep, you’ll need to investigate.

/etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
       DocumentRoot /mnt/volume-nyc1-01/html
        <Directory />
                Options FollowSymLinks
                AllowOverride None
       </Directory>
      <Directory /mnt/volume-nyc1-01/html/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Require all granted
        </Directory>

Step 3 — Restarting Apache

Once you’ve finished the configuration changes, you can make sure the syntax is right with configtest:

  • sudo apachectl configtest

You will get feedback from apachectl configtest with a fresh install:

Output
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message Syntax OK

As long as you get Syntax OK, restart the web server. Otherwise, track down and fix the problems it reported.

Use the following command to restart Apache:

  • sudo systemctl reload apache2

Thursday, May 14, 2020

Screen permission denied


Problem
Cannot make directory '/var/run/screen': Permission denied
Solution
sudo /etc/init.d/screen-cleanup start

Thursday, March 5, 2020

Change ssh port for ubuntu

  1. Edit the file and set Port option

    Type the following command:
    $ sudo vi /etc/ssh/sshd_config
    Locate line that read as follows:
    Port 22
    OR
    #Port 22
    To set the port to 2222, enter:
    Port 2222
  2. Updating your firewall to accept the ssh port 2222 in Linux

    If you are using UFW on a Ubuntu/Debian Linux, type:
    $ sudo ufw allow 2222/tcp
    The syntax for iptables is as follows
    $ sudo /sbin/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 2222 -j ACCEPT

  3. OR if you are using Ubuntu/Debian/Mint Linux:
    $ sudo service ssh restart
Test
ssh xxxx.xxxx.xxxx.xxxx -p 2222

Wednesday, May 2, 2018

undefined reference to `xcb_dri3*'

Problem:
/usr/lib/x86_64-linux-gnu/libGL.so: undefined reference to `xcb_dri3_buffers_from_pixmap

Analysis:
1. libGL.so not working
2. xcb not working

Solution:
1. check if libGL.so is working
ldd  /usr/lib/x86_64-linux-gnu/libGL.so
if working: go to step 2
if not: install opengl

2. add -lxcb -lxcb-dri3
Note that we need to add all libs in the target makefile or qt pro file. Actually, I fix this with this solution.

3. sudo apt-get update
add -lxcb -lxcb-dri3


Monday, December 18, 2017

ssh login timeout but scp works

Problem:
ssh login timeout
scp and sftp work

Analysis:

scp and sftp working means port 22 works.

This was due to my router blocking TCP keepalive messages when I connected wirelessly (go figure).
Solution:
ssh my_server -o TCPKeepAlive=no 

From the documentation:
TCPKeepAlive
  Specifies whether the system should send TCP keepalive messages
  to the other side. If they are sent, death of the connection or
  crash of one of the machines will be properly noticed.  However,
  this means that connections will die if the route is down tem-
  porarily, and some people find it annoying.  On the other hand,
  if TCP keepalives are not sent, sessions may hang indefinitely on
  the server, leaving "ghost" users and consuming server resources.

  The default is "yes" (to send TCP keepalive messages), and the
  server will notice if the network goes down or the client host
  crashes.  This avoids infinitely hanging sessions.

  To disable TCP keepalive messages, the value should be set to
  "no".

Friday, November 17, 2017

Mount a hard disk in ubuntu + reboot (may have different filesystem type)

1. $ sudo fdisk -l
display device
Disk /dev/sdc: 2 TiB, 2197949513728 bytes, 4292870144 sectors

2. $ mkdir /media/DiskC
Better use /media instead of /mnt, because /mnt is usually the automatic mount point, may be replaced when restarting.

3 $ mount /dev/sdc /media/DiskC
Then error occured
mount: wrong fs type, bad option, bad superblock on /dev/sdc,
       missing codepage or helper program, or other error

       In some cases useful info is found in syslog - try
       dmesg | tail or so.

(for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount. helper program)
This is relevant given that you are trying to mount NFS. The /sbin/mount.nfs helper program is provided by nfs-common. You can install it with:
sudo apt install nfs-common
On the other hand, if you are trying to mount CIFS, the helper program is provided by cifs-utils. You can install it with:
sudo apt install cifs-utils
4. convert file type
$ sudo mkfs.ext4 /dev/sdc

goto step 3.
Done!


Auto mount on start up.

[IMPORTANT] sudo cp /etc/fstab /etc/fstab.old - Create a backup of the fstab file just in case something unwanted happens.

Auto-mount at boot

We want the drive to auto-mount at boot.  This usually means editing /etc/fstab.

Firstly, it's always best to use the drives UUID.  To find the drive's UUID do

ls -al /dev/disk/by-uuid/

Copy the resultant UUID (for your disk) and then open fstab for editing (note I'm using vim here but use whatever editor you prefer):

sudo vim /etc/fstab

You want to add an entry for the UUID and mount point.  Below is an example of an fstab file with an entry added for the mount above:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sdb1 during installation
UUID=63a46dce-b895-4c1f-9034-b1104694a956 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sdb5 during installation
UUID=b9b9ee49-c69c-475b-894b-1279d44034ae none            swap    sw              0       0
# data drive
UUID=19fa40a3-fd17-412f-9063-a29ca0e75f93 /media/data   ext4    defaults        0       0

Note: the entry added is the last line.

Test fstab

We always want to test the fstab before rebooting (an incorrect fstab can render a disk unbootable).  To test do:

sudo findmnt --verify

check the last line for errors.  Warnings can help in improving your fstab.


Use lsblk -o NAME,FSTYPE,UUID to find out the UUIDs and filesystems of the partition you want to mount. For example:
$ lsblk -o NAME,FSTYPE,UUID
NAME   FSTYPE UUID
sda
├─sda2
├─sda5 swap   498d24e5-7755-422f-be45-1b78d50b44e8
└─sda1 ext4   d4873b63-0956-42a7-9dcf-bd64e495a9ff

NTFS

UUID=<uuid> <pathtomount> ntfs uid=<userid>,gid=<groupid>,umask=0022,sync,auto,rw 0 0
Examples for the <> variables:
  • <uuid>=3087106951D2FA7E
  • <pathtomount>=/home/data/
  • <userid>=1000
  • <groupid>=1000
Use id -u <username> to get the userid and id -g <username> to get the groupid.

# Mount all disks
sudo mount -a