linux c - get server hostname?

前端 未结 3 1879
眼角桃花
眼角桃花 2021-02-14 18:09

Does anyone know a function to get the hostname of the linux server? I don\'t really want to have to include any headers or compile other libraries, hoping there is a function b

3条回答
  •  青春惊慌失措
    2021-02-14 19:12

    like gethostname() ?

    That's the name of the machine on which your app is running.

    Or read from

    /proc/sys/kernel/hostname
    

    Update

    Simple example

    #include 
    #include 
    #include 
    
    int main(void) {
    
        char hostname[1024];
        gethostname(hostname, 1024);
    
        puts(hostname);
    
        return EXIT_SUCCESS;
    }
    

提交回复
热议问题